annotate src/chat.js @ 11:563a5358f2ee

add delete_chat
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 31 Oct 2024 19:17:53 -0600
parents f9e6a4cc4f7d
children 9f45d32670ae
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;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 function selectChat(div) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 let chatId = div.getAttribute('chat');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 if( chatId === currentChatId )
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 return;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 let selected = div.parentNode.querySelector('[selected]');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 if( selected ) selected.removeAttribute('selected');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 div.setAttribute('selected','');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 ajax(`get_chat.js?chat=${chatId}`);
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 currentChatId = chatId;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 function fixTextarea(event) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 let textarea = event.target;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 textarea.style.height = 'initial';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 textarea.style.height = (textarea.scrollHeight+2) + 'px';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 textarea.scrollIntoViewIfNeeded(false);
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 function addPost() {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 let textarea = document.querySelector('div[input] textarea');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 let text = textarea.value;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 if( text.trim() === '' )
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 return;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 ajax(`add_post.js?chat=${currentChatId}&content=${encodeURIComponent(text)}`);
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 textarea.value = '';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 console.log('addPost');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 function textareaKey(event) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 event.preventDefault();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 addPost();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 function fixDates() {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 let spans = document.querySelectorAll('span[when][fix]');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 for( let span of spans ) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 span.textContent = new Date(Number(span.textContent)).toLocaleString();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 span.removeAttribute('fix');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 }
11
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
49
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
50 function deleteChat() {
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
51 ajax(`delete_chat.js?chat=${currentChatId}`);
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
52 }