Files
VITEC-website/packages/vitec/Resources/Public/Javascript/wp-import.js

46 lines
1.2 KiB
JavaScript

/**
* Select-all for the WordPress import list.
*
* Lives in a module rather than in an onclick attribute: the backend enforces a
* Content Security Policy that blocks inline event handlers, so the attribute
* version simply did nothing.
*
* Also keeps the header box honest - it shows indeterminate while only some
* rows are ticked, which matters on a list of a hundred posts.
*/
function boxes() {
return Array.from(document.querySelectorAll('[data-vitec-post]'));
}
function sync(toggle) {
const all = boxes();
const checked = all.filter((box) => box.checked).length;
toggle.checked = all.length > 0 && checked === all.length;
toggle.indeterminate = checked > 0 && checked < all.length;
}
function init() {
const toggle = document.querySelector('[data-vitec-toggle-all]');
if (toggle === null) {
return;
}
toggle.addEventListener('change', () => {
const state = toggle.checked;
boxes().forEach((box) => {
box.checked = state;
});
toggle.indeterminate = false;
});
boxes().forEach((box) => box.addEventListener('change', () => sync(toggle)));
sync(toggle);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}