[assets] Move toggleAllCheckboxes code to its own file.

This commit is contained in:
Fabien Basmaison 2021-04-05 16:16:05 +02:00
parent d25c68f887
commit 6971c9b133
2 changed files with 19 additions and 19 deletions

View file

@ -1,17 +1,24 @@
/* exported toggleAllCheckboxes */
/**
* Toggle all descendant checkboxes of a target.
*
* Use `data-target="ID_OF_TARGET"` on the node being listened to.
*
* @param {Event} event - change Event
* @return {undefined}
* Use `data-target="ID_OF_TARGET"` on the node on which the event is listened
* to (checkbox, button, link), where_ID_OF_TARGET_ should be the ID of an
* ancestor for the checkboxes.
*/
function toggleAllCheckboxes(event) {
const mainCheckbox = event.target;
(function() {
'use strict';
function toggleAllCheckboxes(event) {
const mainCheckbox = event.target;
document
.querySelectorAll(`#${mainCheckbox.dataset.target} [type="checkbox"]`)
.forEach(checkbox => {checkbox.checked = mainCheckbox.checked;});
}
document
.querySelectorAll(`#${mainCheckbox.dataset.target} [type="checkbox"]`)
.forEach(checkbox => {checkbox.checked = mainCheckbox.checked;});
}
.querySelectorAll('[data-action="toggle-all"]')
.forEach(input => {
input.addEventListener('change', toggleAllCheckboxes);
});
})();

View file

@ -1,4 +1,4 @@
/* globals setDisplay TabGroup toggleAllCheckboxes updateDisplay */
/* globals setDisplay TabGroup updateDisplay */
// set up javascript listeners
window.onload = function() {
@ -36,13 +36,6 @@ window.onload = function() {
// update localstorage
Array.from(document.getElementsByClassName('set-display'))
.forEach(t => t.onclick = updateDisplay);
// Toggle all checkboxes.
document
.querySelectorAll('[data-action="toggle-all"]')
.forEach(input => {
input.addEventListener('change', toggleAllCheckboxes);
});
};
function back(e) {