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