From fcd4751cb7ee29fdc2892f22b03a083e4b0247b3 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 24 Apr 2020 12:01:56 +0200 Subject: [PATCH] Implement selective undoing using the Shift key. Signed-off-by: DL6ER --- scripts/pi-hole/js/queries.js | 67 ++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/scripts/pi-hole/js/queries.js b/scripts/pi-hole/js/queries.js index d4328708..9bf65f94 100644 --- a/scripts/pi-hole/js/queries.js +++ b/scripts/pi-hole/js/queries.js @@ -508,8 +508,7 @@ $(document).ready(function() { }); }); -function addFilteringHint(obj, text) -{ +function addFilteringHint(obj, text) { if (autofilter()) { obj.title = "Click to show only queries " + text; obj.style.color = "#72afd2"; @@ -527,35 +526,57 @@ function addColumnFilter(event, colID, filterstring) { return; } - // Do nothing in case of a requested multi-selection - if (!event.ctrlKey && !event.metaKey) { + // Reset other columns when NOT requesting multi-selection functions + if (!event.ctrlKey && !event.metaKey && !event.shiftKey) { resetColumnsFilters(); } + if (event.shiftKey) { + filterstring = ""; + } + tableFilters[colID] = filterstring; - // Apply filtering - tableApi - .column(colID) - .search("^" + filterstring + "$", true, true) - .draw(); - - // Apply background color - tableApi.$("td:eq(" + colID + ")") - .css("background-color", colHighlightColor); - showResetButton(); + applyColumnFiltering(); } function resetColumnsFilters() { tableFilters.forEach(function(value, index) { tableFilters[index] = ""; - tableApi.column(index).search("", true, true); - tableApi.$("td:eq(" + index + ")") - .css("background-color", "#fff"); }); // Clear filter reset button - hideResetButton(); + applyColumnFiltering(); +} + +function applyColumnFiltering() { + var showReset = false; + tableFilters.forEach(function(value, index) { + // Prepare regex filter string + var regex = ""; + if (value.length > 0) { + regex = "^" + value + "$"; + + // Add background color + tableApi.$("td:eq(" + index + ")").css("background-color", colHighlightColor); + + // Remember to show reset button + showReset = true; + } else { + // Clear background color + tableApi.$("td:eq(" + index + ")").css("background-color", "#fff"); + } + + // Apply filtering on this column (regex may be empty -> no filtering) + tableApi.column(index).search(regex, true, true); + }); + + if (showReset) { + showResetButton(); + } else { + hideResetButton(); + } + // Trigger table update tableApi.draw(); } @@ -566,12 +587,10 @@ function showResetButton() { var button = $("#resetButton"); var text = ""; tableFilters.forEach(function(value, index) { - if (value.length > 0) { - if (text.length === 0) { - text = 'Clear filtering on ' + colTypes[index] + ' "' + value + '"'; - } else { - text += ' and ' + colTypes[index] + ' "' + value + '"'; - } + if (value.length > 0 && text.length === 0) { + text = "Clear filtering on " + colTypes[index] + ' "' + value + '"'; + } else if (value.length > 0) { + text += " and " + colTypes[index] + ' "' + value + '"'; } });