|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const padutils = require('./pad_utils').padutils; |
|
const padcookie = require('./pad_cookie').padcookie; |
|
const Tinycon = require('tinycon/tinycon'); |
|
const hooks = require('./pluginfw/hooks'); |
|
const padeditor = require('./pad_editor').padeditor; |
|
|
|
exports.chat = (() => { |
|
let isStuck = false; |
|
let userAndChat = false; |
|
let chatMentions = 0; |
|
return { |
|
show() { |
|
$('#chaticon').removeClass('visible'); |
|
$('#chatbox').addClass('visible'); |
|
this.scrollDown(true); |
|
chatMentions = 0; |
|
Tinycon.setBubble(0); |
|
$('.chat-gritter-msg').each(function () { |
|
$.gritter.remove(this.id); |
|
}); |
|
}, |
|
focus: () => { |
|
setTimeout(() => { |
|
$('#chatinput').focus(); |
|
}, 100); |
|
}, |
|
|
|
stickToScreen(fromInitialCall) { |
|
if (pad.settings.hideChat) { |
|
return; |
|
} |
|
this.show(); |
|
isStuck = (!isStuck || fromInitialCall); |
|
$('#chatbox').hide(); |
|
|
|
setTimeout(() => { |
|
$('#chatbox, .sticky-container').toggleClass('stickyChat', isStuck); |
|
$('#chatbox').css('display', 'flex'); |
|
}, 0); |
|
|
|
padcookie.setPref('chatAlwaysVisible', isStuck); |
|
$('#options-stickychat').prop('checked', isStuck); |
|
}, |
|
chatAndUsers(fromInitialCall) { |
|
const toEnable = $('#options-chatandusers').is(':checked'); |
|
if (toEnable || !userAndChat || fromInitialCall) { |
|
this.stickToScreen(true); |
|
$('#options-stickychat').prop('checked', true); |
|
$('#options-chatandusers').prop('checked', true); |
|
$('#options-stickychat').prop('disabled', 'disabled'); |
|
userAndChat = true; |
|
} else { |
|
$('#options-stickychat').prop('disabled', false); |
|
userAndChat = false; |
|
} |
|
padcookie.setPref('chatAndUsers', userAndChat); |
|
$('#users, .sticky-container') |
|
.toggleClass('chatAndUsers popup-show stickyUsers', userAndChat); |
|
$('#chatbox').toggleClass('chatAndUsersChat', userAndChat); |
|
}, |
|
hide() { |
|
|
|
if ($('#options-stickychat').prop('checked')) { |
|
this.stickToScreen(); |
|
$('#options-stickychat').prop('checked', false); |
|
} else { |
|
$('#chatcounter').text('0'); |
|
$('#chaticon').addClass('visible'); |
|
$('#chatbox').removeClass('visible'); |
|
} |
|
}, |
|
scrollDown(force) { |
|
if ($('#chatbox').hasClass('visible')) { |
|
if (force || !this.lastMessage || !this.lastMessage.position() || |
|
this.lastMessage.position().top < ($('#chattext').outerHeight() + 20)) { |
|
|
|
|
|
$('#chattext').animate( |
|
{scrollTop: $('#chattext')[0].scrollHeight}, |
|
{duration: 400, queue: false}); |
|
this.lastMessage = $('#chattext > p').eq(-1); |
|
} |
|
} |
|
}, |
|
send() { |
|
const text = $('#chatinput').val(); |
|
if (text.replace(/\s+/, '').length === 0) return; |
|
this._pad.collabClient.sendMessage({type: 'CHAT_MESSAGE', text}); |
|
$('#chatinput').val(''); |
|
}, |
|
addMessage(msg, increment, isHistoryAdd) { |
|
|
|
msg.time += this._pad.clientTimeOffset; |
|
|
|
|
|
let minutes = `${new Date(msg.time).getMinutes()}`; |
|
let hours = `${new Date(msg.time).getHours()}`; |
|
if (minutes.length === 1) minutes = `0${minutes}`; |
|
if (hours.length === 1) hours = `0${hours}`; |
|
const timeStr = `${hours}:${minutes}`; |
|
|
|
|
|
if (!msg.userId) { |
|
|
|
|
|
|
|
|
|
|
|
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.'); |
|
} |
|
|
|
|
|
|
|
msg.userId = padutils.escapeHtml(msg.userId); |
|
const authorClass = `author-${msg.userId.replace(/[^a-y0-9]/g, (c) => { |
|
if (c === '.') return '-'; |
|
return `z${c.charCodeAt(0)}z`; |
|
})}`; |
|
|
|
const text = padutils.escapeHtmlWithClickableLinks(msg.text, '_blank'); |
|
|
|
const authorName = msg.userName == null ? html10n.get('pad.userlist.unnamed') |
|
: padutils.escapeHtml(msg.userName); |
|
|
|
|
|
const ctx = { |
|
authorName, |
|
author: msg.userId, |
|
text, |
|
sticky: false, |
|
timestamp: msg.time, |
|
timeStr, |
|
duration: 4000, |
|
}; |
|
|
|
|
|
const alreadyFocused = $('#chatinput').is(':focus'); |
|
|
|
|
|
const chatOpen = $('#chatbox').hasClass('visible'); |
|
|
|
|
|
const myName = $('#myusernameedit').val(); |
|
const wasMentioned = |
|
text.toLowerCase().indexOf(myName.toLowerCase()) !== -1 && myName !== 'undefined'; |
|
|
|
|
|
if (wasMentioned && !alreadyFocused && !isHistoryAdd && !chatOpen) { |
|
chatMentions++; |
|
Tinycon.setBubble(chatMentions); |
|
ctx.sticky = true; |
|
} |
|
|
|
|
|
hooks.aCallAll('chatNewMessage', ctx, () => { |
|
const 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'); |
|
else $('#chattext').append(html); |
|
|
|
|
|
if (increment && !isHistoryAdd) { |
|
|
|
let count = Number($('#chatcounter').text()); |
|
count++; |
|
$('#chatcounter').text(count); |
|
|
|
if (!chatOpen && ctx.duration > 0) { |
|
$.gritter.add({ |
|
|
|
text: $('<p>') |
|
.append($('<span>').addClass('author-name').html(ctx.authorName)) |
|
.append(ctx.text), |
|
sticky: ctx.sticky, |
|
time: 5000, |
|
position: 'bottom', |
|
class_name: 'chat-gritter-msg', |
|
}); |
|
} |
|
} |
|
}); |
|
|
|
|
|
$('#chatinput').click(() => { |
|
chatMentions = 0; |
|
Tinycon.setBubble(0); |
|
}); |
|
if (!isHistoryAdd) this.scrollDown(); |
|
}, |
|
init(pad) { |
|
this._pad = pad; |
|
$('#chatinput').on('keydown', (evt) => { |
|
|
|
|
|
if ((evt.altKey === true && evt.which === 67) || evt.which === 27) { |
|
|
|
$(':focus').blur(); |
|
padeditor.ace.focus(); |
|
evt.preventDefault(); |
|
return false; |
|
} |
|
}); |
|
|
|
const self = this; |
|
$('body:not(#chatinput)').on('keypress', function (evt) { |
|
if (evt.altKey && evt.which === 67) { |
|
|
|
$(this).blur(); |
|
self.show(); |
|
$('#chatinput').focus(); |
|
evt.preventDefault(); |
|
} |
|
}); |
|
|
|
$('#chatinput').keypress((evt) => { |
|
|
|
if (evt.which === 13 || evt.which === 10) { |
|
evt.preventDefault(); |
|
this.send(); |
|
} |
|
}); |
|
|
|
|
|
|
|
$('#chatcounter').text(0); |
|
$('#chatloadmessagesbutton').click(() => { |
|
const start = Math.max(this.historyPointer - 20, 0); |
|
const end = this.historyPointer; |
|
|
|
if (start === end) return; |
|
|
|
$('#chatloadmessagesbutton').css('display', 'none'); |
|
$('#chatloadmessagesball').css('display', 'block'); |
|
|
|
pad.collabClient.sendMessage({type: 'GET_CHAT_MESSAGES', start, end}); |
|
this.historyPointer = start; |
|
}); |
|
}, |
|
}; |
|
})(); |
|
|