modal loop prevention #992

This commit is contained in:
jokob-sk
2025-02-16 10:06:34 +11:00
parent ffa0457342
commit a0e117f92e

View File

@@ -4,262 +4,262 @@
var modalCallbackFunction = ""; var modalCallbackFunction = "";
function showModalOK(title, message, callbackFunction) { function showModalOK(title, message, callbackFunction) {
showModalOk(title, message, callbackFunction); showModalOk(title, message, callbackFunction);
} }
function showModalOk(title, message, callbackFunction) { function showModalOk(title, message, callbackFunction) {
// set captions // set captions
$("#modal-ok-title").html(title); $("#modal-ok-title").html(title);
$("#modal-ok-message").html(message); $("#modal-ok-message").html(message);
if (callbackFunction != null) { if (callbackFunction != null) {
$("#modal-ok-OK").click(function () { $("#modal-ok-OK").click(function () {
callbackFunction(); callbackFunction();
}); });
} }
// Show modal // Show modal
$("#modal-ok").modal("show"); $("#modal-ok").modal("show");
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function showModalDefault(title, message, btnCancel, btnOK, callbackFunction) { function showModalDefault(title, message, btnCancel, btnOK, callbackFunction) {
// set captions // set captions
$("#modal-default-title").html(title); $("#modal-default-title").html(title);
$("#modal-default-message").html(message); $("#modal-default-message").html(message);
$("#modal-default-cancel").html(btnCancel); $("#modal-default-cancel").html(btnCancel);
$("#modal-default-OK").html(btnOK); $("#modal-default-OK").html(btnOK);
modalCallbackFunction = callbackFunction; modalCallbackFunction = callbackFunction;
// Show modal // Show modal
$("#modal-default").modal("show"); $("#modal-default").modal("show");
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function showModalDefaultStrParam( function showModalDefaultStrParam(
title, title,
message, message,
btnCancel, btnCancel,
btnOK, btnOK,
callbackFunction, callbackFunction,
param = "" param = ""
) { ) {
// set captions // set captions
$("#modal-str-title").html(title); $("#modal-str-title").html(title);
$("#modal-str-message").html(message); $("#modal-str-message").html(message);
$("#modal-str-cancel").html(btnCancel); $("#modal-str-cancel").html(btnCancel);
$("#modal-str-OK").html(btnOK); $("#modal-str-OK").html(btnOK);
$("#modal-str-OK").off("click"); //remove existing handlers $("#modal-str-OK").off("click"); //remove existing handlers
$("#modal-str-OK").on("click", function () { $("#modal-str-OK").on("click", function () {
$("#modal-str").modal("hide"); $("#modal-str").modal("hide");
callbackFunction(param); callbackFunction(param);
}); });
// Show modal // Show modal
$("#modal-str").modal("show"); $("#modal-str").modal("show");
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function showModalWarning( function showModalWarning(
title, title,
message, message,
btnCancel = getString("Gen_Cancel"), btnCancel = getString("Gen_Cancel"),
btnOK = getString("Gen_Okay"), btnOK = getString("Gen_Okay"),
callbackFunction = null, callbackFunction = null,
triggeredBy = null triggeredBy = null
) { ) {
// set captions // set captions
$("#modal-warning-title").html(title); $("#modal-warning-title").html(title);
$("#modal-warning-message").html(message); $("#modal-warning-message").html(message);
$("#modal-warning-cancel").html(btnCancel); $("#modal-warning-cancel").html(btnCancel);
$("#modal-warning-OK").html(btnOK); $("#modal-warning-OK").html(btnOK);
if (callbackFunction != null) { if (callbackFunction != null) {
modalCallbackFunction = callbackFunction; modalCallbackFunction = callbackFunction;
} }
if (triggeredBy != null) { if (triggeredBy != null) {
$('#'+prefix).attr("data-myparam-triggered-by", triggeredBy) $('#'+prefix).attr("data-myparam-triggered-by", triggeredBy)
} }
// Show modal // Show modal
$("#modal-warning").modal("show"); $("#modal-warning").modal("show");
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function showModalInput( function showModalInput(
title, title,
message, message,
btnCancel = getString("Gen_Cancel"), btnCancel = getString("Gen_Cancel"),
btnOK = getString("Gen_Okay"), btnOK = getString("Gen_Okay"),
callbackFunction = null, callbackFunction = null,
triggeredBy = null triggeredBy = null
) { ) {
prefix = "modal-input"; prefix = "modal-input";
// set captions // set captions
$(`#${prefix}-title`).html(title); $(`#${prefix}-title`).html(title);
$(`#${prefix}-message`).html(message); $(`#${prefix}-message`).html(message);
$(`#${prefix}-cancel`).html(btnCancel); $(`#${prefix}-cancel`).html(btnCancel);
$(`#${prefix}-OK`).html(btnOK); $(`#${prefix}-OK`).html(btnOK);
if (callbackFunction != null) { if (callbackFunction != null) {
modalCallbackFunction = callbackFunction; modalCallbackFunction = callbackFunction;
} }
if (triggeredBy != null) { if (triggeredBy != null) {
$('#'+prefix).attr("data-myparam-triggered-by", triggeredBy) $('#'+prefix).attr("data-myparam-triggered-by", triggeredBy)
} }
// Show modal // Show modal
$(`#${prefix}`).modal("show"); $(`#${prefix}`).modal("show");
setTimeout(function () { setTimeout(function () {
$(`#${prefix}-textarea`).focus(); $(`#${prefix}-textarea`).focus();
}, 500); }, 500);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function showModalFieldInput( function showModalFieldInput(
title, title,
message, message,
btnCancel = getString("Gen_Cancel"), btnCancel = getString("Gen_Cancel"),
btnOK = getString("Gen_Okay"), btnOK = getString("Gen_Okay"),
curValue = "", curValue = "",
callbackFunction = null, callbackFunction = null,
triggeredBy = null triggeredBy = null
) { ) {
// set captions // set captions
prefix = "modal-field-input"; prefix = "modal-field-input";
$(`#${prefix}-title`).html(title); $(`#${prefix}-title`).html(title);
$(`#${prefix}-message`).html(message); $(`#${prefix}-message`).html(message);
$(`#${prefix}-cancel`).html(btnCancel); $(`#${prefix}-cancel`).html(btnCancel);
$(`#${prefix}-OK`).html(btnOK); $(`#${prefix}-OK`).html(btnOK);
if (callbackFunction != null) { if (callbackFunction != null) {
modalCallbackFunction = callbackFunction; modalCallbackFunction = callbackFunction;
} }
if (triggeredBy != null) { if (triggeredBy != null) {
$('#'+prefix).attr("data-myparam-triggered-by", triggeredBy) $('#'+prefix).attr("data-myparam-triggered-by", triggeredBy)
} }
$(`#${prefix}-field`).val(curValue); $(`#${prefix}-field`).val(curValue);
setTimeout(function () { setTimeout(function () {
$(`#${prefix}-field`).focus(); $(`#${prefix}-field`).focus();
}, 500); }, 500);
// Show modal // Show modal
$(`#${prefix}`).modal("show"); $(`#${prefix}`).modal("show");
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function modalDefaultOK() { function modalDefaultOK() {
// Hide modal // Hide modal
$("#modal-default").modal("hide"); $("#modal-default").modal("hide");
// timer to execute function // timer to execute function
window.setTimeout(function () { window.setTimeout(function () {
if (typeof modalCallbackFunction === "function") { if (typeof modalCallbackFunction === "function") {
modalCallbackFunction(); // Direct call modalCallbackFunction(); // Direct call
} else if (typeof modalCallbackFunction === "string" && typeof window[modalCallbackFunction] === "function") { } else if (typeof modalCallbackFunction === "string" && typeof window[modalCallbackFunction] === "function") {
window[modalCallbackFunction](); // Call via window window[modalCallbackFunction](); // Call via window
} else { } else {
console.error("Invalid callback function"); console.error("Invalid callback function");
} }
}, 100); }, 100);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function modalDefaultInput() { function modalDefaultInput() {
// Hide modal // Hide modal
$("#modal-input").modal("hide"); $("#modal-input").modal("hide");
// timer to execute function // timer to execute function
window.setTimeout(function () { window.setTimeout(function () {
if (typeof modalCallbackFunction === "function") { if (typeof modalCallbackFunction === "function") {
modalCallbackFunction(); // Direct call modalCallbackFunction(); // Direct call
} else if (typeof modalCallbackFunction === "string" && typeof window[modalCallbackFunction] === "function") { } else if (typeof modalCallbackFunction === "string" && typeof window[modalCallbackFunction] === "function") {
window[modalCallbackFunction](); // Call via window window[modalCallbackFunction](); // Call via window
} else { } else {
console.error("Invalid callback function"); console.error("Invalid callback function");
} }
}, 100); }, 100);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function modalDefaultFieldInput() { function modalDefaultFieldInput() {
// Hide modal // Hide modal
$("#modal-field-input").modal("hide"); $("#modal-field-input").modal("hide");
// timer to execute function // timer to execute function
window.setTimeout(function () { window.setTimeout(function () {
if (typeof modalCallbackFunction === "function") { if (typeof modalCallbackFunction === "function") {
modalCallbackFunction(); // Direct call modalCallbackFunction(); // Direct call
} else if (typeof modalCallbackFunction === "string" && typeof window[modalCallbackFunction] === "function") { } else if (typeof modalCallbackFunction === "string" && typeof window[modalCallbackFunction] === "function") {
window[modalCallbackFunction](); // Call via window window[modalCallbackFunction](); // Call via window
} else { } else {
console.error("Invalid callback function"); console.error("Invalid callback function");
} }
}, 100); }, 100);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function modalWarningOK() { function modalWarningOK() {
// Hide modal // Hide modal
$("#modal-warning").modal("hide"); $("#modal-warning").modal("hide");
// timer to execute function // timer to execute function
window.setTimeout(function () { window.setTimeout(function () {
if (typeof modalCallbackFunction === "function") { if (typeof modalCallbackFunction === "function") {
modalCallbackFunction(); // Direct call modalCallbackFunction(); // Direct call
} else if (typeof modalCallbackFunction === "string" && typeof window[modalCallbackFunction] === "function") { } else if (typeof modalCallbackFunction === "string" && typeof window[modalCallbackFunction] === "function") {
window[modalCallbackFunction](); // Call via window window[modalCallbackFunction](); // Call via window
} else { } else {
console.error("Invalid callback function"); console.error("Invalid callback function");
} }
}, 100); }, 100);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function showMessage(textMessage = "", timeout = 3000, colorClass = "modal_green") { function showMessage(textMessage = "", timeout = 3000, colorClass = "modal_green") {
if (textMessage.toLowerCase().includes("error")) { if (textMessage.toLowerCase().includes("error")) {
// show error // show error
alert(textMessage); alert(textMessage);
} else { } else {
// show temporary notification // show temporary notification
$("#notification_modal").removeClass(); // remove all classes $("#notification_modal").removeClass(); // remove all classes
$("#notification_modal").addClass("alert alert-dimissible notification_modal"); // add default ones $("#notification_modal").addClass("alert alert-dimissible notification_modal"); // add default ones
$("#notification_modal").addClass(colorClass); // add color modifiers $("#notification_modal").addClass(colorClass); // add color modifiers
// message // message
$("#alert-message").html(textMessage); $("#alert-message").html(textMessage);
// timeout // timeout
$("#notification_modal").fadeIn(1, function () { $("#notification_modal").fadeIn(1, function () {
window.setTimeout(function () { window.setTimeout(function () {
$("#notification_modal").fadeOut(500); $("#notification_modal").fadeOut(500);
}, timeout); }, timeout);
}); });
} }
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function showTickerAnnouncement(textMessage = "") { function showTickerAnnouncement(textMessage = "") {
if (textMessage.toLowerCase().includes("error")) { if (textMessage.toLowerCase().includes("error")) {
// show error // show error
alert(textMessage); alert(textMessage);
} else { } else {
// show permanent notification // show permanent notification
$("#ticker-message").html(textMessage); $("#ticker-message").html(textMessage);
$("#tickerAnnouncement").removeClass("myhidden"); $("#tickerAnnouncement").removeClass("myhidden");
// Move the tickerAnnouncement element to ticker_announcement_plc // Move the tickerAnnouncement element to ticker_announcement_plc
$("#tickerAnnouncement").appendTo("#ticker_announcement_plc"); $("#tickerAnnouncement").appendTo("#ticker_announcement_plc");
} }
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@@ -267,30 +267,30 @@ function showTickerAnnouncement(textMessage = "") {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
$(document).ready(function () { $(document).ready(function () {
$(document).on("keydown", function (event) { $(document).on("keydown", function (event) {
// ESC key is pressed // ESC key is pressed
if (event.keyCode === 27) { if (event.keyCode === 27) {
// Trigger modal dismissal // Trigger modal dismissal
$(".modal").modal("hide"); $(".modal").modal("hide");
} }
// Enter key is pressed // Enter key is pressed
if (event.keyCode === 13) { if (event.keyCode === 13) {
$(".modal:visible").find(".btn-modal-submit").click(); // Trigger the click event of the OK button in visible modals $(".modal:visible").find(".btn-modal-submit").click(); // Trigger the click event of the OK button in visible modals
} }
}); });
}); });
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Escape text // Escape text
function safeDecodeURIComponent(content) { function safeDecodeURIComponent(content) {
try { try {
return decodeURIComponent(content); return decodeURIComponent(content);
} catch (error) { } catch (error) {
console.warn('Failed to decode URI component:', error); console.warn('Failed to decode URI component:', error);
return content; // Return the original content if decoding fails return content; // Return the original content if decoding fails
} }
} }
@@ -299,82 +299,83 @@ function safeDecodeURIComponent(content) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Function to check for notifications // Function to check for notifications
function checkNotification() { function checkNotification() {
const notificationEndpoint = 'php/server/utilNotification.php?action=get_unread_notifications'; const notificationEndpoint = 'php/server/utilNotification.php?action=get_unread_notifications';
const phpEndpoint = 'php/server/utilNotification.php'; const phpEndpoint = 'php/server/utilNotification.php';
$.ajax({ $.ajax({
url: notificationEndpoint, url: notificationEndpoint,
type: 'GET', type: 'GET',
success: function(response) { success: function(response) {
// console.log(response); // console.log(response);
if(response != "[]") if(response != "[]")
{ {
// Find the oldest unread notification with level "interrupt"
const oldestInterruptNotification = response.find(notification => notification.read === 0 && notification.level === "interrupt");
const allUnreadNotification = response.filter(notification => notification.read === 0 && notification.level === "alert");
// Find the oldest unread notification with level "interrupt" if (oldestInterruptNotification) {
const oldestInterruptNotification = response.find(notification => notification.read === 0 && notification.level === "interrupt"); // Show modal dialog with the oldest unread notification
const allUnreadNotification = response.filter(notification => notification.read === 0 && notification.level === "alert");
if (oldestInterruptNotification) { console.log(oldestInterruptNotification.content);
// Show modal dialog with the oldest unread notification
console.log(oldestInterruptNotification.content); const decodedContent = safeDecodeURIComponent(oldestInterruptNotification.content);
const decodedContent = safeDecodeURIComponent(oldestInterruptNotification.content); // only check and display modal if no modal currently displayed to prevent looping
if($("#modal-ok").is(":visible") == false)
showModalOK("Notification", decodedContent, function() { {
// Mark the notification as read showModalOK("Notification", decodedContent, function() {
$.ajax({ // Mark the notification as read
url: phpEndpoint, $.ajax({
type: 'GET', url: phpEndpoint,
data: { type: 'GET',
action: 'mark_notification_as_read', data: {
guid: oldestInterruptNotification.guid action: 'mark_notification_as_read',
}, guid: oldestInterruptNotification.guid
success: function(response) { },
console.log(response); success: function(response) {
// After marking the notification as read, check for the next one console.log(response);
checkNotification(); // After marking the notification as read, check for the next one
hideSpinner(); checkNotification();
}, hideSpinner();
error: function(xhr, status, error) { },
console.error("Error marking notification as read:", status, error); error: function(xhr, status, error) {
}, console.error("Error marking notification as read:", status, error);
complete:function() { },
hideSpinner(); complete:function() {
} hideSpinner();
});
});
} }
});
handleUnreadNotifications(allUnreadNotification.length) });
}
}
},
error: function() {
console.warn(`🟥 Error checking ${notificationEndpoint}`)
} }
});
handleUnreadNotifications(allUnreadNotification.length)
}
},
error: function() {
console.warn(`🟥 Error checking ${notificationEndpoint}`)
}
});
} }
// Handling unread notifications favicon + bell floating number bublbe // Handling unread notifications favicon + bell floating number bublbe
function handleUnreadNotifications(count) { function handleUnreadNotifications(count) {
$('#unread-notifications-bell-count').html(count); $('#unread-notifications-bell-count').html(count);
if (count > 0) { if (count > 0) {
$('#unread-notifications-bell-count').show(); $('#unread-notifications-bell-count').show();
// Change the favicon to show there are notifications // Change the favicon to show there are notifications
$('#favicon').attr('href', 'img/NetAlertX_logo_notification.png'); $('#favicon').attr('href', 'img/NetAlertX_logo_notification.png');
// Update the title to include the count // Update the title to include the count
document.title = `(${count}) ` + originalTitle; document.title = `(${count}) ` + originalTitle;
} else { } else {
$('#unread-notifications-bell-count').hide(); $('#unread-notifications-bell-count').hide();
// Change the favicon back to the original // Change the favicon back to the original
$('#favicon').attr('href', 'img/NetAlertX_logo.png'); $('#favicon').attr('href', 'img/NetAlertX_logo.png');
// Revert the title to the original title // Revert the title to the original title
document.title = originalTitle; document.title = originalTitle;
} }
} }
// Store the original title of the document // Store the original title of the document
@@ -392,73 +393,73 @@ const phpEndpoint = 'php/server/utilNotification.php';
// -------------------------------------------------- // --------------------------------------------------
// Write a notification // Write a notification
function write_notification(content, level) { function write_notification(content, level) {
$.ajax({ $.ajax({
url: phpEndpoint, // Change this to the path of your PHP script url: phpEndpoint, // Change this to the path of your PHP script
type: 'GET', type: 'GET',
data: { data: {
action: 'write_notification', action: 'write_notification',
content: content, content: content,
level: level level: level
}, },
success: function(response) { success: function(response) {
console.log('Notification written successfully.'); console.log('Notification written successfully.');
}, },
error: function(xhr, status, error) { error: function(xhr, status, error) {
console.error('Error writing notification:', error); console.error('Error writing notification:', error);
} }
}); });
} }
// -------------------------------------------------- // --------------------------------------------------
// Write a notification // Write a notification
function markNotificationAsRead(guid) { function markNotificationAsRead(guid) {
$.ajax({ $.ajax({
url: phpEndpoint, url: phpEndpoint,
type: 'GET', type: 'GET',
data: { data: {
action: 'mark_notification_as_read', action: 'mark_notification_as_read',
guid: guid guid: guid
}, },
success: function(response) { success: function(response) {
console.log(response); console.log(response);
// Perform any further actions after marking the notification as read here // Perform any further actions after marking the notification as read here
showMessage(getString("Gen_Okay")) showMessage(getString("Gen_Okay"))
}, },
error: function(xhr, status, error) { error: function(xhr, status, error) {
console.error("Error marking notification as read:", status, error); console.error("Error marking notification as read:", status, error);
}, },
complete: function() { complete: function() {
// Perform any cleanup tasks here // Perform any cleanup tasks here
} }
}); });
} }
// -------------------------------------------------- // --------------------------------------------------
// Remove a notification // Remove a notification
function removeNotification(guid) { function removeNotification(guid) {
$.ajax({ $.ajax({
url: phpEndpoint, url: phpEndpoint,
type: 'GET', type: 'GET',
data: { data: {
action: 'remove_notification', action: 'remove_notification',
guid: guid guid: guid
}, },
success: function(response) { success: function(response) {
console.log(response); console.log(response);
// Perform any further actions after marking the notification as read here // Perform any further actions after marking the notification as read here
showMessage(getString("Gen_Okay")) showMessage(getString("Gen_Okay"))
}, },
error: function(xhr, status, error) { error: function(xhr, status, error) {
console.error("Error removing notification:", status, error); console.error("Error removing notification:", status, error);
}, },
complete: function() { complete: function() {
// Perform any cleanup tasks here // Perform any cleanup tasks here
} }
}); });
} }