code restructure

This commit is contained in:
Matthias 2021-11-30 17:19:04 +01:00
parent d542314520
commit a1e0bef70d
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076

View File

@ -124,7 +124,7 @@ function showMessage(message, options={}) {
* The notification checker is initiated when the document is loaded. It checks if there are unread notifications * The notification checker is initiated when the document is loaded. It checks if there are unread notifications
* if unread messages exist the alert flag is raised by making it visible * if unread messages exist the alert flag is raised by making it visible
**/ **/
function notificationCheck() { function notificationCheck() {
// only refresh state if in focus // only refresh state if in focus
if (document.hasFocus()) { if (document.hasFocus()) {
inventreeGet( inventreeGet(
@ -142,17 +142,36 @@ function showMessage(message, options={}) {
} }
/** /**
* updates the notification counter * handles read / unread buttons and UI rebuilding
**/ **/
function updateNotificationIndicator(count) { function updateNotificationReadState(btn, panel_caller=false) {
if (count == 0) { var url = `/api/notifications/${btn.attr('pk')}/${btn.attr('target')}/`;
$("#notification-alert").addClass("d-none");
} else {
$("#notification-alert").removeClass("d-none");
}
$("#notification-counter").html(count);
}
inventreePut(url, {}, {
method: 'POST',
success: function() {
// update the notification tables if they were declared
if (window.updateNotifications) {
window.updateNotifications();
}
// update current notification count
var count = parseInt($("#notification-counter").html());
if (btn.attr('target') == 'read') {
count = count - 1;
} else {
count = count + 1;
}
// update notification indicator now
updateNotificationIndicator(count);
// remove notification if called from notification panel
if (panel_caller) {
btn.parent().parent().remove()
}
}
});
};
/** /**
* Returns the html for a read / unread button * Returns the html for a read / unread button
@ -225,32 +244,14 @@ function closeNotificationPanel() {
$('#notification-center').html(`<p class='text-muted'>{% trans "Notifications will load here" %}</p>`); $('#notification-center').html(`<p class='text-muted'>{% trans "Notifications will load here" %}</p>`);
} }
/**
function updateNotificationReadState(btn, panel_caller=false) { * updates the notification counter
var url = `/api/notifications/${btn.attr('pk')}/${btn.attr('target')}/`; **/
function updateNotificationIndicator(count) {
inventreePut(url, {}, { if (count == 0) {
method: 'POST', $("#notification-alert").addClass("d-none");
success: function() { } else {
// update the notification tables if they were declared $("#notification-alert").removeClass("d-none");
if (window.updateNotifications) {
window.updateNotifications();
}
// update current notification count
var count = parseInt($("#notification-counter").html());
if (btn.attr('target') == 'read') {
count = count - 1;
} else {
count = count + 1;
}
// update notification indicator now
updateNotificationIndicator(count);
// remove notification if called from notification panel
if (panel_caller) {
btn.parent().parent().remove()
}
} }
}); $("#notification-counter").html(count);
}; }