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