simplified selection algorithm

This commit is contained in:
Richard Schreiber
2021-02-19 07:33:01 +01:00
parent fb7d38ede0
commit c9f89dc920

View File

@@ -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);