2011-12-04 15:33:56 +00:00
|
|
|
/**
|
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
|
|
|
* This helps other people to understand this code better and helps them to improve it.
|
|
|
|
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
|
|
|
|
*/
|
|
|
|
|
2011-08-11 14:26:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc., 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-01-16 04:16:11 +00:00
|
|
|
var padutils = require('/pad_utils').padutils;
|
2012-01-26 16:22:44 +00:00
|
|
|
var browser = require('/ace2_common').browser;
|
2012-01-28 22:24:14 +00:00
|
|
|
var padcookie = require('/pad_cookie').padcookie;
|
2012-01-16 04:16:11 +00:00
|
|
|
|
2011-07-14 15:15:38 +00:00
|
|
|
var chat = (function()
|
|
|
|
{
|
2012-01-25 19:03:25 +00:00
|
|
|
var isStuck = false;
|
2011-12-07 14:14:52 +00:00
|
|
|
var sDuration = 500;
|
|
|
|
var hDuration = 750;
|
2011-11-27 20:39:47 +00:00
|
|
|
var chatMentions = 0;
|
|
|
|
var title = document.title;
|
2012-01-26 16:22:44 +00:00
|
|
|
if (browser.mobile){
|
|
|
|
sDuration = hDuration = 0;
|
2011-12-07 14:14:52 +00:00
|
|
|
}
|
2011-07-14 15:15:38 +00:00
|
|
|
var self = {
|
|
|
|
show: function ()
|
|
|
|
{
|
2011-08-10 17:06:22 +00:00
|
|
|
$("#chaticon").hide("slide", {
|
|
|
|
direction: "down"
|
2011-12-07 14:14:52 +00:00
|
|
|
}, hDuration, function ()
|
2011-07-14 15:15:38 +00:00
|
|
|
{
|
2011-08-10 17:06:22 +00:00
|
|
|
$("#chatbox").show("slide", {
|
|
|
|
direction: "down"
|
2011-12-07 14:14:52 +00:00
|
|
|
}, sDuration, self.scrollDown);
|
2011-08-10 17:06:22 +00:00
|
|
|
$("#chatbox").resizable(
|
|
|
|
{
|
|
|
|
handles: 'nw',
|
2011-12-05 19:53:43 +00:00
|
|
|
minHeight: 40,
|
|
|
|
minWidth: 80,
|
2011-08-10 17:06:22 +00:00
|
|
|
start: function (event, ui)
|
|
|
|
{
|
2011-08-10 17:18:50 +00:00
|
|
|
$("#focusprotector").show();
|
2011-08-10 17:06:22 +00:00
|
|
|
},
|
|
|
|
stop: function (event, ui)
|
|
|
|
{
|
2011-08-10 17:18:50 +00:00
|
|
|
$("#focusprotector").hide();
|
2011-08-10 17:06:22 +00:00
|
|
|
|
2012-01-26 16:22:44 +00:00
|
|
|
if(browser.mobile) {
|
|
|
|
$("#chatbox").css({right: "0px", bottom: "32px", left: "", top: ""});
|
|
|
|
} else {
|
|
|
|
$("#chatbox").css({right: "20px", bottom: "0px", left: "", top: ""});
|
|
|
|
}
|
|
|
|
|
2011-08-10 17:06:22 +00:00
|
|
|
self.scrollDown();
|
|
|
|
}
|
|
|
|
});
|
2011-07-14 15:15:38 +00:00
|
|
|
});
|
2011-11-27 20:39:47 +00:00
|
|
|
chatMentions = 0;
|
|
|
|
document.title = title;
|
2011-07-14 15:15:38 +00:00
|
|
|
},
|
2012-01-28 22:24:14 +00:00
|
|
|
stickToScreen: function(fromInitialCall) // Make chat stick to right hand side of screen
|
2012-01-25 19:03:25 +00:00
|
|
|
{
|
|
|
|
chat.show();
|
2012-01-28 22:24:14 +00:00
|
|
|
if(!isStuck || fromInitialCall) { // Stick it to
|
|
|
|
padcookie.setPref("chatAlwaysVisible", true);
|
2012-01-27 00:43:00 +00:00
|
|
|
$('#chatbox').css({"right":"0px", "top":"36px", "border-radius":"0px", "height":"auto", "border-right":"none", "border-left":"1px solid #ccc", "border-top":"none", "background-color":"#f1f1f1", "width":"185px"});
|
2012-01-26 16:22:44 +00:00
|
|
|
$('#chattext').css({"top":"0px"});
|
2012-01-27 00:43:00 +00:00
|
|
|
$('#editorcontainer').css({"right":"192px", "width":"auto"});
|
2012-01-25 19:03:25 +00:00
|
|
|
isStuck = true;
|
2012-01-26 16:22:44 +00:00
|
|
|
} else { // Unstick it
|
2012-01-28 22:24:14 +00:00
|
|
|
padcookie.setPref("chatAlwaysVisible", false);
|
2012-01-26 16:22:44 +00:00
|
|
|
$('#chatbox').css({"right":"20px", "top":"auto", "border-top-left-radius":"5px", "border-top-right-radius":"5px", "border-right":"1px solid #999", "height":"200px", "border-top":"1px solid #999", "background-color":"#f7f7f7"});
|
|
|
|
$('#chattext').css({"top":"25px"});
|
2012-01-25 19:03:25 +00:00
|
|
|
$('#editorcontainer').css({"right":"0px", "width":"100%"});
|
|
|
|
isStuck = false;
|
|
|
|
}
|
2012-01-26 16:22:44 +00:00
|
|
|
},
|
2011-07-14 15:15:38 +00:00
|
|
|
hide: function ()
|
|
|
|
{
|
|
|
|
$("#chatcounter").text("0");
|
2011-12-07 14:14:52 +00:00
|
|
|
$("#chatbox").hide("slide", { direction: "down" }, sDuration, function()
|
2011-07-14 15:15:38 +00:00
|
|
|
{
|
2011-12-07 14:14:52 +00:00
|
|
|
$("#chaticon").show("slide", { direction: "down" }, hDuration);
|
|
|
|
});
|
2011-07-14 15:15:38 +00:00
|
|
|
},
|
|
|
|
scrollDown: function()
|
|
|
|
{
|
|
|
|
if($('#chatbox').css("display") != "none")
|
|
|
|
$('#chattext').animate({scrollTop: $('#chattext')[0].scrollHeight}, "slow");
|
|
|
|
},
|
|
|
|
send: function()
|
|
|
|
{
|
|
|
|
var text = $("#chatinput").val();
|
2012-01-16 05:05:19 +00:00
|
|
|
this._pad.collabClient.sendMessage({"type": "CHAT_MESSAGE", "text": text});
|
2011-07-14 15:15:38 +00:00
|
|
|
$("#chatinput").val("");
|
|
|
|
},
|
|
|
|
addMessage: function(msg, increment)
|
|
|
|
{
|
|
|
|
//correct the time
|
2012-01-16 05:05:19 +00:00
|
|
|
msg.time += this._pad.clientTimeOffset;
|
2011-07-14 15:15:38 +00:00
|
|
|
|
|
|
|
//create the time string
|
|
|
|
var minutes = "" + new Date(msg.time).getMinutes();
|
|
|
|
var hours = "" + new Date(msg.time).getHours();
|
|
|
|
if(minutes.length == 1)
|
|
|
|
minutes = "0" + minutes ;
|
|
|
|
if(hours.length == 1)
|
|
|
|
hours = "0" + hours ;
|
|
|
|
var timeStr = hours + ":" + minutes;
|
|
|
|
|
|
|
|
//create the authorclass
|
|
|
|
var authorClass = "author-" + msg.userId.replace(/[^a-y0-9]/g, function(c)
|
|
|
|
{
|
|
|
|
if (c == ".") return "-";
|
|
|
|
return 'z' + c.charCodeAt(0) + 'z';
|
|
|
|
});
|
|
|
|
|
|
|
|
var text = padutils.escapeHtmlWithClickableLinks(padutils.escapeHtml(msg.text), "_blank");
|
2011-11-27 16:35:20 +00:00
|
|
|
|
|
|
|
/* Performs an action if your name is mentioned */
|
|
|
|
var myName = $('#myusernameedit').val();
|
|
|
|
myName = myName.toLowerCase();
|
|
|
|
var chatText = text.toLowerCase();
|
|
|
|
var wasMentioned = false;
|
|
|
|
if (chatText.indexOf(myName) !== -1 && myName != "undefined"){
|
|
|
|
wasMentioned = true;
|
|
|
|
}
|
|
|
|
/* End of new action */
|
|
|
|
|
2011-07-14 15:15:38 +00:00
|
|
|
var authorName = msg.userName == null ? "unnamed" : padutils.escapeHtml(msg.userName);
|
|
|
|
|
|
|
|
var html = "<p class='" + authorClass + "'><b>" + authorName + ":</b><span class='time'>" + timeStr + "</span> " + text + "</p>";
|
|
|
|
$("#chattext").append(html);
|
|
|
|
|
|
|
|
//should we increment the counter??
|
|
|
|
if(increment)
|
|
|
|
{
|
|
|
|
var count = Number($("#chatcounter").text());
|
|
|
|
count++;
|
|
|
|
$("#chatcounter").text(count);
|
2011-11-27 16:35:20 +00:00
|
|
|
// chat throb stuff -- Just make it throw for twice as long
|
|
|
|
if(wasMentioned)
|
|
|
|
{ // If the user was mentioned show for twice as long and flash the browser window
|
2011-11-27 20:39:47 +00:00
|
|
|
if (chatMentions == 0){
|
|
|
|
title = document.title;
|
|
|
|
}
|
2011-11-27 16:35:20 +00:00
|
|
|
$('#chatthrob').html("<b>"+authorName+"</b>" + ": " + text);
|
|
|
|
$('#chatthrob').effect("pulsate", {times:1,mode:"hide"},4000);
|
2011-11-27 20:39:47 +00:00
|
|
|
chatMentions++;
|
|
|
|
document.title = "("+chatMentions+") " + title;
|
2011-11-27 16:35:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$('#chatthrob').html("<b>"+authorName+"</b>" + ": " + text);
|
|
|
|
$('#chatthrob').effect("pulsate", {times:1,mode:"hide"},2000);
|
|
|
|
}
|
2011-07-14 15:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.scrollDown();
|
2011-07-20 20:50:58 +00:00
|
|
|
|
2011-07-14 15:15:38 +00:00
|
|
|
},
|
2012-01-16 05:05:19 +00:00
|
|
|
init: function(pad)
|
2011-07-14 15:15:38 +00:00
|
|
|
{
|
2012-01-16 05:05:19 +00:00
|
|
|
this._pad = pad;
|
2011-07-14 15:15:38 +00:00
|
|
|
$("#chatinput").keypress(function(evt)
|
|
|
|
{
|
|
|
|
//if the user typed enter, fire the send
|
|
|
|
if(evt.which == 13)
|
|
|
|
{
|
|
|
|
evt.preventDefault();
|
|
|
|
self.send();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for(var i in clientVars.chatHistory)
|
|
|
|
{
|
|
|
|
this.addMessage(clientVars.chatHistory[i], false);
|
|
|
|
}
|
|
|
|
$("#chatcounter").text(clientVars.chatHistory.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2011-12-04 15:33:56 +00:00
|
|
|
}());
|
2012-01-16 01:23:48 +00:00
|
|
|
|
|
|
|
exports.chat = chat;
|
2012-01-26 16:22:44 +00:00
|
|
|
|