Files
meetletacbfree/javascript/featureaccess_matrix.js
T

55 lines
1.9 KiB
JavaScript

/**
* MeetLet feature-access matrix: row/column "select all" toggles.
* Page-local (admin settings + mod form). Not AMD.
*/
(function() {
const bindMatrix = (matrix) => {
if (!matrix || matrix.getAttribute('data-access-bound') === '1') {
return;
}
matrix.setAttribute('data-access-bound', '1');
const setGroup = (checks, forceOn) => {
const list = Array.from(checks).filter((el) => !el.disabled);
if (!list.length) {
return;
}
const turnOn = (typeof forceOn === 'boolean')
? forceOn
: list.some((el) => !el.checked);
list.forEach((el) => {
if (el.checked !== turnOn) {
el.checked = turnOn;
el.dispatchEvent(new Event('change', {bubbles: true}));
}
});
};
matrix.addEventListener('click', (e) => {
const btn = e.target.closest('[data-access-toggle]');
if (!btn || !matrix.contains(btn) || matrix.classList.contains('is-disabled')) {
return;
}
e.preventDefault();
const mode = btn.getAttribute('data-access-toggle');
if (mode === 'row') {
const role = btn.getAttribute('data-role');
setGroup(matrix.querySelectorAll('.meetlet-access-check[data-role="' + role + '"]'));
} else if (mode === 'col') {
const feature = btn.getAttribute('data-feature');
setGroup(matrix.querySelectorAll('.meetlet-access-check[data-feature="' + feature + '"]'));
}
});
};
const init = () => {
document.querySelectorAll('[data-meetlet-access-matrix]').forEach(bindMatrix);
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();