10
|
1 'use strict';
|
|
2
|
|
3 let currentChatId = null;
|
12
|
4 let eventSource;
|
10
|
5
|
|
6 function selectChat(div) {
|
|
7 let chatId = div.getAttribute('chat');
|
|
8 if( chatId === currentChatId )
|
|
9 return;
|
|
10 let selected = div.parentNode.querySelector('[selected]');
|
|
11 if( selected ) selected.removeAttribute('selected');
|
|
12 div.setAttribute('selected','');
|
|
13 ajax(`get_chat.js?chat=${chatId}`);
|
|
14 currentChatId = chatId;
|
12
|
15
|
|
16 if(eventSource) eventSource.close();
|
|
17 eventSource = new EventSource(`${location.origin}/chat/${chatId}`);
|
|
18 eventSource.onmessage = function(event) {
|
|
19 eval(event.data);
|
|
20 };
|
|
21 }
|
|
22
|
|
23 function gotChat(html) {
|
|
24 document.querySelector('div[posts]').innerHTML = html;
|
|
25 fixDates();
|
|
26 document.querySelector('div[input] textarea').focus();
|
|
27 document.querySelector('div[input]').scrollIntoView({block: 'end'});
|
10
|
28 }
|
|
29
|
|
30 function fixTextarea(event) {
|
|
31 let textarea = event.target;
|
|
32 textarea.style.height = 'initial';
|
|
33 textarea.style.height = (textarea.scrollHeight+2) + 'px';
|
|
34 textarea.scrollIntoViewIfNeeded(false);
|
|
35 }
|
|
36
|
|
37 const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
|
|
38
|
|
39 function addPost() {
|
|
40 let textarea = document.querySelector('div[input] textarea');
|
|
41 let text = textarea.value;
|
|
42 if( text.trim() === '' )
|
|
43 return;
|
|
44 ajax(`add_post.js?chat=${currentChatId}&content=${encodeURIComponent(text)}`);
|
|
45 textarea.value = '';
|
|
46 }
|
|
47
|
|
48 function textareaKey(event) {
|
|
49 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
|
|
50 event.preventDefault();
|
|
51 addPost();
|
|
52 }
|
|
53 }
|
|
54
|
|
55 function fixDates() {
|
|
56 let spans = document.querySelectorAll('span[when][fix]');
|
|
57 for( let span of spans ) {
|
|
58 span.textContent = new Date(Number(span.textContent)).toLocaleString();
|
|
59 span.removeAttribute('fix');
|
|
60 }
|
|
61 }
|
11
|
62
|
|
63 function deleteChat() {
|
|
64 ajax(`delete_chat.js?chat=${currentChatId}`);
|
|
65 }
|
12
|
66
|
|
67 function added(html) {
|
|
68 let input = document.querySelector('div[input]');
|
|
69 input.insertAdjacentHTML('beforebegin',html);
|
14
|
70 fixDates();
|
12
|
71 input.scrollIntoView({block: 'end'});
|
|
72 }
|