2021-10-21 07:37:43 +00:00
|
|
|
const {appSubUrl, csrfToken, notificationSettings} = window.config;
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2021-07-17 16:18:10 +00:00
|
|
|
let notificationSequenceNumber = 0;
|
|
|
|
|
2020-04-24 03:57:38 +00:00
|
|
|
export function initNotificationsTable() {
|
|
|
|
$('#notification_table .button').on('click', async function () {
|
|
|
|
const data = await updateNotification(
|
|
|
|
$(this).data('url'),
|
|
|
|
$(this).data('status'),
|
|
|
|
$(this).data('page'),
|
|
|
|
$(this).data('q'),
|
|
|
|
$(this).data('notification-id'),
|
|
|
|
);
|
|
|
|
|
2021-07-17 16:18:10 +00:00
|
|
|
if ($(data).data('sequence-number') === notificationSequenceNumber) {
|
|
|
|
$('#notification_div').replaceWith(data);
|
|
|
|
initNotificationsTable();
|
|
|
|
}
|
2020-04-24 03:57:38 +00:00
|
|
|
await updateNotificationCount();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-03 09:55:36 +00:00
|
|
|
async function receiveUpdateCount(event) {
|
|
|
|
try {
|
|
|
|
const data = JSON.parse(event.data);
|
|
|
|
|
|
|
|
const notificationCount = document.querySelector('.notification_count');
|
|
|
|
if (data.Count > 0) {
|
|
|
|
notificationCount.classList.remove('hidden');
|
|
|
|
} else {
|
|
|
|
notificationCount.classList.add('hidden');
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:04:00 +00:00
|
|
|
notificationCount.textContent = `${data.Count}`;
|
2020-07-03 09:55:36 +00:00
|
|
|
await updateNotificationTable();
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 09:27:25 +00:00
|
|
|
export function initNotificationCount() {
|
2020-05-07 21:49:00 +00:00
|
|
|
const notificationCount = $('.notification_count');
|
|
|
|
|
|
|
|
if (!notificationCount.length) {
|
2020-04-24 03:57:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-21 07:37:43 +00:00
|
|
|
if (notificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource && window.SharedWorker) {
|
2020-07-03 09:55:36 +00:00
|
|
|
// Try to connect to the event source via the shared worker first
|
2020-12-27 14:24:27 +00:00
|
|
|
const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker');
|
|
|
|
worker.addEventListener('error', (event) => {
|
|
|
|
console.error(event);
|
|
|
|
});
|
2021-08-17 05:32:48 +00:00
|
|
|
worker.port.addEventListener('messageerror', () => {
|
2020-12-27 14:24:27 +00:00
|
|
|
console.error('Unable to deserialize message');
|
2021-08-17 05:32:48 +00:00
|
|
|
});
|
2020-12-27 14:24:27 +00:00
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'start',
|
2021-10-21 07:37:43 +00:00
|
|
|
url: `${window.location.origin}${appSubUrl}/user/events`,
|
2020-12-27 14:24:27 +00:00
|
|
|
});
|
|
|
|
worker.port.addEventListener('message', (event) => {
|
|
|
|
if (!event.data || !event.data.type) {
|
2020-07-03 09:55:36 +00:00
|
|
|
console.error(event);
|
2020-12-27 14:24:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.data.type === 'notification-count') {
|
2021-11-09 09:27:25 +00:00
|
|
|
const _promise = receiveUpdateCount(event.data);
|
2020-12-27 14:24:27 +00:00
|
|
|
} else if (event.data.type === 'error') {
|
|
|
|
console.error(event.data);
|
|
|
|
} else if (event.data.type === 'logout') {
|
2021-04-07 23:48:13 +00:00
|
|
|
if (event.data.data !== 'here') {
|
2020-07-03 09:55:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
2021-10-21 07:37:43 +00:00
|
|
|
window.location.href = appSubUrl;
|
2021-04-04 21:37:50 +00:00
|
|
|
} else if (event.data.type === 'close') {
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
2020-12-27 14:24:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
worker.port.addEventListener('error', (e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
worker.port.start();
|
|
|
|
window.addEventListener('beforeunload', () => {
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
2020-07-03 09:55:36 +00:00
|
|
|
});
|
2020-12-27 14:24:27 +00:00
|
|
|
worker.port.close();
|
|
|
|
});
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2020-12-27 14:24:27 +00:00
|
|
|
return;
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
2020-05-07 21:49:00 +00:00
|
|
|
|
2021-10-21 07:37:43 +00:00
|
|
|
if (notificationSettings.MinTimeout <= 0) {
|
2020-05-07 21:49:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn = (timeout, lastCount) => {
|
|
|
|
setTimeout(async () => {
|
|
|
|
await updateNotificationCountWithCallback(fn, timeout, lastCount);
|
|
|
|
}, timeout);
|
|
|
|
};
|
|
|
|
|
2021-10-21 07:37:43 +00:00
|
|
|
fn(notificationSettings.MinTimeout, notificationCount.text());
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNotificationCountWithCallback(callback, timeout, lastCount) {
|
|
|
|
const currentCount = $('.notification_count').text();
|
|
|
|
if (lastCount !== currentCount) {
|
2021-10-21 07:37:43 +00:00
|
|
|
callback(notificationSettings.MinTimeout, currentCount);
|
2020-04-24 03:57:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newCount = await updateNotificationCount();
|
|
|
|
let needsUpdate = false;
|
|
|
|
|
|
|
|
if (lastCount !== newCount) {
|
|
|
|
needsUpdate = true;
|
2021-10-21 07:37:43 +00:00
|
|
|
timeout = notificationSettings.MinTimeout;
|
|
|
|
} else if (timeout < notificationSettings.MaxTimeout) {
|
|
|
|
timeout += notificationSettings.TimeoutStep;
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(timeout, newCount);
|
2020-05-07 21:49:00 +00:00
|
|
|
if (needsUpdate) {
|
|
|
|
await updateNotificationTable();
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2020-05-07 21:49:00 +00:00
|
|
|
async function updateNotificationTable() {
|
2020-04-24 03:57:38 +00:00
|
|
|
const notificationDiv = $('#notification_div');
|
2020-05-07 21:49:00 +00:00
|
|
|
if (notificationDiv.length > 0) {
|
2020-04-24 03:57:38 +00:00
|
|
|
const data = await $.ajax({
|
|
|
|
type: 'GET',
|
2021-10-21 07:37:43 +00:00
|
|
|
url: `${appSubUrl}/notifications?${notificationDiv.data('params')}`,
|
2020-04-24 03:57:38 +00:00
|
|
|
data: {
|
|
|
|
'div-only': true,
|
2021-07-17 16:18:10 +00:00
|
|
|
'sequence-number': ++notificationSequenceNumber,
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
|
|
|
});
|
2021-07-17 16:18:10 +00:00
|
|
|
if ($(data).data('sequence-number') === notificationSequenceNumber) {
|
|
|
|
notificationDiv.replaceWith(data);
|
|
|
|
initNotificationsTable();
|
|
|
|
}
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNotificationCount() {
|
|
|
|
const data = await $.ajax({
|
|
|
|
type: 'GET',
|
2021-10-21 07:37:43 +00:00
|
|
|
url: `${appSubUrl}/api/v1/notifications/new`,
|
2020-04-24 03:57:38 +00:00
|
|
|
headers: {
|
2021-10-21 07:37:43 +00:00
|
|
|
'X-Csrf-Token': csrfToken,
|
2020-04-24 03:57:38 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const notificationCount = $('.notification_count');
|
|
|
|
if (data.new === 0) {
|
|
|
|
notificationCount.addClass('hidden');
|
|
|
|
} else {
|
|
|
|
notificationCount.removeClass('hidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
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: {
|
2021-10-21 07:37:43 +00:00
|
|
|
_csrf: csrfToken,
|
2020-04-24 03:57:38 +00:00
|
|
|
notification_id: notificationID,
|
|
|
|
status,
|
|
|
|
page,
|
|
|
|
q,
|
|
|
|
noredirect: true,
|
2021-07-17 16:18:10 +00:00
|
|
|
'sequence-number': ++notificationSequenceNumber,
|
2020-04-24 03:57:38 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|