2023-04-18 23:20:02 +02:00
|
|
|
import {Component} from './component';
|
2019-04-13 13:07:27 +02:00
|
|
|
|
2022-11-15 13:44:57 +01:00
|
|
|
export class PermissionsTable extends Component {
|
2019-04-13 13:07:27 +02:00
|
|
|
|
2022-10-02 14:57:32 +02:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
2022-11-03 14:28:07 +01:00
|
|
|
this.cellSelector = this.$opts.cellSelector || 'td,th';
|
|
|
|
this.rowSelector = this.$opts.rowSelector || 'tr';
|
2019-04-13 13:07:27 +02:00
|
|
|
|
|
|
|
// Handle toggle all event
|
2022-10-02 14:57:32 +02:00
|
|
|
for (const toggleAllElem of (this.$manyRefs.toggleAll || [])) {
|
|
|
|
toggleAllElem.addEventListener('click', this.toggleAllClick.bind(this));
|
|
|
|
}
|
2019-04-13 13:07:27 +02:00
|
|
|
|
|
|
|
// Handle toggle row event
|
2022-10-02 14:57:32 +02:00
|
|
|
for (const toggleRowElem of (this.$manyRefs.toggleRow || [])) {
|
2019-04-13 13:07:27 +02:00
|
|
|
toggleRowElem.addEventListener('click', this.toggleRowClick.bind(this));
|
|
|
|
}
|
2019-04-13 14:16:18 +02:00
|
|
|
|
|
|
|
// Handle toggle column event
|
2022-10-02 14:57:32 +02:00
|
|
|
for (const toggleColElem of (this.$manyRefs.toggleColumn || [])) {
|
2019-04-13 14:16:18 +02:00
|
|
|
toggleColElem.addEventListener('click', this.toggleColumnClick.bind(this));
|
|
|
|
}
|
2019-04-13 13:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleAllClick(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.toggleAllInElement(this.container);
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleRowClick(event) {
|
|
|
|
event.preventDefault();
|
2022-11-03 14:28:07 +01:00
|
|
|
this.toggleAllInElement(event.target.closest(this.rowSelector));
|
2019-04-13 13:07:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-13 14:16:18 +02:00
|
|
|
toggleColumnClick(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2022-11-03 14:28:07 +01:00
|
|
|
const tableCell = event.target.closest(this.cellSelector);
|
2019-04-13 14:16:18 +02:00
|
|
|
const colIndex = Array.from(tableCell.parentElement.children).indexOf(tableCell);
|
2022-11-03 14:28:07 +01:00
|
|
|
const tableRows = this.container.querySelectorAll(this.rowSelector);
|
2019-04-13 14:16:18 +02:00
|
|
|
const inputsToToggle = [];
|
|
|
|
|
2023-04-18 23:20:02 +02:00
|
|
|
for (const row of tableRows) {
|
2019-04-13 14:16:18 +02:00
|
|
|
const targetCell = row.children[colIndex];
|
|
|
|
if (targetCell) {
|
|
|
|
inputsToToggle.push(...targetCell.querySelectorAll('input[type=checkbox]'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.toggleAllInputs(inputsToToggle);
|
|
|
|
}
|
|
|
|
|
2019-04-13 13:07:27 +02:00
|
|
|
toggleAllInElement(domElem) {
|
2019-04-13 14:16:18 +02:00
|
|
|
const inputsToToggle = domElem.querySelectorAll('input[type=checkbox]');
|
|
|
|
this.toggleAllInputs(inputsToToggle);
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleAllInputs(inputsToToggle) {
|
|
|
|
const currentState = inputsToToggle.length > 0 ? inputsToToggle[0].checked : false;
|
2023-04-18 23:20:02 +02:00
|
|
|
for (const checkbox of inputsToToggle) {
|
2019-04-13 13:07:27 +02:00
|
|
|
checkbox.checked = !currentState;
|
|
|
|
checkbox.dispatchEvent(new Event('change'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 23:20:02 +02:00
|
|
|
}
|