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