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 +++++++++++++++++++++++++++-- src/home.js | 21 +++++++-------------- src/media.js | 18 +++++++----------- 3 files changed, 41 insertions(+), 27 deletions(-) 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 diff --git a/src/home.js b/src/home.js index 0778edb..a75ba1d 100644 --- a/src/home.js +++ b/src/home.js @@ -1,18 +1,11 @@ -import { checkallHandler, closeSubmenus, activateSelectAll } from './fn/fn'; +import { + activateCheckall, + activateCloseSubmenus, + activateSelectAll, +} from './fn/fn'; window.addEventListener('load', () => { - let checkboxes = document.getElementsByName('pagesid[]'); - let checkall = document.getElementById('checkall'); - if (!checkall) { - return; - } - let checkbox = document.createElement('input'); - checkbox.type = 'checkbox'; - checkbox.addEventListener('input', checkallHandler.bind({ checkboxes })); - checkall.innerHTML = ''; - checkall.appendChild(checkbox); - + activateCheckall('pagesid[]', 'checkall'); activateSelectAll(); + activateCloseSubmenus(); }); - -window.addEventListener('click', closeSubmenus); diff --git a/src/media.js b/src/media.js index 356c7c9..1073d23 100644 --- a/src/media.js +++ b/src/media.js @@ -1,15 +1,11 @@ -import { checkallHandler, closeSubmenus, activateSelectAll } from './fn/fn'; +import { + activateCheckall, + activateCloseSubmenus, + activateSelectAll, +} from './fn/fn'; window.addEventListener('load', () => { - let checkboxes = document.getElementsByName('id[]'); - let checkall = document.getElementById('checkall'); - let checkbox = document.createElement('input'); - checkbox.type = 'checkbox'; - checkbox.addEventListener('input', checkallHandler.bind({ checkboxes })); - checkall.innerHTML = ''; - checkall.appendChild(checkbox); - + activateCheckall('id[]', 'checkall'); activateSelectAll(); + activateCloseSubmenus(); }); - -window.addEventListener('click', closeSubmenus); -- cgit v1.2.3