feat(check-all): added shift click functionality

This commit is contained in:
Johannes Eder 2021-09-17 17:59:42 +02:00
parent 7d7c376622
commit da1c8b5451

View File

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