annotate src/chat.js @ 16:82b55186a4a0

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