|
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]');
|
|
118
|
95 textDiv.innerHTML = handleMarkdown(textDiv.innerHTML);
|
|
79
|
96 let reply = div.querySelector('blockquote');
|
|
|
97 if( reply )
|
|
118
|
98 reply.innerHTML = handleMarkdown(reply.innerHTML);
|
|
79
|
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
|
|
118
|
254 let converter = window.markdownit({linkify:true});
|
|
|
255 let filebinUrlRegex = />https:\/\/filebin.net\/[0-9a-f]+\/([^<]+)</g;
|
|
121
|
256 let urlRegex = /(<a href="https?:\/\/[^" ]+")>/g;
|
|
19
|
257
|
|
118
|
258 function handleMarkdown(text) {
|
|
120
|
259 text = converter.renderInline(text);
|
|
118
|
260 text = text.replace( filebinUrlRegex, '>$1<' );
|
|
121
|
261 text = text.replace( urlRegex, '$1 target="_blank">' );
|
|
66
|
262 return text;
|
|
19
|
263 }
|
|
22
|
264
|
|
|
265 let currentPulldown = null;
|
|
|
266 let newPulldown = null;
|
|
|
267
|
|
|
268 function clickMenu(clicked,display) {
|
|
95
|
269 //console.log('clickMenu');
|
|
22
|
270 let pulldown = clicked.parentNode.querySelector('div');
|
|
|
271 if( pulldown !== currentPulldown ) {
|
|
95
|
272 pulldown.style.display = display || 'block';
|
|
22
|
273 newPulldown = pulldown;
|
|
|
274 window.onclick = function() {
|
|
95
|
275 //console.log('window.onclick');
|
|
22
|
276 if( currentPulldown ) {
|
|
95
|
277 currentPulldown.style.display = 'none';
|
|
22
|
278 if( !newPulldown )
|
|
|
279 window.onclick = null;
|
|
|
280 }
|
|
|
281 currentPulldown = newPulldown;
|
|
|
282 newPulldown = null;
|
|
|
283 };
|
|
|
284 pulldown.scrollIntoViewIfNeeded(false);
|
|
|
285 }
|
|
|
286 }
|
|
30
|
287
|
|
42
|
288 function heartbeat() {
|
|
111
|
289 let url = `heartbeat.js?last_update=${lastUpdate}&focus=${document.hasFocus()}`;
|
|
75
|
290 if( currentChatId )
|
|
|
291 url += `&chat=${currentChatId}`;
|
|
|
292 ajax(url);
|
|
42
|
293 }
|
|
|
294
|
|
|
295 setInterval( heartbeat, 10000 );
|
|
73
|
296 heartbeat();
|
|
30
|
297
|
|
33
|
298 let online = {};
|
|
|
299
|
|
|
300 function showOnline() {
|
|
42
|
301 let old = Date.now() - 70000;
|
|
72
|
302 let spans = document.querySelectorAll('span[online]');
|
|
|
303 for( let span of spans ) {
|
|
|
304 let id = span.getAttribute('online');
|
|
73
|
305 let wasOnline = online[id];
|
|
|
306 let isOnline = !!wasOnline && wasOnline > old;
|
|
|
307 span.setAttribute('is_online',isOnline);
|
|
33
|
308 }
|
|
|
309 }
|
|
53
|
310
|
|
|
311 function clearUnread() {
|
|
89
|
312 if( spy ) return;
|
|
53
|
313 let span = document.querySelector(`div[chat="${currentChatId}"] span[unread]`);
|
|
|
314 span.setAttribute('unread','0');
|
|
|
315 span.textContent = '0';
|
|
|
316 }
|
|
|
317
|
|
|
318 function incUnread(div) {
|
|
|
319 let span = div.querySelector('span[unread]');
|
|
|
320 let n = parseInt(span.getAttribute('unread')) + 1;
|
|
|
321 span.setAttribute('unread',n);
|
|
|
322 span.textContent = n;
|
|
|
323 }
|
|
56
|
324
|
|
|
325 function invite() {
|
|
|
326 let email = document.querySelector('input[type=email]').value;
|
|
|
327 ajax(`invite.js?email=${encodeURIComponent(email)}`);
|
|
|
328 }
|
|
|
329
|
|
|
330 function openInvite(email) {
|
|
|
331 let dialog = document.querySelector('dialog[invite]');
|
|
|
332 let span = dialog.querySelector('span[email]');
|
|
|
333 span.textContent = email;
|
|
|
334 openModal(dialog);
|
|
|
335 }
|
|
60
|
336
|
|
|
337 function gotoInvite(el) {
|
|
|
338 let email = document.querySelector('dialog[invite] span[email]').textContent;
|
|
|
339 closeModal(el);
|
|
83
|
340 location = `chat?with=${encodeURIComponent(email)}`;
|
|
60
|
341 ajax(`save_post.js?post=${currentPostId}`,`content=${encodeURIComponent(text)}`);
|
|
|
342 }
|
|
63
|
343
|
|
|
344 function uploadFile() {
|
|
|
345 document.querySelector('input[type="file"]').click();
|
|
|
346 }
|
|
|
347
|
|
|
348 function addFileUrl(url) {
|
|
|
349 let textarea = document.querySelector('div[input] textarea');
|
|
|
350 let text = textarea.value;
|
|
|
351 if( /\S$/.test(text) )
|
|
|
352 text += ' ';
|
|
|
353 text += url;
|
|
|
354 textarea.value = text;
|
|
|
355 }
|
|
|
356
|
|
|
357 function uploadToFilebin(fileName,fileContent) {
|
|
|
358 //console.log(fileContent.byteLength);
|
|
|
359 let request = new XMLHttpRequest();
|
|
93
|
360 let url = filebinUrl + encodeURIComponent(fileName);
|
|
63
|
361 request.open( 'POST', url );
|
|
|
362 request.onload = function() {
|
|
|
363 if( request.status !== 201 ) {
|
|
|
364 let err = 'upload failed: ' + request.status;
|
|
|
365 if( request.responseText ) {
|
|
|
366 err += '\n' + request.responseText;
|
|
|
367 }
|
|
|
368 console.log(err);
|
|
|
369 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
|
|
|
370 return;
|
|
|
371 }
|
|
|
372 addFileUrl(url);
|
|
|
373 };
|
|
|
374 request.send(fileContent);
|
|
|
375 }
|
|
|
376
|
|
|
377 function loadedFile(input) {
|
|
|
378 let file = input.files[0];
|
|
|
379 input.value = null;
|
|
|
380 console.log(file);
|
|
|
381 let reader = new FileReader();
|
|
|
382 reader.onload = function() {
|
|
|
383 uploadToFilebin(file.name,reader.result);
|
|
|
384 };
|
|
|
385 reader.readAsArrayBuffer(file);
|
|
|
386 }
|
|
65
|
387
|
|
|
388 function openPeople() {
|
|
|
389 let dialog = document.querySelector('dialog[people]');
|
|
73
|
390 let spans = dialog.querySelectorAll('span[last_seen]');
|
|
|
391 let now = Date.now();
|
|
|
392 let old = now - 70000;
|
|
|
393 for( let span of spans ) {
|
|
|
394 let id = span.getAttribute('last_seen');
|
|
|
395 let s;
|
|
|
396 let lastOnline = online[id];
|
|
|
397 if( !lastOnline ) {
|
|
|
398 s = '';
|
|
|
399 } else if( lastOnline > old ) {
|
|
|
400 s = 'Active now';
|
|
|
401 } else {
|
|
|
402 s = 'Last seen ' + ago(now - lastOnline);
|
|
|
403 }
|
|
|
404 console.log(`${id} ${s}`);
|
|
|
405 span.textContent = s;
|
|
|
406 }
|
|
65
|
407 openModal(dialog);
|
|
|
408 }
|
|
75
|
409
|
|
|
410 function readUpTo(userId,userNameHtml,unread) {
|
|
76
|
411 //console.log(`readUpTo ${unread}`);
|
|
75
|
412 let divs = document.querySelectorAll('div[post]');
|
|
|
413 if( unread >= divs.length )
|
|
|
414 return;
|
|
76
|
415 let div = divs[divs.length - unread - 1];
|
|
89
|
416 let old = document.querySelector(`div[up_to][user="${userId}"]`);
|
|
76
|
417 if( old ) {
|
|
89
|
418 //console.log(`was ${div.getAttribute('up_to')}`);
|
|
76
|
419 if( div == old.parentNode )
|
|
|
420 return;
|
|
|
421 old.outerHTML = '';
|
|
|
422 }
|
|
|
423 //console.log('readUpTo');
|
|
89
|
424 let html = `<div user="${userId}" up_to="${unread}">read by ${userNameHtml}</div>`;
|
|
75
|
425 div.insertAdjacentHTML('beforeend',html);
|
|
81
|
426 if( !old ) {
|
|
89
|
427 let dy = document.querySelector('div[up_to]').clientHeight;
|
|
81
|
428 document.querySelector('div[main]').scrollBy(0,dy);
|
|
|
429 }
|
|
75
|
430 }
|
|
79
|
431
|
|
|
432 function replyPost(el) {
|
|
|
433 let postId = getPostId(el);
|
|
|
434 let div = document.querySelector('div[reply]');
|
|
|
435 div.removeAttribute('hidden');
|
|
|
436 div.setAttribute('reply',postId);
|
|
112
|
437 document.querySelector('div[reply] div[text]').innerHTML = document.querySelector(`div[post="${postId}"] div[text]`).innerHTML;
|
|
109
|
438 let a = document.querySelector('div[reply] a[when]');
|
|
79
|
439 a.href = `#p${postId}`;
|
|
|
440 a.textContent = document.querySelector(`div[post="${postId}"] span[when]`).textContent;
|
|
|
441 document.querySelector('div[input] textarea').focus();
|
|
|
442 document.querySelector('div[input]').scrollIntoView({block: 'end'});
|
|
|
443 }
|
|
|
444
|
|
|
445 function closeReply() {
|
|
|
446 let div = document.querySelector('div[reply]');
|
|
|
447 div.setAttribute('hidden','');
|
|
|
448 div.setAttribute('reply','');
|
|
|
449 }
|
|
83
|
450
|
|
|
451 function openAddToChat() {
|
|
|
452 let dialog = document.querySelector('dialog[add]');
|
|
|
453 dialog.querySelector('input[name=email]').value = '';
|
|
|
454 openModal(dialog);
|
|
|
455 }
|
|
|
456
|
|
|
457 function addToChat() {
|
|
|
458 let dialog = document.querySelector('dialog[add]');
|
|
|
459 let email = dialog.querySelector('input[name=email]').value;
|
|
|
460 ajax(`add_to_chat.js?chat=${currentChatId}&email=${encodeURIComponent(email)}`);
|
|
|
461 }
|
|
|
462
|
|
|
463 function addToChatError(msg) {
|
|
|
464 let dialog = document.querySelector('dialog[add]');
|
|
|
465 dialog.querySelector('span[error]').textContent = msg;
|
|
|
466 }
|
|
112
|
467
|
|
|
468 function selectPost(el) {
|
|
|
469 let postId = getPostId(el);
|
|
|
470 let div = document.querySelector(`div[post="${postId}"] div[text]`);
|
|
|
471 let range = document.createRange();
|
|
|
472 range.selectNodeContents(div);
|
|
|
473 let selection = window.getSelection();
|
|
|
474 selection.removeAllRanges();
|
|
|
475 selection.addRange(range);
|
|
|
476 }
|