forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistUnitSelect.js
More file actions
47 lines (38 loc) · 1.41 KB
/
listUnitSelect.js
File metadata and controls
47 lines (38 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Select unit behavior
export default function handleListUnitSelect() {
const checkboxes = Array.from(document.querySelectorAll('.js-unit-checkbox'));
const units = checkboxes.map((checkbox) => checkbox.closest('.js-unit'));
const selectAllCheckbox = document.querySelector('.js-toggle-all-checkbox');
if (checkboxes.length === 0 || !selectAllCheckbox) {
return;
}
let lastCheckedIndex = null;
checkboxes.forEach((checkbox, index) => {
checkbox.addEventListener('click', (event) => {
const isChecked = checkbox.checked;
updateUnitSelection(units[index], isChecked);
if (event.shiftKey && lastCheckedIndex !== null) {
handleMultiSelect(checkboxes, units, index, lastCheckedIndex, isChecked);
}
lastCheckedIndex = index;
});
});
selectAllCheckbox.addEventListener('change', () => {
const isChecked = selectAllCheckbox.checked;
checkboxes.forEach((checkbox) => {
checkbox.checked = isChecked;
});
units.forEach((unit) => updateUnitSelection(unit, isChecked));
});
}
function updateUnitSelection(unit, isChecked) {
unit.classList.toggle('selected', isChecked);
}
function handleMultiSelect(checkboxes, units, index, lastCheckedIndex, isChecked) {
const rangeStart = Math.min(index, lastCheckedIndex);
const rangeEnd = Math.max(index, lastCheckedIndex);
for (let i = rangeStart; i <= rangeEnd; i++) {
checkboxes[i].checked = isChecked;
updateUnitSelection(units[i], isChecked);
}
}