2020-05-08 13:27:44 +00:00
|
|
|
//- Update badges from notifications list
|
|
|
|
async function updateBadges(element) {
|
|
|
|
const unreadNotifications = new Map();
|
|
|
|
const notifications = element.component.resource;
|
|
|
|
if (!notifications) return;
|
|
|
|
// Generate unread map
|
|
|
|
for (let notification of notifications['ldp:contains']) {
|
|
|
|
if (await notification['unread']) {
|
|
|
|
const object = await notification['object'];
|
|
|
|
unreadNotifications.set(object['@id'], (unreadNotifications.get(object['@id']) || 0) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// update badges
|
|
|
|
unreadNotifications.forEach((notifNumber, objectId) => {
|
2020-06-18 18:02:54 +00:00
|
|
|
const badge = document.querySelector(`solid-fix-badge[data-src="${objectId}"]`);
|
2020-05-08 13:27:44 +00:00
|
|
|
if (badge) {
|
|
|
|
badge.innerText = notifNumber || '';
|
|
|
|
badge.style.display = notifNumber ? 'block' : 'none';
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", function (event) {
|
|
|
|
|
|
|
|
//- Fix badges performances
|
|
|
|
// on load time
|
|
|
|
const checkNotificationMenuExist = setInterval(function () { // wait for notification menu to exist
|
|
|
|
const notifsMenu = document.getElementById('notifications-list');
|
|
|
|
if (notifsMenu) {
|
|
|
|
clearInterval(checkNotificationMenuExist);
|
|
|
|
notifsMenu.addEventListener('populate', (event) => {
|
|
|
|
const checkExist = setInterval(function () { // wait for left menus to exist
|
2020-06-18 18:02:54 +00:00
|
|
|
const subMenus = document.querySelectorAll('.sub-menu > solid-display > div');
|
2020-05-08 13:27:44 +00:00
|
|
|
if (subMenus.length >= 2) {
|
|
|
|
updateBadges(event.target);
|
|
|
|
clearInterval(checkExist);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
}, {
|
|
|
|
once: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
// on refresh notification list
|
|
|
|
window.addEventListener('notificationsRefresh', () => {
|
|
|
|
document.getElementById('notifications-list').addEventListener('populate', (event) => {
|
|
|
|
updateBadges(event.target);
|
|
|
|
}, {
|
|
|
|
once: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// on read notification
|
|
|
|
window.addEventListener('read', (event) => {
|
|
|
|
if (event.detail && event.detail.resource && event.detail.resource['@id']) {
|
2020-06-18 18:02:54 +00:00
|
|
|
const badge = document.querySelector(`solid-fix-badge[data-src="${event.detail.resource['@id']}"]`);
|
2020-05-08 13:27:44 +00:00
|
|
|
if (badge) badge.style.display = "none";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|