[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. * Toggle all descendant checkboxes of a target.
* *
* Use `data-target="ID_OF_TARGET"` on the node being listened to. * 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
* @param {Event} event - change Event * ancestor for the checkboxes.
* @return {undefined}
*/ */
function toggleAllCheckboxes(event) { (function() {
const mainCheckbox = event.target; 'use strict';
function toggleAllCheckboxes(event) {
const mainCheckbox = event.target;
document
.querySelectorAll(`#${mainCheckbox.dataset.target} [type="checkbox"]`)
.forEach(checkbox => {checkbox.checked = mainCheckbox.checked;});
}
document document
.querySelectorAll(`#${mainCheckbox.dataset.target} [type="checkbox"]`) .querySelectorAll('[data-action="toggle-all"]')
.forEach(checkbox => {checkbox.checked = mainCheckbox.checked;}); .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 // set up javascript listeners
window.onload = function() { window.onload = function() {
@ -36,13 +36,6 @@ window.onload = function() {
// update localstorage // update localstorage
Array.from(document.getElementsByClassName('set-display')) Array.from(document.getElementsByClassName('set-display'))
.forEach(t => t.onclick = updateDisplay); .forEach(t => t.onclick = updateDisplay);
// Toggle all checkboxes.
document
.querySelectorAll('[data-action="toggle-all"]')
.forEach(input => {
input.addEventListener('change', toggleAllCheckboxes);
});
}; };
function back(e) { function back(e) {