annotate src/chat.js @ 20:dade6a560494

add dialogs
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 Nov 2024 21:37:58 -0700
parents 435f474f07c7
children 949914955bfe
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
18
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
3 let title = document.title;
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 let currentChatId = null;
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
5 let eventSource;
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
7 function evalEvent(event) {
17
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
8 // console.log(event);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
9 eval(event.data);
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
10 }
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
11
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
12 function setUserEventSource(userId) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
13 let userEventSource = new EventSource(`${location.origin}/user/${userId}`);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
14 userEventSource.onmessage = evalEvent;
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
15 }
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
16
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 function selectChat(div) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 let chatId = div.getAttribute('chat');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 if( chatId === currentChatId )
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 return;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 let selected = div.parentNode.querySelector('[selected]');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 if( selected ) selected.removeAttribute('selected');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 div.setAttribute('selected','');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 ajax(`get_chat.js?chat=${chatId}`);
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 currentChatId = chatId;
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
26
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
27 if(eventSource) eventSource.close();
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
28 eventSource = new EventSource(`${location.origin}/chat/${chatId}`);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
29 eventSource.onmessage = evalEvent;
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
30 }
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
31
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
32 function gotChat(html) {
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
33 document.querySelector('div[posts]').innerHTML = html;
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
34 fixPosts();
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
35 document.querySelector('div[input] textarea').focus();
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
36 document.querySelector('div[input]').scrollIntoView({block: 'end'});
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 function fixTextarea(event) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 let textarea = event.target;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 textarea.style.height = 'initial';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 textarea.style.height = (textarea.scrollHeight+2) + 'px';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 textarea.scrollIntoViewIfNeeded(false);
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 function addPost() {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 let textarea = document.querySelector('div[input] textarea');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 let text = textarea.value;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 if( text.trim() === '' )
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 return;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 ajax(`add_post.js?chat=${currentChatId}&content=${encodeURIComponent(text)}`);
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 textarea.value = '';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 function textareaKey(event) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 event.preventDefault();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 addPost();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
64 function fixPosts() {
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 let spans = document.querySelectorAll('span[when][fix]');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 for( let span of spans ) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 span.textContent = new Date(Number(span.textContent)).toLocaleString();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 span.removeAttribute('fix');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 }
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
70 let divs = document.querySelectorAll('div[text][fix]');
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
71 for( let div of divs ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
72 div.innerHTML = urlsToLinks(div.innerHTML);
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
73 div.removeAttribute('fix');
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
74 }
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 }
11
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
76
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
77 function deleteChat() {
20
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
78 let dialog = document.querySelector('dialog[delete_chat]');
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
79 openModal(dialog);
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
80 }
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
81
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
82 function doDeleteChat(el) {
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
83 closeModal(el);
11
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
84 ajax(`delete_chat.js?chat=${currentChatId}`);
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
85 }
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
86
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
87 function added(html) {
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
88 let input = document.querySelector('div[input]');
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
89 input.insertAdjacentHTML('beforebegin',html);
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
90 fixPosts();
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
91 input.scrollIntoView({block: 'end'});
18
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
92 if( !document.hasFocus() )
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
93 document.title = title + ' *';
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
94 }
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
95
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
96 function getChats(chatId) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
97 let first = document.querySelector('div[chat]');
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
98 if( !first || first.getAttribute('chat') != chatId ) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
99 // console.log('getChats');
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
100 ajax('get_chats.js');
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
101 }
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
102 }
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
103
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
104 function gotChats(html) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
105 document.querySelector('div[chats]').innerHTML = html;
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
106 if( currentChatId ) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
107 let current = document.querySelector(`div[chat="${currentChatId}"]`);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
108 if( current ) {
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
109 current.setAttribute('selected','');
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
110 current.scrollIntoViewIfNeeded(false);
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
111 } else {
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
112 currentChatId = null;
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
113 document.querySelector('div[posts]').innerHTML = '';
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
114 }
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
115 }
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
116 }
18
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
117
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
118 window.onfocus = function() {
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
119 document.title = title;
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
120 };
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
121
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
122 let urlRegex = /(^|\s)(https?:\/\/\S+)/g;
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
123
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
124 function urlsToLinks(text) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
125 return text.replace( urlRegex, '$1<a href="$2">$2</a>' );
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
126 }