comparison default/static/chat.js @ 85:05b500bd6235

chat: chat implementation, first draft: this chat use the new dynamic pages feature. Updates are pushed directly by server. Identities are used to retrieve avatar, and first letter of nickname is used to generate an avatar is none is found (temporary, a more elaborate avatar generation should follow in the future). Scroll is done automatically when new messages arrive, except if scroll is not at the end, as it probably means that user is checking history. User can resize text area and use [shift] + [enter] to enter multi-line messages. History will then scroll to bottom after message has been sent.
author Goffi <goffi@goffi.org>
date Wed, 03 Jan 2018 01:12:16 +0100
parents
children
comparison
equal deleted inserted replaced
84:b2ef34e602cf 85:05b500bd6235
1 /* SàT Template: Chat page handling
2 *
3 * Copyright (C) 2017 Jérôme Poisson (goffi@goffi.org)
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 var msgInput = document.getElementById('message_input');
21 var messages = document.getElementById('messages');
22 const messagesTransitionOri = messages.style.transition;
23
24 msgInput.addEventListener('keypress', function(event) {
25 if (event.which == 13 && !event.shiftKey) {
26 if (messages.style.height !== '100%') {
27 messages.style.transition = messagesTransitionOri;
28 setTimeout(function() {
29 messages.style.transition = 'initial';
30 messages.scrollTop = messages.scrollHeight;
31 }, 1000);
32 messages.style.height = "100%";
33 }
34 if (!this.value.trim()) {
35 return;
36 }
37 socket.send({'type': 'msg',
38 'body': this.value});
39 this.value = '';
40 event.preventDefault();
41 }}
42 );
43
44 var mutationCb = function(mutationsList) {
45 scrollPos = messages.scrollTop + messages.clientHeight;
46 if (messages.lastChild.offsetTop - scrollPos - 10 <= 0) {
47 // we auto scroll only if we are at the bottom of the page
48 // else the use is probably checking history
49 // Note thas this doesn't take margin into account,
50 // we suppose margin to be 0 for messages children
51 messages.scrollTop = messages.scrollHeight;
52 }
53 };
54
55 var observer = new MutationObserver(mutationCb);
56
57 observer.observe(messages, { childList: true });
58 // we want to start with scrolling at bottom
59 messages.scrollTop = messages.scrollHeight;
60 messages.style.transition = 'initial';