1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/movim_ynh.git synced 2024-09-03 19:46:19 +02:00
movim_ynh/sources/app/widgets/Chat/chat.js

181 lines
5.7 KiB
JavaScript
Raw Normal View History

var Chat = {
left : null,
right: null,
room: null,
previous: null,
2015-12-15 11:54:49 +01:00
date: null,
lastScroll: null,
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();
movim_textarea_autoheight(n);
Chat_ajaxSendMessage(jid, encodeURIComponent(text), muc);
},
focus: function()
{
if(document.documentElement.clientWidth > 1024) {
document.querySelector('#chat_textarea').focus();
}
},
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;
Chat.left = div.firstChild.cloneNode(true);
div.innerHTML = right;
Chat.right = div.firstChild.cloneNode(true);
div.innerHTML = room;
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);
}
};
},
appendMessages : function(messages) {
if(messages) {
2015-12-15 11:54:49 +01:00
Chat.date = messages[0].published;
for(var i = 0, len = messages.length; i < len; ++i ) {
2015-12-15 11:54:49 +01:00
Chat.appendMessage(messages[i], false);
}
}
},
2015-12-15 11:54:49 +01:00
appendMessage : function(message, prepend) {
if(message.body == '') return;
var bubble = null;
var id = null;
2015-08-10 23:42:45 +02:00
var scrolled = MovimTpl.isPanelScrolled();
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-12-21 22:59:03 +01:00
bubble.querySelector('p.user').onclick = function(n) {
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;
var conversation = document.getElementById(id);
if(conversation) {
conversation.appendChild(bubble);
}
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) {
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);
}
bubble.querySelector('div.bubble').className = 'bubble';
2015-12-15 11:54:49 +01:00
if(bubble.className.indexOf('oppose') > -1
&& prepend == null) MovimTpl.scrollPanel();
}
}
2015-12-15 11:54:49 +01:00
if(scrolled && prepend == null) MovimTpl.scrollPanel();
}
}
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);
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);
});
var state = 0;