diff --git a/scripts/pi-hole/js/messages.js b/scripts/pi-hole/js/messages.js index 15a415fd1..e97f5cfd6 100644 --- a/scripts/pi-hole/js/messages.js +++ b/scripts/pi-hole/js/messages.js @@ -6,7 +6,8 @@ * Please see LICENSE file for your rights under this license. */ /* global utils:false */ -var table; +var table, + toasts = {}; $(function () { var ignoreNonfatal = localStorage @@ -98,13 +99,10 @@ $(function () { className: "btn-sm datatable-bt deleteSelected", action: function () { // For each ".selected" row ... - var ids = []; $("tr.selected").each(function () { - // ... add the row identified by "data-id". - ids.push(parseInt($(this).attr("data-id"), 10)); + // ... delete the row identified by "data-id". + delMsg($(this).attr("data-id")); }); - // Delete all selected rows at once - delMsg(ids); }, }, ], @@ -150,26 +148,16 @@ $.fn.dataTable.Buttons.defaults.dom.container.className = "dt-buttons"; function deleteMessage() { // Passes the button data-del-id attribute as ID - var ids = [parseInt($(this).attr("data-del-id"), 10)]; - - // Check input validity - if (!Array.isArray(ids)) return; - - // Exploit prevention: Return early for non-numeric IDs - for (var id in ids) { - if (Object.hasOwnProperty.call(ids, id)) { - if (typeof ids[id] !== "number") return; - delMsg(ids); - } - } + delMsg($(this).attr("data-del-id")); } -function delMsg(ids) { +function delMsg(id) { + id = parseInt(id, 10); utils.disableAll(); - utils.showAlert("info", "", "Deleting message..."); + toasts[id] = utils.showAlert("info", "", "Deleting message...", "ID: " + id, null); $.ajax({ - url: "/api/info/messages/" + ids, + url: "/api/info/messages/" + id, method: "DELETE", }) .done(function (response) { @@ -178,19 +166,21 @@ function delMsg(ids) { utils.showAlert( "success", "far fa-trash-alt", - "Successfully deleted " + ids.length + " message" + (ids.length > 1 ? "s" : ""), - "" + "Successfully deleted message", + "ID: " + id, + toasts[id] ); - // Loop over id in case of multiple IDs - for (var id in ids) { - if (Object.hasOwnProperty.call(ids, id)) { - table.row(id).remove(); - } - } + table.row(id).remove(); table.draw(false).ajax.reload(null, false); } else { - utils.showAlert("error", "", "Error while deleting message: " + ids, response.message); + utils.showAlert( + "error", + "", + "Error while deleting message: " + id, + response.message, + toasts[id] + ); } // Clear selection after deletion @@ -202,7 +192,13 @@ function delMsg(ids) { ) .fail(function (jqXHR, exception) { utils.enableAll(); - utils.showAlert("error", "", "Error while deleting message: " + ids, jqXHR.responseText); + utils.showAlert( + "error", + "", + "Error while deleting message: " + id, + jqXHR.responseText, + toasts[id] + ); console.log(exception); // eslint-disable-line no-console }); } diff --git a/scripts/pi-hole/js/settings-dhcp.js b/scripts/pi-hole/js/settings-dhcp.js index 56b10ab4e..6e467668f 100644 --- a/scripts/pi-hole/js/settings-dhcp.js +++ b/scripts/pi-hole/js/settings-dhcp.js @@ -7,7 +7,8 @@ /* global utils:false, setConfigValues: false, apiFailure: false */ -var dhcpLeaesTable = null; +var dhcpLeaesTable = null, + toasts = {}; // DHCP leases tooltips $(function () { @@ -115,13 +116,10 @@ $(function () { className: "btn-sm datatable-bt deleteSelected", action: function () { // For each ".selected" row ... - var ids = []; $("tr.selected").each(function () { - // ... add the row identified by "data-id". - ids.push(parseInt($(this).attr("data-id"), 10)); + // ... delete the row identified by "data-id". + delLease($(this).attr("data-id")); }); - // Delete all selected rows at once - delLease(ids); }, }, ], @@ -161,34 +159,36 @@ $(function () { function deleteLease() { // Passes the button data-del-id attribute as IP - var ips = [$(this).attr("data-del-ip")]; - - // Check input validity - if (!Array.isArray(ips)) return; - - // Exploit prevention: Return early for non-numeric IDs - for (var ip in ips) { - if (Object.hasOwnProperty.call(ips, ip)) { - delLease(ips); - } - } + delLease($(this).attr("data-del-ip")); } function delLease(ip) { utils.disableAll(); - utils.showAlert("info", "", "Deleting lease..."); + toasts[ip] = utils.showAlert("info", "", "Deleting lease...", ip, null); $.ajax({ - url: "/api/dhcp/leases/" + ip, + url: "/api/dhcp/leases/" + encodeURIComponent(ip), method: "DELETE", }) .done(function (response) { utils.enableAll(); if (response === undefined) { - utils.showAlert("success", "far fa-trash-alt", "Successfully deleted lease", ""); + utils.showAlert( + "success", + "far fa-trash-alt", + "Successfully deleted lease", + ip, + toasts[ip] + ); dhcpLeaesTable.ajax.reload(null, false); } else { - utils.showAlert("error", "", "Error while deleting lease: " + ip, response.lease); + utils.showAlert( + "error", + "", + "Error while deleting lease: " + ip, + response.lease, + toasts[ip] + ); } // Clear selection after deletion @@ -197,7 +197,13 @@ function delLease(ip) { }) .fail(function (jqXHR, exception) { utils.enableAll(); - utils.showAlert("error", "", "Error while deleting lease: " + ip, jqXHR.responseText); + utils.showAlert( + "error", + "", + "Error while deleting lease: " + ip, + jqXHR.responseText, + toasts[ip] + ); console.log(exception); // eslint-disable-line no-console }); } diff --git a/scripts/pi-hole/js/settings-dns-records.js b/scripts/pi-hole/js/settings-dns-records.js index d664d8ee6..28f61de08 100644 --- a/scripts/pi-hole/js/settings-dns-records.js +++ b/scripts/pi-hole/js/settings-dns-records.js @@ -141,19 +141,8 @@ $(function () { }); function deleteRecord() { - // Get the tags - var tags = [$(this).attr("data-tag")]; - var types = [$(this).attr("data-type")]; - // Check input validity - if (!Array.isArray(tags)) return; - - // Exploit prevention: Return early for non-numeric IDs - for (var tag in tags) { - if (Object.hasOwnProperty.call(tags, tag)) { - if (types[0] === "hosts") delHosts(tags); - else delCNAME(tags); - } - } + if ($(this).attr("data-type") === "hosts") delHosts($(this).attr("data-tag")); + else delCNAME($(this).attr("data-tag")); } function delHosts(elem) { diff --git a/scripts/pi-hole/js/utils.js b/scripts/pi-hole/js/utils.js index 74bf07b6e..caca985b8 100644 --- a/scripts/pi-hole/js/utils.js +++ b/scripts/pi-hole/js/utils.js @@ -84,7 +84,7 @@ function padNumber(num) { } var showAlertBox = null; -function showAlert(type, icon, title, message) { +function showAlert(type, icon, title, message, toast) { const options = { title: " " + escapeHtml(title) + "
", message: escapeHtml(message), @@ -138,16 +138,28 @@ function showAlert(type, icon, title, message) { return; } - if (type === "info") { - // Create a new notification for info boxes - showAlertBox = $.notify(options, settings); - } else if (showAlertBox !== null) { - // Update existing notification for other boxes (if available) - showAlertBox.update(options); - showAlertBox.update(settings); + if (toast === undefined) { + if (type === "info") { + // Create a new notification for info boxes + showAlertBox = $.notify(options, settings); + return showAlertBox; + } else if (showAlertBox !== null) { + // Update existing notification for other boxes (if available) + showAlertBox.update(options); + showAlertBox.update(settings); + return showAlertBox; + } else { + // Create a new notification for other boxes if no previous info box exists + return $.notify(options, settings); + } + } else if (toast === null) { + // Always create a new toast + return $.notify(options, settings); } else { - // Create a new notification for other boxes if no previous info box exists - $.notify(options, settings); + // Update existing toast + toast.update(options); + toast.update(settings); + return toast; } }