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

268 lines
9 KiB
JavaScript
Raw Normal View History

/**
* 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-09-09 23:55:43 +02:00
var Tinycon = require('tinycon/tinycon');
2013-03-19 03:21:53 +01:00
var hooks = require('./pluginfw/hooks');
2015-03-26 17:58:13 +01:00
var padeditor = require('./pad_editor').padeditor;
2011-07-14 17:15:38 +02:00
var chat = (function()
{
var isStuck = false;
2015-02-09 20:11:35 +01:00
var userAndChat = false;
var gotInitialMessages = false;
var historyPointer = 0;
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").css('display', 'flex');
2013-01-29 02:55:36 +01:00
$("#gritter-notice-wrapper").hide();
2012-02-03 22:52:57 +01:00
self.scrollDown();
chatMentions = 0;
Tinycon.setBubble(0);
2011-07-14 17:15:38 +02:00
},
focus: function ()
{
2015-03-26 17:58:13 +01:00
setTimeout(function(){
$("#chatinput").focus();
},100);
},
stickToScreen: function(fromInitialCall) // Make chat stick to right hand side of screen
{
chat.show();
isStuck = (!isStuck || fromInitialCall);
$('#chatbox, .sticky-container').toggleClass("stickyChat", isStuck);
padcookie.setPref("chatAlwaysVisible", isStuck);
$('#options-stickychat').prop('checked', isStuck);
},
2015-01-21 17:08:54 +01:00
chatAndUsers: function(fromInitialCall)
{
2015-02-09 19:01:45 +01:00
var toEnable = $('#options-chatandusers').is(":checked");
2015-02-09 20:11:35 +01:00
if(toEnable || !userAndChat || fromInitialCall){
2015-01-19 02:45:49 +01:00
chat.stickToScreen(true);
2015-01-21 17:08:54 +01:00
$('#options-stickychat').prop('checked', true)
2015-02-09 20:11:35 +01:00
$('#options-chatandusers').prop('checked', true)
2015-01-19 02:45:49 +01:00
$('#options-stickychat').prop("disabled", "disabled");
2015-02-09 20:11:35 +01:00
userAndChat = true;
2015-01-19 02:45:49 +01:00
}else{
$('#options-stickychat').prop("disabled", false);
userAndChat = false;
2015-01-19 02:45:49 +01:00
}
padcookie.setPref("chatAndUsers", userAndChat);
$('#users, .sticky-container').toggleClass("chatAndUsers stickyUsers", userAndChat);
$("#chatbox").toggleClass("chatAndUsersChat", userAndChat);
2015-01-19 02:45:49 +01:00
},
hide: function ()
2011-07-14 17:15:38 +02:00
{
// decide on hide logic based on chat window being maximized or not
2013-12-15 11:02:43 +01:00
if ($('#options-stickychat').prop('checked')) {
chat.stickToScreen();
$('#options-stickychat').prop('checked', false);
}
else {
2013-12-15 11:02:43 +01:00
$("#chatcounter").text("0");
$("#chaticon").show();
$("#chatbox").hide();
$.gritter.removeAll();
$("#gritter-notice-wrapper").show();
}
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()) {
// if we use a slow animate here we can have a race condition when a users focus can not be moved away
// from the last message recieved.
2015-02-08 15:34:48 +01:00
$('#chattext').animate({scrollTop: $('#chattext')[0].scrollHeight}, { duration: 400, queue: false });
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();
if(text.replace(/\s+/,'').length == 0)
return;
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, isHistoryAdd)
{
2011-07-14 17:15:38 +02:00
//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;
2011-07-14 17:15:38 +02:00
//create the authorclass
if (!msg.userId) {
/*
* If, for a bug or a database corruption, the message coming from the
* server does not contain the userId field (see for example #3731),
* let's be defensive and replace it with "unknown".
*/
msg.userId = "unknown";
console.warn('The "userId" field of a chat message coming from the server was not present. Replacing with "unknown". This may be a bug or a database corruption.');
}
2011-07-14 17:15:38 +02:00
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
var authorName = msg.userName == null ? _('pad.userlist.unnamed') : padutils.escapeHtml(msg.userName);
// the hook args
var ctx = {
"authorName" : authorName,
"author" : msg.userId,
"text" : text,
"sticky" : false,
"timestamp" : msg.time,
"timeStr" : timeStr,
"duration" : 4000
2011-11-27 17:35:20 +01:00
}
// is the users focus already in the chatbox?
var alreadyFocused = $("#chatinput").is(":focus");
2013-01-29 02:55:36 +01:00
// does the user already have the chatbox open?
var chatOpen = $("#chatbox").is(":visible");
2013-01-29 02:55:36 +01:00
// does this message contain this user's name? (is the curretn user mentioned?)
var myName = $('#myusernameedit').val();
var wasMentioned = (text.toLowerCase().indexOf(myName.toLowerCase()) !== -1 && myName != "undefined");
if(wasMentioned && !alreadyFocused && !isHistoryAdd && !chatOpen)
{ // If the user was mentioned show for twice as long and flash the browser window
chatMentions++;
Tinycon.setBubble(chatMentions);
ctx.sticky = true;
}
// Call chat message hook
hooks.aCallAll("chatNewMessage", ctx, function() {
var html = "<p data-authorId='" + msg.userId + "' class='" + authorClass + "'><b>" + authorName + ":</b><span class='time " + authorClass + "'>" + ctx.timeStr + "</span> " + ctx.text + "</p>";
if(isHistoryAdd)
$(html).insertAfter('#chatloadmessagesbutton');
2011-11-27 17:35:20 +01:00
else
$("#chattext").append(html);
//should we increment the counter??
if(increment && !isHistoryAdd)
2011-11-27 17:35:20 +01:00
{
// Update the counter of unread messages
var count = Number($("#chatcounter").text());
count++;
$("#chatcounter").text(count);
if(!chatOpen && ctx.duration > 0) {
2013-01-29 02:55:36 +01:00
$.gritter.add({
// (string | mandatory) the heading of the notification
title: ctx.authorName,
2013-01-29 02:55:36 +01:00
// (string | mandatory) the text inside the notification
text: ctx.text,
2013-01-29 02:55:36 +01:00
// (bool | optional) if you want it to fade out on its own or just sit there
sticky: ctx.sticky,
2013-01-29 02:55:36 +01:00
// (int | optional) the time you want it to be alive for before fading out
time: ctx.duration
2013-01-29 02:55:36 +01:00
});
}
2011-11-27 17:35:20 +01:00
}
});
// Clear the chat mentions when the user clicks on the chat input box
$('#chatinput').click(function(){
chatMentions = 0;
Tinycon.setBubble(0);
});
if(!isHistoryAdd)
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;
2015-05-06 01:32:36 +02:00
$("#chatinput").on("keydown", function(evt){
2015-03-26 17:58:13 +01:00
// If the event is Alt C or Escape & we're already in the chat menu
// Send the users focus back to the pad
if((evt.altKey == true && evt.which === 67) || evt.which === 27){
// If we're in chat already..
$(':focus').blur(); // required to do not try to remove!
padeditor.ace.focus(); // Sends focus back to pad
2015-05-06 01:32:36 +02:00
evt.preventDefault();
return false;
2015-03-26 17:58:13 +01:00
}
});
2015-05-06 01:32:36 +02:00
$('body:not(#chatinput)').on("keypress", function(evt){
if (evt.altKey && evt.which == 67){
// Alt c focuses on the Chat window
$(this).blur();
2015-05-06 01:32:36 +02:00
chat.show();
$("#chatinput").focus();
evt.preventDefault();
}
});
2015-03-26 17:58:13 +01:00
$("#chatinput").keypress(function(evt){
2011-07-14 17:15:38 +02:00
//if the user typed enter, fire the send
if(evt.which == 13 || evt.which == 10)
2011-07-14 17:15:38 +02:00
{
evt.preventDefault();
self.send();
}
});
// initial messages are loaded in pad.js' _afterHandshake
$("#chatcounter").text(0);
$("#chatloadmessagesbutton").click(function()
{
var start = Math.max(self.historyPointer - 20, 0);
var end = self.historyPointer;
if(start == end) // nothing to load
return;
$("#chatloadmessagesbutton").css("display", "none");
$("#chatloadmessagesball").css("display", "block");
pad.collabClient.sendMessage({"type": "GET_CHAT_MESSAGES", "start": start, "end": end});
self.historyPointer = start;
});
2011-07-14 17:15:38 +02:00
}
}
return self;
}());
exports.chat = chat;