Cleanup and modernize JS

This commit is contained in:
Peter Pfeufer
2021-05-09 06:43:58 +00:00
committed by Ariel Rin
parent 630400fee4
commit bd2d19f867
13 changed files with 270 additions and 251 deletions

View File

@@ -1,3 +1,5 @@
/* global notificationUPdateSettings */
/*
This script refreshed the unread notification count in the top menu
on a regular basis so to keep the user apprised about newly arrived
@@ -6,70 +8,67 @@
The refresh rate can be changes via the Django setting NOTIFICATIONS_REFRESH_TIME.
See documentation for details.
*/
$(function () {
var elem = document.getElementById("dataExport");
var notificationsListViewUrl = elem.getAttribute("data-notificationsListViewUrl");
var notificationsRefreshTime = elem.getAttribute("data-notificationsRefreshTime");
var userNotificationsCountViewUrl = elem.getAttribute(
"data-userNotificationsCountViewUrl"
);
'use strict';
let notificationsListViewUrl = notificationUPdateSettings.notificationsListViewUrl;
let notificationsRefreshTime = notificationUPdateSettings.notificationsRefreshTime;
let userNotificationsCountViewUrl = notificationUPdateSettings.userNotificationsCountViewUrl;
// update the notification unread count in the top menu
function update_notifications() {
let updateNotifications = function () {
$.getJSON(userNotificationsCountViewUrl, function (data, status) {
if (status == 'success') {
var innerHtml = "";
var unread_count = data.unread_count;
if (unread_count > 0) {
if (status === 'success') {
let innerHtml = '';
let unreadCount = data.unread_count;
if (unreadCount > 0) {
innerHtml = (
`Notifications <span class="badge">${unread_count}</span>`
)
`Notifications <span class="badge">${unreadCount}</span>`
);
} else {
innerHtml = '<i class="far fa-bell"></i>';
}
else {
innerHtml = '<i class="far fa-bell"></i>'
}
$("#menu_item_notifications").html(
$('#menu_item_notifications').html(
`<a href="${notificationsListViewUrl}">${innerHtml}</a>`
);
}
else {
} else {
console.error(
`Failed to load HTMl to render notifications item. Error: `
`${xhr.status}': '${xhr.statusText}`
`Failed to load HTMl to render notifications item. Error: ${xhr.status}': '${xhr.statusText}`
);
}
});
}
};
var myInterval;
let myInterval;
// activate automatic refreshing every x seconds
function activate_refreshing() {
let activateRefreshing = function () {
if (notificationsRefreshTime > 0) {
myInterval = setInterval(
update_notifications, notificationsRefreshTime * 1000
updateNotifications, notificationsRefreshTime * 1000
);
}
}
};
// deactivate automatic refreshing
function deactivate_refreshing() {
let deactivateRefreshing = function () {
if ((notificationsRefreshTime > 0) && (typeof myInterval !== 'undefined')) {
clearInterval(myInterval)
clearInterval(myInterval);
}
}
};
// refreshing only happens on active browser tab
$(document).on({
'show': function () {
activate_refreshing()
activateRefreshing();
},
'hide': function () {
deactivate_refreshing()
deactivateRefreshing();
}
});
// Initial start of refreshing on script loading
activate_refreshing()
activateRefreshing();
});

View File

@@ -1,23 +1,49 @@
/* global moment */
/**
* Get a duration string like countdown.js
* e.g. "1y 2d 3h 4m 5s"
* @param duration moment.duration
*/
function getDurationString(duration) {
var out = "";
* Get a duration string like countdown.js
* e.g. "1y 2d 3h 4m 5s"
*
* @param duration
* @returns {string}
*/
let getDurationString = function (duration) {
'use strict';
let out = '';
if (duration.years()) {
out += duration.years() + 'y ';
}
if (duration.months()) {
out += duration.months() + 'm ';
}
if (duration.days()) {
out += duration.days() + 'd ';
}
return out + duration.hours() + "h " + duration.minutes() + "m " + duration.seconds() + "s";
}
return out + duration.hours() + 'h ' + duration.minutes() + 'm ' + duration.seconds() + 's';
};
function getCurrentEveTimeString() {
return moment().utc().format('dddd LL HH:mm:ss')
}
/**
* returns the current eve time as a formatted string
*
* condition:
* only if moment.js is loaded before,
* if not this function returns an empty string to avoid JS errors from happening.
*
* @returns {string}
*/
let getCurrentEveTimeString = function () {
'use strict';
let returnValue = '';
if (window.moment) {
returnValue = moment().utc().format('dddd LL HH:mm:ss');
}
return returnValue;
};