1
1
import { GET } from '../modules/fetch.ts' ;
2
- import { toggleElem , type DOMEvent , createElementFromHTML } from '../utils/dom.ts' ;
2
+ import { toggleElem , createElementFromHTML } from '../utils/dom.ts' ;
3
3
import { logoutFromWorker } from '../modules/worker.ts' ;
4
4
5
5
const { appSubUrl, notificationSettings, assetVersionEncoded} = window . config ;
6
6
let notificationSequenceNumber = 0 ;
7
7
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
-
34
8
async function receiveUpdateCount ( event : MessageEvent ) {
35
9
try {
36
10
const data = event . data ;
@@ -143,11 +117,11 @@ async function updateNotificationCountWithCallback(callback: (timeout: number, n
143
117
}
144
118
145
119
async function updateNotificationTable ( ) {
146
- const notificationDiv = document . querySelector ( '#notification_div' ) ;
120
+ let notificationDiv = document . querySelector ( '#notification_div' ) ;
147
121
if ( notificationDiv ) {
148
122
try {
149
123
const params = new URLSearchParams ( window . location . search ) ;
150
- params . set ( 'div-only' , String ( true ) ) ;
124
+ params . set ( 'div-only' , ' true' ) ;
151
125
params . set ( 'sequence-number' , String ( ++ notificationSequenceNumber ) ) ;
152
126
const response = await GET ( `${ appSubUrl } /notifications?${ params . toString ( ) } ` ) ;
153
127
@@ -159,7 +133,8 @@ async function updateNotificationTable() {
159
133
const el = createElementFromHTML ( data ) ;
160
134
if ( parseInt ( el . getAttribute ( 'data-sequence-number' ) ) === notificationSequenceNumber ) {
161
135
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
163
138
}
164
139
} catch ( error ) {
165
140
console . error ( error ) ;
0 commit comments