2015-08-03 11:35:01 +02:00
|
|
|
var Chat = {
|
|
|
|
left : null,
|
|
|
|
right: null,
|
|
|
|
room: null,
|
|
|
|
previous: null,
|
2015-12-15 11:54:49 +01:00
|
|
|
date: null,
|
|
|
|
lastScroll: null,
|
2015-08-03 11:35:01 +02:00
|
|
|
addSmiley: function(element) {
|
|
|
|
var n = document.querySelector('#chat_textarea');
|
|
|
|
n.value = n.value + element.dataset.emoji;
|
|
|
|
n.focus();
|
|
|
|
Dialog.clear();
|
|
|
|
},
|
|
|
|
sendMessage: function(jid, muc)
|
|
|
|
{
|
|
|
|
var n = document.querySelector('#chat_textarea');
|
|
|
|
var text = n.value;
|
|
|
|
n.value = "";
|
|
|
|
n.focus();
|
2015-11-13 13:37:39 +01:00
|
|
|
movim_textarea_autoheight(n);
|
2015-08-03 11:35:01 +02:00
|
|
|
Chat_ajaxSendMessage(jid, encodeURIComponent(text), muc);
|
|
|
|
},
|
2015-11-13 13:37:39 +01:00
|
|
|
focus: function()
|
|
|
|
{
|
|
|
|
if(document.documentElement.clientWidth > 1024) {
|
|
|
|
document.querySelector('#chat_textarea').focus();
|
|
|
|
}
|
|
|
|
},
|
2015-08-03 11:35:01 +02:00
|
|
|
appendTextarea: function(value)
|
|
|
|
{
|
|
|
|
},
|
|
|
|
notify : function(title, body, image)
|
|
|
|
{
|
|
|
|
if(document_focus == false) {
|
|
|
|
movim_title_inc();
|
|
|
|
movim_desktop_notification(title, body, image);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
empty : function()
|
|
|
|
{
|
|
|
|
Chat_ajaxGet();
|
|
|
|
},
|
|
|
|
setBubbles : function(left, right, room) {
|
|
|
|
var div = document.createElement('div');
|
|
|
|
|
|
|
|
div.innerHTML = left;
|
2015-11-13 13:37:39 +01:00
|
|
|
Chat.left = div.firstChild.cloneNode(true);
|
2015-08-03 11:35:01 +02:00
|
|
|
div.innerHTML = right;
|
2015-11-13 13:37:39 +01:00
|
|
|
Chat.right = div.firstChild.cloneNode(true);
|
2015-08-03 11:35:01 +02:00
|
|
|
div.innerHTML = room;
|
2015-11-13 13:37:39 +01:00
|
|
|
Chat.room = div.firstChild.cloneNode(true);
|
2015-12-15 11:54:49 +01:00
|
|
|
|
|
|
|
Chat.setScrollBehaviour();
|
|
|
|
},
|
|
|
|
setScrollBehaviour : function() {
|
|
|
|
var discussion = document.querySelector('#chat_widget div.contained');
|
|
|
|
discussion.onscroll = function() {
|
|
|
|
if(this.scrollTop < 1) {
|
|
|
|
var chat = document.querySelector('#chat_widget');
|
|
|
|
Chat.lastScroll = this.scrollHeight;
|
|
|
|
Chat_ajaxGetHistory(chat.dataset.jid, Chat.date);
|
|
|
|
}
|
|
|
|
};
|
2015-08-03 11:35:01 +02:00
|
|
|
},
|
|
|
|
appendMessages : function(messages) {
|
|
|
|
if(messages) {
|
2015-12-15 11:54:49 +01:00
|
|
|
Chat.date = messages[0].published;
|
2015-11-13 13:37:39 +01:00
|
|
|
for(var i = 0, len = messages.length; i < len; ++i ) {
|
2015-12-15 11:54:49 +01:00
|
|
|
Chat.appendMessage(messages[i], false);
|
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
}
|
2015-08-03 11:35:01 +02:00
|
|
|
},
|
2015-12-15 11:54:49 +01:00
|
|
|
appendMessage : function(message, prepend) {
|
2015-08-03 11:35:01 +02:00
|
|
|
if(message.body == '') return;
|
|
|
|
|
|
|
|
var bubble = null;
|
|
|
|
var id = null;
|
|
|
|
|
2015-08-10 23:42:45 +02:00
|
|
|
var scrolled = MovimTpl.isPanelScrolled();
|
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
if(message.type == 'groupchat') {
|
|
|
|
bubble = Chat.room.cloneNode(true);
|
|
|
|
|
|
|
|
id = message.jidfrom + '_conversation';
|
|
|
|
|
|
|
|
if(message.body.match(/^\/me/)) {
|
|
|
|
bubble.querySelector('div').className = 'quote';
|
|
|
|
message.body = message.body.substr(4);
|
|
|
|
}
|
|
|
|
|
2015-12-21 22:59:03 +01:00
|
|
|
bubble.querySelector('p.message').innerHTML = message.body;
|
2015-12-15 11:54:49 +01:00
|
|
|
bubble.querySelector('span.info').innerHTML = message.publishedPrepared;
|
2015-12-21 22:59:03 +01:00
|
|
|
bubble.querySelector('p.user').className = 'user ' + message.color;
|
2015-08-03 11:35:01 +02:00
|
|
|
|
2015-12-21 22:59:03 +01:00
|
|
|
bubble.querySelector('p.user').onclick = function(n) {
|
2015-08-03 11:35:01 +02:00
|
|
|
var textarea = document.querySelector('#chat_textarea');
|
|
|
|
textarea.value = this.innerHTML + ', ' + textarea.value;
|
|
|
|
textarea.focus();
|
|
|
|
};
|
|
|
|
|
2015-12-21 22:59:03 +01:00
|
|
|
bubble.querySelector('p.user').innerHTML = message.resource;
|
2015-08-03 11:35:01 +02:00
|
|
|
var conversation = document.getElementById(id);
|
|
|
|
if(conversation) {
|
2015-11-13 13:37:39 +01:00
|
|
|
conversation.appendChild(bubble);
|
|
|
|
}
|
2015-08-03 11:35:01 +02:00
|
|
|
|
2015-12-21 22:59:03 +01:00
|
|
|
//bubble.querySelector('p.message').className = '';
|
2015-09-22 08:18:52 +02:00
|
|
|
} else if(Chat.left != null) {
|
2015-08-03 11:35:01 +02:00
|
|
|
if(message.session == message.jidfrom) {
|
|
|
|
bubble = Chat.right.cloneNode(true);
|
|
|
|
if(Chat.previous == 'right') {
|
|
|
|
bubble.className += ' same';
|
|
|
|
}
|
|
|
|
|
|
|
|
Chat.previous = 'right';
|
|
|
|
id = message.jidto + '_conversation';
|
|
|
|
} else {
|
|
|
|
bubble = Chat.left.cloneNode(true);
|
|
|
|
if(Chat.previous == 'left') {
|
|
|
|
bubble.className += ' same';
|
|
|
|
}
|
|
|
|
|
|
|
|
Chat.previous = 'left';
|
|
|
|
id = message.jidfrom + '_conversation';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(message.body.match(/^\/me/)) {
|
|
|
|
bubble.querySelector('div.bubble').className = 'bubble quote';
|
|
|
|
message.body = message.body.substr(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bubble) {
|
2015-12-21 22:59:03 +01:00
|
|
|
bubble.querySelector('div.bubble > p').innerHTML = message.body;
|
2015-09-10 15:41:45 +02:00
|
|
|
|
2015-12-21 22:59:03 +01:00
|
|
|
bubble.querySelector('div.bubble > span.info').innerHTML = message.publishedPrepared;
|
2015-12-15 11:54:49 +01:00
|
|
|
|
|
|
|
if(prepend) {
|
|
|
|
Chat.date = message.published;
|
|
|
|
var discussion = document.querySelector('#chat_widget div.contained');
|
|
|
|
|
|
|
|
// We prepend
|
|
|
|
movim_prepend(id, bubble.outerHTML);
|
|
|
|
|
|
|
|
// And we scroll where we were
|
|
|
|
var scrollDiff = discussion.scrollHeight - Chat.lastScroll;
|
|
|
|
discussion.scrollTop += scrollDiff;
|
|
|
|
Chat.lastScroll = discussion.scrollHeight;
|
|
|
|
} else {
|
|
|
|
movim_append(id, bubble.outerHTML);
|
|
|
|
}
|
2015-08-03 11:35:01 +02:00
|
|
|
|
|
|
|
bubble.querySelector('div.bubble').className = 'bubble';
|
2015-11-13 13:37:39 +01:00
|
|
|
|
2015-12-15 11:54:49 +01:00
|
|
|
if(bubble.className.indexOf('oppose') > -1
|
|
|
|
&& prepend == null) MovimTpl.scrollPanel();
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 11:54:49 +01:00
|
|
|
if(scrolled && prepend == null) MovimTpl.scrollPanel();
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 17:50:34 +02:00
|
|
|
MovimWebsocket.attach(function() {
|
2015-12-15 11:54:49 +01:00
|
|
|
var chat = document.querySelector('#chat_widget');
|
|
|
|
var jid = chat.dataset.jid;
|
2015-08-24 17:50:34 +02:00
|
|
|
if(jid) {
|
2015-09-22 08:18:52 +02:00
|
|
|
MovimTpl.showPanel();
|
2015-08-24 17:50:34 +02:00
|
|
|
Chat_ajaxGet(jid);
|
2015-11-13 13:37:39 +01:00
|
|
|
Notification.current('chat|' + jid);
|
2015-08-24 17:50:34 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-08-19 11:28:28 +02:00
|
|
|
Upload.attach(function() {
|
|
|
|
var textarea = document.querySelector('#chat_textarea');
|
|
|
|
textarea.value = Upload.get + ' ' + textarea.value;
|
|
|
|
textarea.focus();
|
|
|
|
movim_textarea_autoheight(textarea);
|
|
|
|
});
|
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
var state = 0;
|