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