10
|
1 'use strict';
|
|
2
|
18
|
3 let title = document.title;
|
10
|
4 let currentChatId = null;
|
12
|
5 let eventSource;
|
73
|
6 let lastUpdate = Date.now();
|
35
|
7 let userId;
|
63
|
8 let filebinUrl;
|
89
|
9 let spy;
|
10
|
10
|
16
|
11 function evalEvent(event) {
|
17
|
12 // console.log(event);
|
16
|
13 eval(event.data);
|
|
14 }
|
|
15
|
15
|
16 function setUserEventSource(userId) {
|
|
17 let userEventSource = new EventSource(`${location.origin}/user/${userId}`);
|
16
|
18 userEventSource.onmessage = evalEvent;
|
15
|
19 }
|
|
20
|
59
|
21 function selectChat(chatId) {
|
48
|
22 document.querySelector('div[chat_content]').setAttribute('show','posts');
|
10
|
23 if( chatId === currentChatId )
|
|
24 return;
|
59
|
25 let div = document.querySelector(`div[chat="${chatId}"]`);
|
10
|
26 let selected = div.parentNode.querySelector('[selected]');
|
|
27 if( selected ) selected.removeAttribute('selected');
|
|
28 div.setAttribute('selected','');
|
|
29 ajax(`get_chat.js?chat=${chatId}`);
|
|
30 currentChatId = chatId;
|
59
|
31 history.replaceState(null,null,`?chat=${chatId}`);
|
53
|
32 clearUnread();
|
12
|
33
|
|
34 if(eventSource) eventSource.close();
|
|
35 eventSource = new EventSource(`${location.origin}/chat/${chatId}`);
|
16
|
36 eventSource.onmessage = evalEvent;
|
12
|
37 }
|
|
38
|
27
|
39 function back() {
|
48
|
40 document.querySelector('div[chat_content]').setAttribute('show','chats');
|
27
|
41 }
|
|
42
|
12
|
43 function gotChat(html) {
|
|
44 document.querySelector('div[posts]').innerHTML = html;
|
19
|
45 fixPosts();
|
12
|
46 document.querySelector('div[input] textarea').focus();
|
|
47 document.querySelector('div[input]').scrollIntoView({block: 'end'});
|
10
|
48 }
|
|
49
|
|
50 function fixTextarea(event) {
|
|
51 let textarea = event.target;
|
|
52 textarea.style.height = 'initial';
|
|
53 textarea.style.height = (textarea.scrollHeight+2) + 'px';
|
|
54 textarea.scrollIntoViewIfNeeded(false);
|
|
55 }
|
|
56
|
|
57 const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
|
|
58
|
|
59 function addPost() {
|
|
60 let textarea = document.querySelector('div[input] textarea');
|
|
61 let text = textarea.value;
|
|
62 if( text.trim() === '' )
|
|
63 return;
|
79
|
64 let url = `add_post.js?chat=${currentChatId}`;
|
|
65 let reply = document.querySelector('div[reply]').getAttribute('reply');
|
|
66 if( reply )
|
|
67 url += `&reply=${reply}`;
|
|
68 ajax(url,`content=${encodeURIComponent(text)}`);
|
10
|
69 textarea.value = '';
|
84
|
70 textarea.style.height = 'initial';
|
79
|
71 closeReply();
|
10
|
72 }
|
|
73
|
|
74 function textareaKey(event) {
|
|
75 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
|
|
76 event.preventDefault();
|
|
77 addPost();
|
|
78 }
|
|
79 }
|
|
80
|
78
|
81 function editTextareaKey(event) {
|
|
82 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
|
|
83 event.preventDefault();
|
|
84 savePost(event.target);
|
|
85 }
|
|
86 }
|
|
87
|
19
|
88 function fixPosts() {
|
35
|
89 let divs = document.querySelectorAll('div[post][fix]');
|
19
|
90 for( let div of divs ) {
|
79
|
91 for( let whenSpan of div.querySelectorAll('[when]') ) {
|
|
92 whenSpan.textContent = new Date(Number(whenSpan.textContent)).toLocaleString([],{dateStyle:'short',timeStyle:'short'});
|
|
93 }
|
35
|
94 let textDiv = div.querySelector('div[text]');
|
|
95 textDiv.innerHTML = urlsToLinks(textDiv.innerHTML);
|
79
|
96 let reply = div.querySelector('blockquote');
|
|
97 if( reply )
|
|
98 reply.innerHTML = urlsToLinks(reply.innerHTML);
|
|
99 let html;
|
|
100 if( div.getAttribute('author') === userId ) {
|
|
101 html = document.querySelector('div[hidden] span[pulldown=author]').innerHTML;
|
|
102 } else {
|
|
103 html = document.querySelector('div[hidden] span[pulldown=other]').innerHTML;
|
|
104 }
|
|
105 div.querySelector('span[pulldown]').innerHTML = html;
|
19
|
106 div.removeAttribute('fix');
|
|
107 }
|
10
|
108 }
|
11
|
109
|
|
110 function deleteChat() {
|
20
|
111 let dialog = document.querySelector('dialog[delete_chat]');
|
|
112 openModal(dialog);
|
|
113 }
|
|
114
|
|
115 function doDeleteChat(el) {
|
|
116 closeModal(el);
|
11
|
117 ajax(`delete_chat.js?chat=${currentChatId}`);
|
|
118 }
|
12
|
119
|
107
|
120 function deleteOld() {
|
|
121 let dialog = document.querySelector('dialog[delete_old]');
|
|
122 openModal(dialog);
|
|
123 }
|
|
124
|
|
125 function doDeleteOld(el) {
|
|
126 closeModal(el);
|
|
127 ajax(`delete_old.js?chat=${currentChatId}`);
|
|
128 }
|
|
129
|
23
|
130 let currentPostId;
|
|
131
|
38
|
132 function getPostId(el) {
|
|
133 while(true) {
|
|
134 let postId = el.getAttribute('post');
|
|
135 if( postId )
|
|
136 return postId;
|
|
137 el = el.parentNode;
|
|
138 }
|
|
139 }
|
|
140
|
|
141 function deletePost(el) {
|
|
142 currentPostId = getPostId(el);
|
23
|
143 let dialog = document.querySelector('dialog[delete_post]');
|
|
144 openModal(dialog);
|
|
145 }
|
|
146
|
|
147 function doDeletePost(el) {
|
|
148 closeModal(el);
|
|
149 ajax(`delete_post.js?post=${currentPostId}`);
|
|
150 }
|
|
151
|
|
152 function deleted(postId) {
|
|
153 let div = document.querySelector(`div[post="${postId}"]`);
|
|
154 if( div )
|
|
155 div.outerHTML = '';
|
|
156 }
|
|
157
|
38
|
158 function editPost(el) {
|
|
159 ajax(`edit_post.js?post=${getPostId(el)}`);
|
24
|
160 }
|
|
161
|
|
162 function openEditPost(postId,text) {
|
|
163 currentPostId = postId;
|
|
164 let dialog = document.querySelector('dialog[edit_post]');
|
|
165 let textarea = dialog.querySelector('textarea');
|
|
166 textarea.value = text;
|
|
167 openModal(dialog);
|
|
168 }
|
|
169
|
|
170 function savePost(el) {
|
|
171 let text = document.querySelector('dialog[edit_post] textarea').value;
|
|
172 closeModal(el);
|
|
173 ajax(`save_post.js?post=${currentPostId}`,`content=${encodeURIComponent(text)}`);
|
|
174 }
|
|
175
|
|
176 function edited(postId,html) {
|
|
177 let div = document.querySelector(`div[post="${postId}"]`);
|
|
178 if( div ) {
|
|
179 div.outerHTML = html;
|
|
180 fixPosts();
|
|
181 }
|
|
182 }
|
|
183
|
104
|
184 function openMute() {
|
|
185 ajax(`open_mute.js?chat=${currentChatId}`);
|
|
186 }
|
|
187
|
|
188 function doOpenMute(muted) {
|
|
189 let dialog = document.querySelector('dialog[mute]');
|
|
190 let checkbox = dialog.querySelector('input');
|
|
191 checkbox.checked = muted;
|
|
192 openModal(dialog);
|
|
193 }
|
|
194
|
|
195 function saveMute(el) {
|
|
196 let muted = document.querySelector('dialog[mute] input').checked;
|
|
197 closeModal(el);
|
|
198 ajax(`save_mute.js?chat=${currentChatId}&muted=${muted}`);
|
|
199 }
|
|
200
|
77
|
201 function active() {
|
|
202 let url = 'active.js';
|
|
203 if( currentChatId )
|
|
204 url += `?chat=${currentChatId}`;
|
|
205 ajax(url);
|
|
206 }
|
|
207
|
92
|
208 function added(html,updated) {
|
12
|
209 let input = document.querySelector('div[input]');
|
|
210 input.insertAdjacentHTML('beforebegin',html);
|
19
|
211 fixPosts();
|
12
|
212 input.scrollIntoView({block: 'end'});
|
40
|
213 if( document.hasFocus() )
|
77
|
214 active();
|
92
|
215 lastUpdate = updated;
|
12
|
216 }
|
15
|
217
|
30
|
218 function getChats(chatId,updated) {
|
15
|
219 let first = document.querySelector('div[chat]');
|
53
|
220 if( !first || first.getAttribute('chat') !== chatId ) {
|
15
|
221 // console.log('getChats');
|
|
222 ajax('get_chats.js');
|
92
|
223 if( updated )
|
|
224 lastUpdate = updated;
|
89
|
225 } else if( first && (currentChatId !== chatId || spy) ) {
|
53
|
226 incUnread(first);
|
15
|
227 }
|
40
|
228 if( !document.hasFocus() ) {
|
31
|
229 document.title = title + ' *';
|
|
230 }
|
15
|
231 }
|
|
232
|
|
233 function gotChats(html) {
|
|
234 document.querySelector('div[chats]').innerHTML = html;
|
|
235 if( currentChatId ) {
|
|
236 let current = document.querySelector(`div[chat="${currentChatId}"]`);
|
16
|
237 if( current ) {
|
|
238 current.setAttribute('selected','');
|
|
239 current.scrollIntoViewIfNeeded(false);
|
61
|
240 clearUnread();
|
16
|
241 } else {
|
|
242 currentChatId = null;
|
|
243 document.querySelector('div[posts]').innerHTML = '';
|
|
244 }
|
15
|
245 }
|
|
246 }
|
18
|
247
|
|
248 window.onfocus = function() {
|
33
|
249 // console.log('onfocus');
|
18
|
250 document.title = title;
|
77
|
251 active();
|
18
|
252 };
|
19
|
253
|
|
254 let urlRegex = /(^|\s)(https?:\/\/\S+)/g;
|
66
|
255 let filebinUrlRegex = /(^|\s)(https:\/\/filebin.net\/[0-9a-f]+\/(\S+))/g;
|
19
|
256
|
|
257 function urlsToLinks(text) {
|
66
|
258 text = text.replace( filebinUrlRegex, '$1<a target="_blank" href="$2">$3</a>' );
|
|
259 text = text.replace( urlRegex, '$1<a target="_blank" href="$2">$2</a>' );
|
|
260 return text;
|
19
|
261 }
|
22
|
262
|
|
263 let currentPulldown = null;
|
|
264 let newPulldown = null;
|
|
265
|
|
266 function clickMenu(clicked,display) {
|
95
|
267 //console.log('clickMenu');
|
22
|
268 let pulldown = clicked.parentNode.querySelector('div');
|
|
269 if( pulldown !== currentPulldown ) {
|
95
|
270 pulldown.style.display = display || 'block';
|
22
|
271 newPulldown = pulldown;
|
|
272 window.onclick = function() {
|
95
|
273 //console.log('window.onclick');
|
22
|
274 if( currentPulldown ) {
|
95
|
275 currentPulldown.style.display = 'none';
|
22
|
276 if( !newPulldown )
|
|
277 window.onclick = null;
|
|
278 }
|
|
279 currentPulldown = newPulldown;
|
|
280 newPulldown = null;
|
|
281 };
|
|
282 pulldown.scrollIntoViewIfNeeded(false);
|
|
283 }
|
|
284 }
|
30
|
285
|
42
|
286 function heartbeat() {
|
75
|
287 let url = `heartbeat.js?last_update=${lastUpdate}`;
|
|
288 if( currentChatId )
|
|
289 url += `&chat=${currentChatId}`;
|
|
290 ajax(url);
|
42
|
291 }
|
|
292
|
|
293 setInterval( heartbeat, 10000 );
|
73
|
294 heartbeat();
|
30
|
295
|
33
|
296 let online = {};
|
|
297
|
|
298 function showOnline() {
|
42
|
299 let old = Date.now() - 70000;
|
72
|
300 let spans = document.querySelectorAll('span[online]');
|
|
301 for( let span of spans ) {
|
|
302 let id = span.getAttribute('online');
|
73
|
303 let wasOnline = online[id];
|
|
304 let isOnline = !!wasOnline && wasOnline > old;
|
|
305 span.setAttribute('is_online',isOnline);
|
33
|
306 }
|
|
307 }
|
53
|
308
|
|
309 function clearUnread() {
|
89
|
310 if( spy ) return;
|
53
|
311 let span = document.querySelector(`div[chat="${currentChatId}"] span[unread]`);
|
|
312 span.setAttribute('unread','0');
|
|
313 span.textContent = '0';
|
|
314 }
|
|
315
|
|
316 function incUnread(div) {
|
|
317 let span = div.querySelector('span[unread]');
|
|
318 let n = parseInt(span.getAttribute('unread')) + 1;
|
|
319 span.setAttribute('unread',n);
|
|
320 span.textContent = n;
|
|
321 }
|
56
|
322
|
|
323 function invite() {
|
|
324 let email = document.querySelector('input[type=email]').value;
|
|
325 ajax(`invite.js?email=${encodeURIComponent(email)}`);
|
|
326 }
|
|
327
|
|
328 function openInvite(email) {
|
|
329 let dialog = document.querySelector('dialog[invite]');
|
|
330 let span = dialog.querySelector('span[email]');
|
|
331 span.textContent = email;
|
|
332 openModal(dialog);
|
|
333 }
|
60
|
334
|
|
335 function gotoInvite(el) {
|
|
336 let email = document.querySelector('dialog[invite] span[email]').textContent;
|
|
337 closeModal(el);
|
83
|
338 location = `chat?with=${encodeURIComponent(email)}`;
|
60
|
339 ajax(`save_post.js?post=${currentPostId}`,`content=${encodeURIComponent(text)}`);
|
|
340 }
|
63
|
341
|
|
342 function uploadFile() {
|
|
343 document.querySelector('input[type="file"]').click();
|
|
344 }
|
|
345
|
|
346 function addFileUrl(url) {
|
|
347 let textarea = document.querySelector('div[input] textarea');
|
|
348 let text = textarea.value;
|
|
349 if( /\S$/.test(text) )
|
|
350 text += ' ';
|
|
351 text += url;
|
|
352 textarea.value = text;
|
|
353 }
|
|
354
|
|
355 function uploadToFilebin(fileName,fileContent) {
|
|
356 //console.log(fileContent.byteLength);
|
|
357 let request = new XMLHttpRequest();
|
93
|
358 let url = filebinUrl + encodeURIComponent(fileName);
|
63
|
359 request.open( 'POST', url );
|
|
360 request.onload = function() {
|
|
361 if( request.status !== 201 ) {
|
|
362 let err = 'upload failed: ' + request.status;
|
|
363 if( request.responseText ) {
|
|
364 err += '\n' + request.responseText;
|
|
365 }
|
|
366 console.log(err);
|
|
367 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
|
|
368 return;
|
|
369 }
|
|
370 addFileUrl(url);
|
|
371 };
|
|
372 request.send(fileContent);
|
|
373 }
|
|
374
|
|
375 function loadedFile(input) {
|
|
376 let file = input.files[0];
|
|
377 input.value = null;
|
|
378 console.log(file);
|
|
379 let reader = new FileReader();
|
|
380 reader.onload = function() {
|
|
381 uploadToFilebin(file.name,reader.result);
|
|
382 };
|
|
383 reader.readAsArrayBuffer(file);
|
|
384 }
|
65
|
385
|
|
386 function openPeople() {
|
|
387 let dialog = document.querySelector('dialog[people]');
|
73
|
388 let spans = dialog.querySelectorAll('span[last_seen]');
|
|
389 let now = Date.now();
|
|
390 let old = now - 70000;
|
|
391 for( let span of spans ) {
|
|
392 let id = span.getAttribute('last_seen');
|
|
393 let s;
|
|
394 let lastOnline = online[id];
|
|
395 if( !lastOnline ) {
|
|
396 s = '';
|
|
397 } else if( lastOnline > old ) {
|
|
398 s = 'Active now';
|
|
399 } else {
|
|
400 s = 'Last seen ' + ago(now - lastOnline);
|
|
401 }
|
|
402 console.log(`${id} ${s}`);
|
|
403 span.textContent = s;
|
|
404 }
|
65
|
405 openModal(dialog);
|
|
406 }
|
75
|
407
|
|
408 function readUpTo(userId,userNameHtml,unread) {
|
76
|
409 //console.log(`readUpTo ${unread}`);
|
75
|
410 let divs = document.querySelectorAll('div[post]');
|
|
411 if( unread >= divs.length )
|
|
412 return;
|
76
|
413 let div = divs[divs.length - unread - 1];
|
89
|
414 let old = document.querySelector(`div[up_to][user="${userId}"]`);
|
76
|
415 if( old ) {
|
89
|
416 //console.log(`was ${div.getAttribute('up_to')}`);
|
76
|
417 if( div == old.parentNode )
|
|
418 return;
|
|
419 old.outerHTML = '';
|
|
420 }
|
|
421 //console.log('readUpTo');
|
89
|
422 let html = `<div user="${userId}" up_to="${unread}">read by ${userNameHtml}</div>`;
|
75
|
423 div.insertAdjacentHTML('beforeend',html);
|
81
|
424 if( !old ) {
|
89
|
425 let dy = document.querySelector('div[up_to]').clientHeight;
|
81
|
426 document.querySelector('div[main]').scrollBy(0,dy);
|
|
427 }
|
75
|
428 }
|
79
|
429
|
|
430 function replyPost(el) {
|
|
431 let postId = getPostId(el);
|
|
432 let div = document.querySelector('div[reply]');
|
|
433 div.removeAttribute('hidden');
|
|
434 div.setAttribute('reply',postId);
|
|
435 document.querySelector('div[reply] div[text]').innerHTML = document.querySelector(`div[post="${postId}"] div[text]`).innerHTML
|
|
436 let a = document.querySelector('div[reply] a');
|
|
437 a.href = `#p${postId}`;
|
|
438 a.textContent = document.querySelector(`div[post="${postId}"] span[when]`).textContent;
|
|
439 document.querySelector('div[input] textarea').focus();
|
|
440 document.querySelector('div[input]').scrollIntoView({block: 'end'});
|
|
441 }
|
|
442
|
|
443 function closeReply() {
|
|
444 let div = document.querySelector('div[reply]');
|
|
445 div.setAttribute('hidden','');
|
|
446 div.setAttribute('reply','');
|
|
447 }
|
83
|
448
|
|
449 function openAddToChat() {
|
|
450 let dialog = document.querySelector('dialog[add]');
|
|
451 dialog.querySelector('input[name=email]').value = '';
|
|
452 openModal(dialog);
|
|
453 }
|
|
454
|
|
455 function addToChat() {
|
|
456 let dialog = document.querySelector('dialog[add]');
|
|
457 let email = dialog.querySelector('input[name=email]').value;
|
|
458 ajax(`add_to_chat.js?chat=${currentChatId}&email=${encodeURIComponent(email)}`);
|
|
459 }
|
|
460
|
|
461 function addToChatError(msg) {
|
|
462 let dialog = document.querySelector('dialog[add]');
|
|
463 dialog.querySelector('span[error]').textContent = msg;
|
|
464 }
|