Mercurial Hosting > chat
comparison src/chat.js @ 12:9f45d32670ae
server push
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 31 Oct 2024 21:40:57 -0600 |
| parents | 563a5358f2ee |
| children | 0df3a63a895f |
comparison
equal
deleted
inserted
replaced
| 11:563a5358f2ee | 12:9f45d32670ae |
|---|---|
| 1 'use strict'; | 1 'use strict'; |
| 2 | 2 |
| 3 let currentChatId = null; | 3 let currentChatId = null; |
| 4 let eventSource; | |
| 4 | 5 |
| 5 function selectChat(div) { | 6 function selectChat(div) { |
| 6 let chatId = div.getAttribute('chat'); | 7 let chatId = div.getAttribute('chat'); |
| 7 if( chatId === currentChatId ) | 8 if( chatId === currentChatId ) |
| 8 return; | 9 return; |
| 9 let selected = div.parentNode.querySelector('[selected]'); | 10 let selected = div.parentNode.querySelector('[selected]'); |
| 10 if( selected ) selected.removeAttribute('selected'); | 11 if( selected ) selected.removeAttribute('selected'); |
| 11 div.setAttribute('selected',''); | 12 div.setAttribute('selected',''); |
| 12 ajax(`get_chat.js?chat=${chatId}`); | 13 ajax(`get_chat.js?chat=${chatId}`); |
| 13 currentChatId = chatId; | 14 currentChatId = chatId; |
| 15 | |
| 16 if(eventSource) eventSource.close(); | |
| 17 eventSource = new EventSource(`${location.origin}/chat/${chatId}`); | |
| 18 eventSource.onmessage = function(event) { | |
| 19 eval(event.data); | |
| 20 }; | |
| 21 } | |
| 22 | |
| 23 function gotChat(html) { | |
| 24 document.querySelector('div[posts]').innerHTML = html; | |
| 25 fixDates(); | |
| 26 document.querySelector('div[input] textarea').focus(); | |
| 27 document.querySelector('div[input]').scrollIntoView({block: 'end'}); | |
| 14 } | 28 } |
| 15 | 29 |
| 16 function fixTextarea(event) { | 30 function fixTextarea(event) { |
| 17 let textarea = event.target; | 31 let textarea = event.target; |
| 18 textarea.style.height = 'initial'; | 32 textarea.style.height = 'initial'; |
| 27 let text = textarea.value; | 41 let text = textarea.value; |
| 28 if( text.trim() === '' ) | 42 if( text.trim() === '' ) |
| 29 return; | 43 return; |
| 30 ajax(`add_post.js?chat=${currentChatId}&content=${encodeURIComponent(text)}`); | 44 ajax(`add_post.js?chat=${currentChatId}&content=${encodeURIComponent(text)}`); |
| 31 textarea.value = ''; | 45 textarea.value = ''; |
| 32 console.log('addPost'); | |
| 33 } | 46 } |
| 34 | 47 |
| 35 function textareaKey(event) { | 48 function textareaKey(event) { |
| 36 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) { | 49 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) { |
| 37 event.preventDefault(); | 50 event.preventDefault(); |
| 48 } | 61 } |
| 49 | 62 |
| 50 function deleteChat() { | 63 function deleteChat() { |
| 51 ajax(`delete_chat.js?chat=${currentChatId}`); | 64 ajax(`delete_chat.js?chat=${currentChatId}`); |
| 52 } | 65 } |
| 66 | |
| 67 function added(html) { | |
| 68 let input = document.querySelector('div[input]'); | |
| 69 input.insertAdjacentHTML('beforebegin',html); | |
| 70 input.scrollIntoView({block: 'end'}); | |
| 71 } |
