Files
Atay-Makhzan/web_src/js/features/notification.js
T

221 lines
6.6 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
2020-04-24 04:57:38 +01:00
const {appSubUrl, csrfToken, notificationSettings, assetVersionEncoded} = window.config;
let notificationSequenceNumber = 0;
2020-04-24 04:57:38 +01:00
export function initNotificationsTable() {
2023-05-25 04:31:26 +02:00
const table = document.getElementById('notification_table');
if (!table) return;
// when page restores from bfcache, delete previously clicked items
window.addEventListener('pageshow', (e) => {
if (e.persisted) { // page was restored from bfcache
const table = document.getElementById('notification_table');
const unreadCountEl = document.querySelector('.notifications-unread-count');
let unreadCount = parseInt(unreadCountEl.textContent);
for (const item of table.querySelectorAll('.notifications-item[data-remove="true"]')) {
item.remove();
unreadCount -= 1;
}
unreadCountEl.textContent = unreadCount;
}
});
// mark clicked unread links for deletion on bfcache restore
for (const link of table.querySelectorAll('.notifications-item[data-status="1"] .notifications-link')) {
link.addEventListener('click', (e) => {
e.target.closest('.notifications-item').setAttribute('data-remove', 'true');
});
}
$('#notification_table .button').on('click', function () {
(async () => {
const data = await updateNotification(
$(this).data('url'),
$(this).data('status'),
$(this).data('page'),
$(this).data('q'),
$(this).data('notification-id'),
);
2020-04-24 04:57:38 +01:00
if ($(data).data('sequence-number') === notificationSequenceNumber) {
$('#notification_div').replaceWith(data);
initNotificationsTable();
}
await updateNotificationCount();
})();
2020-04-24 04:57:38 +01:00
return false;
});
}
2020-07-03 10:55:36 +01:00
async function receiveUpdateCount(event) {
try {
const data = JSON.parse(event.data);
for (const count of document.querySelectorAll('.notification_count')) {
count.classList.toggle('gt-hidden', data.Count === 0);
count.textContent = `${data.Count}`;
2020-07-03 10:55:36 +01:00
}
await updateNotificationTable();
} catch (error) {
console.error(error, event);
}
}
export function initNotificationCount() {
2020-05-07 22:49:00 +01:00
const notificationCount = $('.notification_count');
if (!notificationCount.length) {
2020-04-24 04:57:38 +01:00
return;
}
let usingPeriodicPoller = false;
const startPeriodicPoller = (timeout, lastCount) => {
if (timeout <= 0 || !Number.isFinite(timeout)) return;
usingPeriodicPoller = true;
lastCount = lastCount ?? notificationCount.text();
setTimeout(async () => {
await updateNotificationCountWithCallback(startPeriodicPoller, timeout, lastCount);
}, timeout);
};
if (notificationSettings.EventSourceUpdateTime > 0 && window.EventSource && window.SharedWorker) {
2020-07-03 10:55:36 +01:00
// Try to connect to the event source via the shared worker first
const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js?v=${assetVersionEncoded}`, 'notification-worker');
2020-12-27 15:24:27 +01:00
worker.addEventListener('error', (event) => {
console.error('worker error', event);
2020-12-27 15:24:27 +01:00
});
2021-08-17 07:32:48 +02:00
worker.port.addEventListener('messageerror', () => {
console.error('unable to deserialize message');
2021-08-17 07:32:48 +02:00
});
2020-12-27 15:24:27 +01:00
worker.port.postMessage({
type: 'start',
url: `${window.location.origin}${appSubUrl}/user/events`,
2020-12-27 15:24:27 +01:00
});
worker.port.addEventListener('message', (event) => {
if (!event.data || !event.data.type) {
console.error('unknown worker message event', event);
2020-12-27 15:24:27 +01:00
return;
}
if (event.data.type === 'notification-count') {
const _promise = receiveUpdateCount(event.data);
} else if (event.data.type === 'no-event-source') {
// browser doesn't support EventSource, falling back to periodic poller
if (!usingPeriodicPoller) startPeriodicPoller(notificationSettings.MinTimeout);
2020-12-27 15:24:27 +01:00
} else if (event.data.type === 'error') {
console.error('worker port event error', event.data);
2020-12-27 15:24:27 +01:00
} else if (event.data.type === 'logout') {
2021-04-08 00:48:13 +01:00
if (event.data.data !== 'here') {
2020-07-03 10:55:36 +01:00
return;
}
worker.port.postMessage({
type: 'close',
});
worker.port.close();
window.location.href = appSubUrl;
} else if (event.data.type === 'close') {
worker.port.postMessage({
type: 'close',
});
worker.port.close();
2020-12-27 15:24:27 +01:00
}
});
worker.port.addEventListener('error', (e) => {
console.error('worker port error', e);
2020-12-27 15:24:27 +01:00
});
worker.port.start();
window.addEventListener('beforeunload', () => {
worker.port.postMessage({
type: 'close',
2020-07-03 10:55:36 +01:00
});
2020-12-27 15:24:27 +01:00
worker.port.close();
});
2020-04-24 04:57:38 +01:00
2020-12-27 15:24:27 +01:00
return;
2020-04-24 04:57:38 +01:00
}
2020-05-07 22:49:00 +01:00
startPeriodicPoller(notificationSettings.MinTimeout);
2020-04-24 04:57:38 +01:00
}
async function updateNotificationCountWithCallback(callback, timeout, lastCount) {
const currentCount = $('.notification_count').text();
if (lastCount !== currentCount) {
callback(notificationSettings.MinTimeout, currentCount);
2020-04-24 04:57:38 +01:00
return;
}
const newCount = await updateNotificationCount();
let needsUpdate = false;
if (lastCount !== newCount) {
needsUpdate = true;
timeout = notificationSettings.MinTimeout;
} else if (timeout < notificationSettings.MaxTimeout) {
timeout += notificationSettings.TimeoutStep;
2020-04-24 04:57:38 +01:00
}
callback(timeout, newCount);
2020-05-07 22:49:00 +01:00
if (needsUpdate) {
await updateNotificationTable();
}
}
2020-04-24 04:57:38 +01:00
2020-05-07 22:49:00 +01:00
async function updateNotificationTable() {
2020-04-24 04:57:38 +01:00
const notificationDiv = $('#notification_div');
2020-05-07 22:49:00 +01:00
if (notificationDiv.length > 0) {
2020-04-24 04:57:38 +01:00
const data = await $.ajax({
type: 'GET',
2023-07-10 02:42:31 +08:00
url: `${appSubUrl}/notifications${window.location.search}`,
2020-04-24 04:57:38 +01:00
data: {
'div-only': true,
'sequence-number': ++notificationSequenceNumber,
2020-04-24 04:57:38 +01:00
}
});
if ($(data).data('sequence-number') === notificationSequenceNumber) {
notificationDiv.replaceWith(data);
initNotificationsTable();
}
2020-04-24 04:57:38 +01:00
}
}
async function updateNotificationCount() {
const data = await $.ajax({
type: 'GET',
url: `${appSubUrl}/notifications/new`,
2020-04-24 04:57:38 +01:00
headers: {
'X-Csrf-Token': csrfToken,
2020-04-24 04:57:38 +01:00
},
});
const notificationCount = $('.notification_count');
if (data.new === 0) {
notificationCount.addClass('gt-hidden');
2020-04-24 04:57:38 +01:00
} else {
notificationCount.removeClass('gt-hidden');
2020-04-24 04:57:38 +01:00
}
notificationCount.text(`${data.new}`);
return `${data.new}`;
}
async function updateNotification(url, status, page, q, notificationID) {
if (status !== 'pinned') {
$(`#notification_${notificationID}`).remove();
}
return $.ajax({
type: 'POST',
url,
data: {
_csrf: csrfToken,
2020-04-24 04:57:38 +01:00
notification_id: notificationID,
status,
page,
q,
noredirect: true,
'sequence-number': ++notificationSequenceNumber,
2020-04-24 04:57:38 +01:00
},
});
}