annotate src/chat.js @ 61:d4d154b404f8

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 04 Mar 2025 21:29:17 -0700
parents 3521166513b3
children afd5ab5b02a2
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;
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
6 let lastUpdate;
35
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
7 let userId;
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
9 function evalEvent(event) {
17
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
10 // console.log(event);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
11 eval(event.data);
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
12 }
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
13
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
14 function setUserEventSource(userId) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
15 let userEventSource = new EventSource(`${location.origin}/user/${userId}`);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
16 userEventSource.onmessage = evalEvent;
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
17 }
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
18
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 56
diff changeset
19 function selectChat(chatId) {
48
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
20 document.querySelector('div[chat_content]').setAttribute('show','posts');
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 if( chatId === currentChatId )
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 return;
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 56
diff changeset
23 let div = document.querySelector(`div[chat="${chatId}"]`);
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 let selected = div.parentNode.querySelector('[selected]');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 if( selected ) selected.removeAttribute('selected');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 div.setAttribute('selected','');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 ajax(`get_chat.js?chat=${chatId}`);
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 currentChatId = chatId;
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 56
diff changeset
29 history.replaceState(null,null,`?chat=${chatId}`);
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
30 clearUnread();
12
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 if(eventSource) eventSource.close();
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
33 eventSource = new EventSource(`${location.origin}/chat/${chatId}`);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
34 eventSource.onmessage = evalEvent;
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
35 }
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
36
27
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
37 function back() {
48
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
38 document.querySelector('div[chat_content]').setAttribute('show','chats');
27
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
39 }
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
40
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
41 function gotChat(html) {
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
42 document.querySelector('div[posts]').innerHTML = html;
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
43 fixPosts();
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
44 document.querySelector('div[input] textarea').focus();
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
45 document.querySelector('div[input]').scrollIntoView({block: 'end'});
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 }
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 fixTextarea(event) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 let textarea = event.target;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 textarea.style.height = 'initial';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 textarea.style.height = (textarea.scrollHeight+2) + 'px';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 textarea.scrollIntoViewIfNeeded(false);
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 const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
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 addPost() {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 let textarea = document.querySelector('div[input] textarea');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 let text = textarea.value;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 if( text.trim() === '' )
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 return;
24
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
62 ajax(`add_post.js?chat=${currentChatId}`,`content=${encodeURIComponent(text)}`);
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 textarea.value = '';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 function textareaKey(event) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 event.preventDefault();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 addPost();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
73 function fixPosts() {
35
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
74 let divs = document.querySelectorAll('div[post][fix]');
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
75 for( let div of divs ) {
35
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
76 let whenSpan = div.querySelector('span[when]');
39
471b13e6ce2c date formatting
Franklin Schmidt <fschmidt@gmail.com>
parents: 38
diff changeset
77 whenSpan.textContent = new Date(Number(whenSpan.textContent)).toLocaleString([],{dateStyle:'short',timeStyle:'short'});
35
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
78 let textDiv = div.querySelector('div[text]');
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
79 textDiv.innerHTML = urlsToLinks(textDiv.innerHTML);
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
80 if( div.getAttribute('author') === userId )
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
81 div.querySelector('span[pulldown]').innerHTML = document.querySelector('div[hidden] span[pulldown]').innerHTML;
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
82 div.removeAttribute('fix');
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
83 }
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 }
11
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
85
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
86 function deleteChat() {
20
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
87 let dialog = document.querySelector('dialog[delete_chat]');
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
88 openModal(dialog);
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
89 }
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
90
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
91 function doDeleteChat(el) {
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
92 closeModal(el);
11
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
93 ajax(`delete_chat.js?chat=${currentChatId}`);
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
94 }
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
95
23
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
96 let currentPostId;
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
97
38
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
98 function getPostId(el) {
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
99 while(true) {
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
100 let postId = el.getAttribute('post');
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
101 if( postId )
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
102 return postId;
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
103 el = el.parentNode;
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
104 }
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
105 }
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
106
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
107 function deletePost(el) {
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
108 currentPostId = getPostId(el);
23
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
109 let dialog = document.querySelector('dialog[delete_post]');
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
110 openModal(dialog);
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
111 }
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
112
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
113 function doDeletePost(el) {
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
114 closeModal(el);
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
115 ajax(`delete_post.js?post=${currentPostId}`);
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
116 }
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
117
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
118 function deleted(postId) {
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
119 let div = document.querySelector(`div[post="${postId}"]`);
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
120 if( div )
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
121 div.outerHTML = '';
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
122 }
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
123
38
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
124 function editPost(el) {
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
125 ajax(`edit_post.js?post=${getPostId(el)}`);
24
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
126 }
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
127
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
128 function openEditPost(postId,text) {
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
129 currentPostId = postId;
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
130 let dialog = document.querySelector('dialog[edit_post]');
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
131 let textarea = dialog.querySelector('textarea');
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
132 textarea.value = text;
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
133 openModal(dialog);
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
134 }
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
135
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
136 function savePost(el) {
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
137 let text = document.querySelector('dialog[edit_post] textarea').value;
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
138 closeModal(el);
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
139 ajax(`save_post.js?post=${currentPostId}`,`content=${encodeURIComponent(text)}`);
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
140 }
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
141
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
142 function edited(postId,html) {
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
143 let div = document.querySelector(`div[post="${postId}"]`);
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
144 if( div ) {
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
145 div.outerHTML = html;
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
146 fixPosts();
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
147 }
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
148 }
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
149
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
150 function added(html) {
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
151 let input = document.querySelector('div[input]');
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
152 input.insertAdjacentHTML('beforebegin',html);
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
153 fixPosts();
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
154 input.scrollIntoView({block: 'end'});
54
1d724f187cff minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
155 ajax(`added.js?chat=${currentChatId}`);
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 39
diff changeset
156 if( document.hasFocus() )
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 39
diff changeset
157 ajax('active.js');
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
158 }
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
159
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
160 function getChats(chatId,updated) {
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
161 let first = document.querySelector('div[chat]');
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
162 if( !first || first.getAttribute('chat') !== chatId ) {
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
163 // console.log('getChats');
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
164 ajax('get_chats.js');
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
165 } else if( first && currentChatId !== chatId ) {
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
166 incUnread(first);
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
167 }
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
168 if( updated )
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
169 lastUpdate = updated;
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 39
diff changeset
170 if( !document.hasFocus() ) {
31
a0820965ba04 notify sound
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
171 document.title = title + ' *';
a0820965ba04 notify sound
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
172 }
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
173 }
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
174
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
175 function gotChats(html) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
176 document.querySelector('div[chats]').innerHTML = html;
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
177 if( currentChatId ) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
178 let current = document.querySelector(`div[chat="${currentChatId}"]`);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
179 if( current ) {
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
180 current.setAttribute('selected','');
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
181 current.scrollIntoViewIfNeeded(false);
61
Franklin Schmidt <fschmidt@gmail.com>
parents: 60
diff changeset
182 clearUnread();
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
183 } else {
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
184 currentChatId = null;
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
185 document.querySelector('div[posts]').innerHTML = '';
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
186 }
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
187 }
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
188 }
18
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
189
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
190 window.onfocus = function() {
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
191 // console.log('onfocus');
18
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
192 document.title = title;
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 39
diff changeset
193 ajax('active.js');
18
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
194 };
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
195
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
196 let urlRegex = /(^|\s)(https?:\/\/\S+)/g;
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
197
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
198 function urlsToLinks(text) {
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 39
diff changeset
199 return text.replace( urlRegex, '$1<a target="_blank" href="$2">$2</a>' );
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
200 }
22
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
201
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
202 let currentPulldown = null;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
203 let newPulldown = null;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
204
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
205 function clickMenu(clicked,display) {
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
206 //console.log("clickMenu");
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
207 let pulldown = clicked.parentNode.querySelector('div');
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
208 if( pulldown !== currentPulldown ) {
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
209 pulldown.style.display = display || "block";
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
210 newPulldown = pulldown;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
211 window.onclick = function() {
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
212 //console.log("window.onclick");
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
213 if( currentPulldown ) {
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
214 currentPulldown.style.display = "none";
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
215 if( !newPulldown )
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
216 window.onclick = null;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
217 }
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
218 currentPulldown = newPulldown;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
219 newPulldown = null;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
220 };
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
221 pulldown.scrollIntoViewIfNeeded(false);
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
222 }
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
223 }
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
224
42
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
225 function heartbeat() {
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
226 showOnline();
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
227 ajax(`heartbeat.js?last_update=${lastUpdate}`);
42
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
228 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
229
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
230 setInterval( heartbeat, 10000 );
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
231
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
232 let online = {};
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
233
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
234 function setOnline(userId) {
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
235 online[userId] = Date.now();
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
236 }
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
237
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
238 function showOnline() {
42
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
239 let old = Date.now() - 70000;
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
240 for( let id of Object.keys(online) ) {
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
241 if( online[id] < old )
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
242 delete online[id];
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
243 }
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
244 let a = [];
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
245 for( let id in online ) {
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
246 a.push( `span[online="${id}"]` );
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
247 }
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
248 let style = document.querySelector('style[online]');
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
249 if( a.length === 0 ) {
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
250 style.innerHTML = '';
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
251 } else {
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
252 style.innerHTML = `
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
253 ${a.join(', ')} {
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
254 background-color: green;
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
255 }
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
256 ` ;
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
257 }
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
258 }
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
259
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
260 function clearUnread() {
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
261 let span = document.querySelector(`div[chat="${currentChatId}"] span[unread]`);
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
262 span.setAttribute('unread','0');
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
263 span.textContent = '0';
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
264 }
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
265
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
266 function incUnread(div) {
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
267 let span = div.querySelector('span[unread]');
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
268 let n = parseInt(span.getAttribute('unread')) + 1;
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
269 span.setAttribute('unread',n);
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
270 span.textContent = n;
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
271 }
56
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
272
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
273 function invite() {
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
274 let email = document.querySelector('input[type=email]').value;
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
275 ajax(`invite.js?email=${encodeURIComponent(email)}`);
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
276 }
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
277
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
278 function openInvite(email) {
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
279 let dialog = document.querySelector('dialog[invite]');
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
280 let span = dialog.querySelector('span[email]');
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
281 span.textContent = email;
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
282 openModal(dialog);
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
283 }
60
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
284
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
285 function gotoInvite(el) {
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
286 let email = document.querySelector('dialog[invite] span[email]').textContent;
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
287 closeModal(el);
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
288 location = `chat?with=${email}`;
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
289 ajax(`save_post.js?post=${currentPostId}`,`content=${encodeURIComponent(text)}`);
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
290 }