annotate src/chat.js @ 35:27c41f22d2a9

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