From c9f89dc920088f367e53484b16e1e02c47325b2d Mon Sep 17 00:00:00 2001 From: Richard Schreiber Date: Fri, 19 Feb 2021 07:33:01 +0100 Subject: [PATCH] simplified selection algorithm --- src/pretix/static/pretixcontrol/js/ui/main.js | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/pretix/static/pretixcontrol/js/ui/main.js b/src/pretix/static/pretixcontrol/js/ui/main.js index e672b9271a..7c9069c77b 100644 --- a/src/pretix/static/pretixcontrol/js/ui/main.js +++ b/src/pretix/static/pretixcontrol/js/ui/main.js @@ -676,7 +676,7 @@ $(function () { var $table = $toggle.closest("table"); var $rows = $table.find("tbody tr"); var $checkboxes = $rows.find("td:first-child input[type=checkbox]"); - var firstIndex, lastIndex, currentIndex, selectionChecked, onChangeSelectionHappened = false; + var firstIndex, lastIndex, selectionChecked, onChangeSelectionHappened = false; var updateSelection = function(a, b, checked) { if (a > b) { //[a, b] = [b, a];// ES6 not ready yet for pretix @@ -695,29 +695,21 @@ $(function () { onChangeSelectionHappened = true; var row = ev.target.closest("tr"); - currentIndex = 0; + var currentIndex = 0; while(row = row.previousSibling) { if (row.tagName) currentIndex++; } - - if (currentIndex > firstIndex && currentIndex > lastIndex) { - // special case: selection direction changed - if (lastIndex < firstIndex) { - // reset all selected checkboxes before setting new lastIndex - updateSelection(lastIndex, firstIndex); - } - lastIndex = currentIndex; - - } else if (currentIndex < firstIndex && currentIndex < lastIndex) { - // special case: selection direction changed - if (lastIndex > firstIndex) { - // reset all selected checkboxes before setting new lastIndex - updateSelection(firstIndex, lastIndex); - } - lastIndex = currentIndex; + var dCurrent = currentIndex - firstIndex; + var dLast = lastIndex - firstIndex; + if (dCurrent*dLast < 0) { + // direction of selection changed => reset all previously selected + updateSelection(lastIndex, firstIndex); } - - if (currentIndex != lastIndex) updateSelection(currentIndex, lastIndex); + else if (Math.abs(dCurrent) < Math.abs(dLast)) { + // selection distance decreased => reset unselected + updateSelection(currentIndex, lastIndex); + } + lastIndex = currentIndex; updateSelection(firstIndex, currentIndex, selectionChecked); ev.preventDefault(); @@ -731,7 +723,7 @@ $(function () { while(row = row.previousSibling) { if (row.tagName) firstIndex++; } - lastIndex = currentIndex = firstIndex; + lastIndex = firstIndex; ev.preventDefault(); $rows.on("pointerenter", onChangeSelection);