Mercurial Hosting > chat
diff src/chat.js @ 53:9298b04607ae
add unread
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 03 Mar 2025 19:39:30 -0700 |
parents | 7628fd0e3560 |
children | 1d724f187cff |
line wrap: on
line diff
--- a/src/chat.js Mon Mar 03 12:29:06 2025 -0700 +++ b/src/chat.js Mon Mar 03 19:39:30 2025 -0700 @@ -27,6 +27,7 @@ ajax(`get_chat.js?chat=${chatId}`); currentChatId = chatId; history.replaceState(null,null,`?with=${email}`); + clearUnread(); if(eventSource) eventSource.close(); eventSource = new EventSource(`${location.origin}/chat/${chatId}`); @@ -157,9 +158,11 @@ function getChats(chatId,updated) { let first = document.querySelector('div[chat]'); - if( !first || first.getAttribute('chat') != chatId ) { + if( !first || first.getAttribute('chat') !== chatId ) { // console.log('getChats'); ajax('get_chats.js'); + } else if( first && currentChatId !== chatId ) { + incUnread(first); } if( updated ) lastUpdate = updated; @@ -251,3 +254,16 @@ ` ; } } + +function clearUnread() { + let span = document.querySelector(`div[chat="${currentChatId}"] span[unread]`); + span.setAttribute('unread','0'); + span.textContent = '0'; +} + +function incUnread(div) { + let span = div.querySelector('span[unread]'); + let n = parseInt(span.getAttribute('unread')) + 1; + span.setAttribute('unread',n); + span.textContent = n; +}