Mercurial Hosting > chat
comparison src/chat.js @ 16:82b55186a4a0
fix delete_chat
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 03 Nov 2024 21:22:06 -0700 |
| parents | 8b8905f63d80 |
| children | 7230c821c368 |
comparison
equal
deleted
inserted
replaced
| 15:8b8905f63d80 | 16:82b55186a4a0 |
|---|---|
| 1 'use strict'; | 1 'use strict'; |
| 2 | 2 |
| 3 let currentChatId = null; | 3 let currentChatId = null; |
| 4 let eventSource; | 4 let eventSource; |
| 5 | 5 |
| 6 function evalEvent(event) { | |
| 7 eval(event.data); | |
| 8 } | |
| 9 | |
| 6 function setUserEventSource(userId) { | 10 function setUserEventSource(userId) { |
| 7 let userEventSource = new EventSource(`${location.origin}/user/${userId}`); | 11 let userEventSource = new EventSource(`${location.origin}/user/${userId}`); |
| 8 userEventSource.onmessage = function(event) { | 12 userEventSource.onmessage = evalEvent; |
| 9 eval(event.data); | |
| 10 }; | |
| 11 } | 13 } |
| 12 | 14 |
| 13 function selectChat(div) { | 15 function selectChat(div) { |
| 14 let chatId = div.getAttribute('chat'); | 16 let chatId = div.getAttribute('chat'); |
| 15 if( chatId === currentChatId ) | 17 if( chatId === currentChatId ) |
| 20 ajax(`get_chat.js?chat=${chatId}`); | 22 ajax(`get_chat.js?chat=${chatId}`); |
| 21 currentChatId = chatId; | 23 currentChatId = chatId; |
| 22 | 24 |
| 23 if(eventSource) eventSource.close(); | 25 if(eventSource) eventSource.close(); |
| 24 eventSource = new EventSource(`${location.origin}/chat/${chatId}`); | 26 eventSource = new EventSource(`${location.origin}/chat/${chatId}`); |
| 25 eventSource.onmessage = function(event) { | 27 eventSource.onmessage = evalEvent; |
| 26 eval(event.data); | |
| 27 }; | |
| 28 } | 28 } |
| 29 | 29 |
| 30 function gotChat(html) { | 30 function gotChat(html) { |
| 31 document.querySelector('div[posts]').innerHTML = html; | 31 document.querySelector('div[posts]').innerHTML = html; |
| 32 fixDates(); | 32 fixDates(); |
| 88 | 88 |
| 89 function gotChats(html) { | 89 function gotChats(html) { |
| 90 document.querySelector('div[chats]').innerHTML = html; | 90 document.querySelector('div[chats]').innerHTML = html; |
| 91 if( currentChatId ) { | 91 if( currentChatId ) { |
| 92 let current = document.querySelector(`div[chat="${currentChatId}"]`); | 92 let current = document.querySelector(`div[chat="${currentChatId}"]`); |
| 93 current.setAttribute('selected',''); | 93 if( current ) { |
| 94 current.scrollIntoViewIfNeeded(false); | 94 current.setAttribute('selected',''); |
| 95 current.scrollIntoViewIfNeeded(false); | |
| 96 } else { | |
| 97 currentChatId = null; | |
| 98 document.querySelector('div[posts]').innerHTML = ''; | |
| 99 } | |
| 95 } | 100 } |
| 96 } | 101 } |
