pad.libre-service.eu-etherpad/src/static/js/chat.js

174 lines
5.5 KiB
JavaScript
Raw Normal View History

/**
* 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
*/
/**
* 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-03-07 02:27:03 +01:00
var padutils = require('./pad_utils').padutils;
var padcookie = require('./pad_cookie').padcookie;
2012-01-16 05:16:11 +01:00
require('./tinycon');
2011-07-14 17:15:38 +02:00
var chat = (function()
{
var isStuck = false;
var chatMentions = 0;
2011-07-14 17:15:38 +02:00
var self = {
show: function ()
{
2012-02-03 21:20:02 +01:00
$("#chaticon").hide();
$("#chatbox").show();
2012-02-03 22:52:57 +01:00
self.scrollDown();
chatMentions = 0;
Tinycon.setBubble(0);
2011-07-14 17:15:38 +02:00
},
stickToScreen: function(fromInitialCall) // Make chat stick to right hand side of screen
{
chat.show();
if(!isStuck || fromInitialCall) { // Stick it to
padcookie.setPref("chatAlwaysVisible", true);
$('#chatbox').addClass("stickyChat");
$('#chattext').css({"top":"0px"});
$('#editorcontainer').css({"right":"192px", "width":"auto"});
isStuck = true;
} else { // Unstick it
padcookie.setPref("chatAlwaysVisible", false);
$('#chatbox').removeClass("stickyChat");
$('#chattext').css({"top":"25px"});
$('#editorcontainer').css({"right":"0px", "width":"100%"});
isStuck = false;
}
},
2011-07-14 17:15:38 +02:00
hide: function ()
{
$("#chatcounter").text("0");
2012-02-03 21:20:02 +01:00
$("#chaticon").show();
$("#chatbox").hide();
2011-07-14 17:15:38 +02:00
},
scrollDown: function()
{
if($('#chatbox').css("display") != "none"){
2012-07-12 10:34:11 +02:00
if(!self.lastMessage || !self.lastMessage.position() || self.lastMessage.position().top < $('#chattext').height()) {
2012-07-11 18:42:59 +02:00
$('#chattext').animate({scrollTop: $('#chattext')[0].scrollHeight}, "slow");
self.lastMessage = $('#chattext > p').eq(-1);
2012-07-11 18:42:59 +02:00
}
}
2011-07-14 17:15:38 +02:00
},
send: function()
{
var text = $("#chatinput").val();
2012-01-16 06:05:19 +01:00
this._pad.collabClient.sendMessage({"type": "CHAT_MESSAGE", "text": text});
2011-07-14 17:15:38 +02:00
$("#chatinput").val("");
},
addMessage: function(msg, increment)
{
//correct the time
2012-01-16 06:05:19 +01:00
msg.time += this._pad.clientTimeOffset;
2011-07-14 17:15:38 +02: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(msg.text, "_blank");
2011-11-27 17:35:20 +01: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 17:15:38 +02:00
var authorName = msg.userName == null ? "unnamed" : padutils.escapeHtml(msg.userName);
2012-02-06 16:16:31 +01:00
var html = "<p class='" + authorClass + "'><b>" + authorName + ":</b><span class='time " + authorClass + "'>" + timeStr + "</span> " + text + "</p>";
2011-07-14 17:15:38 +02:00
$("#chattext").append(html);
//should we increment the counter??
if(increment)
{
var count = Number($("#chatcounter").text());
count++;
// is the users focus already in the chatbox?
var alreadyFocused = $("#chatinput").is(":focus");
2011-07-14 17:15:38 +02:00
$("#chatcounter").text(count);
2011-11-27 17:35:20 +01:00
// chat throb stuff -- Just make it throw for twice as long
if(wasMentioned && !alreadyFocused)
2011-11-27 17:35:20 +01:00
{ // If the user was mentioned show for twice as long and flash the browser window
2012-02-03 21:20:02 +01:00
$('#chatthrob').html("<b>"+authorName+"</b>" + ": " + text).show().delay(4000).hide(400);
chatMentions++;
Tinycon.setBubble(chatMentions);
2011-11-27 17:35:20 +01:00
}
else
{
2012-02-03 21:20:02 +01:00
$('#chatthrob').html("<b>"+authorName+"</b>" + ": " + text).show().delay(2000).hide(400);
2011-11-27 17:35:20 +01:00
}
2011-07-14 17:15:38 +02:00
}
// Clear the chat mentions when the user clicks on the chat input box
$('#chatinput').click(function(){
chatMentions = 0;
Tinycon.setBubble(0);
});
2011-07-14 17:15:38 +02:00
self.scrollDown();
2011-07-14 17:15:38 +02:00
},
2012-01-16 06:05:19 +01:00
init: function(pad)
2011-07-14 17:15:38 +02:00
{
2012-01-16 06:05:19 +01:00
this._pad = pad;
2011-07-14 17:15:38 +02:00
$("#chatinput").keypress(function(evt)
{
//if the user typed enter, fire the send
if(evt.which == 13)
{
evt.preventDefault();
self.send();
}
});
2012-03-13 22:44:33 +01:00
var that = this;
$.each(clientVars.chatHistory, function(i, o){
that.addMessage(o, false);
})
2011-07-14 17:15:38 +02:00
$("#chatcounter").text(clientVars.chatHistory.length);
}
}
return self;
}());
exports.chat = chat;