From da1c8b54510ee1436fefe97ba32372a08299b83e Mon Sep 17 00:00:00 2001 From: Johannes Eder Date: Fri, 17 Sep 2021 17:59:42 +0200 Subject: [PATCH] feat(check-all): added shift click functionality --- frontend/src/utils/check-all/check-all.js | 36 ++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/frontend/src/utils/check-all/check-all.js b/frontend/src/utils/check-all/check-all.js index b9796eeb5..e1bef2b09 100644 --- a/frontend/src/utils/check-all/check-all.js +++ b/frontend/src/utils/check-all/check-all.js @@ -18,6 +18,8 @@ export class CheckAll { _tableIndices; + _lastCheckedCell = null; + constructor(element, app) { if (!element) { throw new Error('Check All utility cannot be setup without an element!'); @@ -35,13 +37,44 @@ export class CheckAll { if (DEBUG_MODE > 0) console.log(this._columns); + + //Todo: 1 forEach loop - this._findCheckboxColumns().forEach(columnId => this._checkAllColumns.push(new CheckAllColumn(this._element, app, this._columns[columnId]))); + let checkboxColumns = this._findCheckboxColumns(); + + checkboxColumns.forEach(columnId => this._checkAllColumns.push(new CheckAllColumn(this._element, app, this._columns[columnId]))); + + checkboxColumns.forEach(columnId => { + let currentColumn = this._columns[columnId]; + currentColumn.forEach(el => el.addEventListener('click', (ev) => { + + if(ev.shiftKey && this.lastCheckedCell !== null) { + let lastClickedIndex = this._tableIndices.rowIndex(this._lastCheckedCell); + let currentCellIndex = this._tableIndices.rowIndex(el); + if(currentCellIndex > lastClickedIndex) + this._checkMultipleCells(lastClickedIndex, currentCellIndex, columnId); + else + this._checkMultipleCells(currentCellIndex, lastClickedIndex, columnId); + } else { + this._lastCheckedCell = el; + } + })); + + }); // mark initialized this._element.classList.add(CHECK_ALL_INITIALIZED_CLASS); } + _checkMultipleCells(firstRowIndex, lastRowIndex, columnId) { + for(let i=firstRowIndex; i<=lastRowIndex; i++) { + let cell = this._columns[columnId][i]; + if (cell.tagName !== 'TH') { + cell.querySelector(CHECKBOX_SELECTOR).checked = true; + } + } + } + _gatherColumns() { for (const rowIndex of Array(this._tableIndices.maxRow + 1).keys()) { for (const colIndex of Array(this._tableIndices.maxCol + 1).keys()) { @@ -97,6 +130,7 @@ class CheckAllColumn { this._checkAllCheckbox = document.createElement('input'); this._checkAllCheckbox.setAttribute('type', 'checkbox'); this._checkAllCheckbox.setAttribute('id', this._checkboxId); + th.insertBefore(this._checkAllCheckbox, th.firstChild); // set up new checkbox