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