diff --git a/sib-badge.js b/sib-badge.js index 8255ef350275776f9f9fa6f1547fbf2fadb6648d..2b7f7e64160c0245e9adf2c79d00a521b81e0cb8 100644 --- a/sib-badge.js +++ b/sib-badge.js @@ -4,6 +4,11 @@ const base_url = import.meta.url.replace(/\/[^\/]*$/, ''); Helpers.importCSS(`${base_url}/css/badge.css`) class SIBBadge extends SIBTemplateElement { + constructor() { + super() + window.addEventListener('notificationsUpdated', () => this.render()); + } + static get propsDefinition() { return { dataSrc: 'data-src', diff --git a/sib-notifications.js b/sib-notifications.js index da6ca050e43113c34e6a8de74adadded703bf6d7..4e18513d112017b6255c8f020b476da8e729f962 100644 --- a/sib-notifications.js +++ b/sib-notifications.js @@ -22,10 +22,15 @@ class SIBNotifications extends SIBTemplateElement { // On click events, ... this.addEventListener("click", event => { - // unread button -> mark as read + // unread button -> dispatch read event let unreadButton = event.target.closest('button[data-unread=unread]') if (unreadButton) { - this._onClickUnreadButton(unreadButton) + window.dispatchEvent(new CustomEvent('read', { + detail: { + resource: unreadButton.closest('sib-display').resource // get resource from sib display + } + })); + unreadButton.disabled = true return } @@ -74,11 +79,14 @@ class SIBNotifications extends SIBTemplateElement { _onReadResource(resourceId) { const edits = this._getNotificationsList() .filter((notification) => notification.unread && - (notification.object['@id'] === resourceId || notification['@id'] === resourceId)) + (notification.object['@id'] === resourceId || notification['@id'] === resourceId)) // compare id with objects id or notifications id .map((notification) => store.patch(notification['@id'], { unread: false })) Promise.all(edits) - .then(() => this.render()) + .then(() => { + this.render(); + window.dispatchEvent(new CustomEvent('notificationsUpdated')); + }) .catch((error) => this._printError(error)); } @@ -94,10 +102,6 @@ class SIBNotifications extends SIBTemplateElement { this.querySelector('details').removeAttribute("open") }) } - _onClickUnreadButton(target) { - this._onReadResource(target.closest('sib-display').resource['@id']) - target.disabled = true - } _updateUnread() { let unread = Number(document.querySelector('#badge-unread').innerText)