11import { GET } from '../modules/fetch.ts' ;
2- import { toggleElem , type DOMEvent , createElementFromHTML } from '../utils/dom.ts' ;
2+ import { toggleElem , createElementFromHTML } from '../utils/dom.ts' ;
33import { logoutFromWorker } from '../modules/worker.ts' ;
44
55const { appSubUrl, notificationSettings, assetVersionEncoded} = window . config ;
66let notificationSequenceNumber = 0 ;
77
8- export function initNotificationsTable ( ) {
9- const table = document . querySelector ( '#notification_table' ) ;
10- if ( ! table ) return ;
11-
12- // when page restores from bfcache, delete previously clicked items
13- window . addEventListener ( 'pageshow' , ( e ) => {
14- if ( e . persisted ) { // page was restored from bfcache
15- const table = document . querySelector ( '#notification_table' ) ;
16- const unreadCountEl = document . querySelector < HTMLElement > ( '.notifications-unread-count' ) ;
17- let unreadCount = parseInt ( unreadCountEl . textContent ) ;
18- for ( const item of table . querySelectorAll ( '.notifications-item[data-remove="true"]' ) ) {
19- item . remove ( ) ;
20- unreadCount -= 1 ;
21- }
22- unreadCountEl . textContent = String ( unreadCount ) ;
23- }
24- } ) ;
25-
26- // mark clicked unread links for deletion on bfcache restore
27- for ( const link of table . querySelectorAll < HTMLAnchorElement > ( '.notifications-item[data-status="1"] .notifications-link' ) ) {
28- link . addEventListener ( 'click' , ( e : DOMEvent < MouseEvent > ) => {
29- e . target . closest ( '.notifications-item' ) . setAttribute ( 'data-remove' , 'true' ) ;
30- } ) ;
31- }
32- }
33-
348async function receiveUpdateCount ( event : MessageEvent ) {
359 try {
3610 const data = event . data ;
@@ -143,11 +117,11 @@ async function updateNotificationCountWithCallback(callback: (timeout: number, n
143117}
144118
145119async function updateNotificationTable ( ) {
146- const notificationDiv = document . querySelector ( '#notification_div' ) ;
120+ let notificationDiv = document . querySelector ( '#notification_div' ) ;
147121 if ( notificationDiv ) {
148122 try {
149123 const params = new URLSearchParams ( window . location . search ) ;
150- params . set ( 'div-only' , String ( true ) ) ;
124+ params . set ( 'div-only' , ' true' ) ;
151125 params . set ( 'sequence-number' , String ( ++ notificationSequenceNumber ) ) ;
152126 const response = await GET ( `${ appSubUrl } /notifications?${ params . toString ( ) } ` ) ;
153127
@@ -159,7 +133,8 @@ async function updateNotificationTable() {
159133 const el = createElementFromHTML ( data ) ;
160134 if ( parseInt ( el . getAttribute ( 'data-sequence-number' ) ) === notificationSequenceNumber ) {
161135 notificationDiv . outerHTML = data ;
162- initNotificationsTable ( ) ;
136+ notificationDiv = document . querySelector ( '#notification_div' ) ;
137+ window . htmx . process ( notificationDiv ) ; // when using htmx, we must always remember to process the new content changed by us
163138 }
164139 } catch ( error ) {
165140 console . error ( error ) ;
0 commit comments