import { globalState } from '../../app/global-state'; export default class CheckboxUtils { private static shiftClickBound: boolean = false; static bindTableShiftClick() { if (CheckboxUtils.shiftClickBound) { return; } CheckboxUtils.shiftClickBound = true; if (!globalState.windowExists) { return; } const INPUT_SELECTOR = '.dt-selection-checkbox input[type="checkbox"]'; document.addEventListener('click', (e) => { if (!(e as MouseEvent).shiftKey) { return; } const targetEl = (e.target as HTMLElement | null)?.closest('.dt-selection-checkbox .inv-chckb-clickable') as HTMLElement | null; if (targetEl == null) { return; } let rootSelector = '.dt-item'; if (targetEl.closest(rootSelector) == null) { rootSelector = '.dt-mobile-row'; } const checkbox = targetEl.parentElement?.querySelector('input'); if (checkbox == null) { return; } const willBeChecked = !checkbox.checked; const rangeState = !willBeChecked; const chbRange: HTMLInputElement[] = [checkbox]; const shouldMoveUp = willBeChecked; let currRow = targetEl.closest(rootSelector) as HTMLElement | null; while (currRow != null) { currRow = (shouldMoveUp ? currRow.previousElementSibling : currRow.nextElementSibling) as HTMLElement | null; if (currRow == null) { break; } const rowInput = currRow.querySelector(INPUT_SELECTOR); if (rowInput == null) { break; } if (rowInput.checked == rangeState) { chbRange.push(rowInput); } else { break; } } chbRange.forEach((chbItem) => { chbItem.checked = willBeChecked; }); e.preventDefault(); e.stopImmediatePropagation(); e.stopPropagation(); }); } }