From 3183ce9dd42211b7279c7ce545c414452f5a9ef0 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Tue, 28 Apr 2020 18:15:46 +0200 Subject: refactor: clean js code with "activate" functions --- src/fn/fn.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src/fn') diff --git a/src/fn/fn.js b/src/fn/fn.js index a5aa538..69cbe2a 100644 --- a/src/fn/fn.js +++ b/src/fn/fn.js @@ -3,7 +3,7 @@ * Call with .bind({checkboxes: HTMLElement[]}) * @param {InputEvent} e the input event */ -export function checkallHandler(e) { +function checkallHandler(e) { if (e.target.checked) { for (const checkbox of this.checkboxes) { checkbox.checked = true; @@ -15,11 +15,29 @@ export function checkallHandler(e) { } } +/** + * Activate the checkall feature + * @param {string} checkboxesName value of the name property of the desired checkbox elements. + * @param {string} checkallId value of the id property of the desired checkall element. + */ +export function activateCheckall(checkboxesName, checkallId) { + let checkboxes = document.getElementsByName(checkboxesName); + let checkall = document.getElementById(checkallId); + if (!checkall) { + return; + } + let checkbox = document.createElement('input'); + checkbox.type = 'checkbox'; + checkbox.addEventListener('input', checkallHandler.bind({ checkboxes })); + checkall.innerHTML = ''; + checkall.appendChild(checkbox); +} + /** * Close all submenus of the menubar. * @param {MouseEvent} e */ -export function closeSubmenus(e) { +function closeSubmenus(e) { let details = document.querySelectorAll('aside details'); let currentDetail = e.target.closest('details'); for (const detail of details) { @@ -29,6 +47,13 @@ export function closeSubmenus(e) { } } +/** + * Activate "close submenus" feature on click anywhere. + */ +export function activateCloseSubmenus() { + window.addEventListener('click', closeSubmenus); +} + /** * Select the whole content of the clicked item. * @param {MouseEvent} e -- cgit v1.2.3