From b84683e1a9221fe4bd11391faedf97a847f335fc Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 26 Oct 2023 00:11:15 +0200 Subject: [PATCH] Rename functions for clarity Signed-off-by: DL6ER --- scripts/pi-hole/js/settings-api.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/scripts/pi-hole/js/settings-api.js b/scripts/pi-hole/js/settings-api.js index 873fa5bc..e498f297 100644 --- a/scripts/pi-hole/js/settings-api.js +++ b/scripts/pi-hole/js/settings-api.js @@ -61,7 +61,7 @@ $(function () { }, ], drawCallback: function () { - $('button[id^="deleteSession_"]').on("click", deleteSession); + $('button[id^="deleteSession_"]').on("click", deleteThisSession); // Hide buttons if all messages were deleted var hasRows = this.api().rows({ filter: "applied" }).data().length > 0; @@ -149,7 +149,7 @@ $(function () { ids.push(parseInt($(this).attr("data-id"), 10)); }); // Delete all selected rows at once - delSessions(ids); + deleteMultipleSessions(ids); }, }, ], @@ -187,13 +187,18 @@ $(function () { }); }); -function deleteSession() { - // Passes the button data-del-id attribute as ID - var ids = [$(this).attr("data-del-id")]; - delSessions(ids); +function deleteThisSession() { + // This function is called when a red trash button is clicked + // We get the ID of the current item from the data-del-id attribute + const thisID = parseInt(this.attr("data-del-id"), 10); + deleted = 0; + deleteOneSession(thisID, 1, false); } -function delSessions(ids) { +function deleteMultipleSessions(ids) { + // This function is called when multiple sessions are selected and the gray + // trash button is clicked + // Check input validity if (!Array.isArray(ids)) return; @@ -223,11 +228,16 @@ function delSessions(ids) { // Loop through IDs and delete them deleted = 0; for (const id of ids) { - delSession(id, ids.length, ownSessionDelete); + deleteOneSession(id, ids.length, ownSessionDelete); } } -function delSession(id, len, ownSessionDelete) { +function deleteOneSession(id, len, ownSessionDelete) { + // This function is called to delete a single session + // If we are batch deleting, we ensure that we do not delete our own session + // before having successfully deleted all other sessions, the deletion of + // our own session is then triggered by the last successful deletion of + // another session (ownSessionDelete == true, len == global deleted) $.ajax({ url: "/api/auth/session/" + id, method: "DELETE", @@ -237,7 +247,7 @@ function delSession(id, len, ownSessionDelete) { if (++deleted < len) return; // All other sessions have been deleted, now delete own session - if (ownSessionDelete) delSession(ownSessionID, 1, false); + if (ownSessionDelete) deleteOneSession(ownSessionID, 1, false); if (id !== ownSessionID) { // Reload table to remove session