diff options
author | n-peugnet <n.peugnet@free.fr> | 2020-04-28 18:15:46 +0200 |
---|---|---|
committer | n-peugnet <n.peugnet@free.fr> | 2020-04-28 18:15:46 +0200 |
commit | 3183ce9dd42211b7279c7ce545c414452f5a9ef0 (patch) | |
tree | 445b0abaf9404a1449fa9f55d52fd42d49826642 | |
parent | 5b1457835be6c00a7d108537ecc88d3d85d48d51 (diff) | |
download | wcms-3183ce9dd42211b7279c7ce545c414452f5a9ef0.tar.gz wcms-3183ce9dd42211b7279c7ce545c414452f5a9ef0.zip |
refactor: clean js code with "activate" functions
-rw-r--r-- | src/fn/fn.js | 29 | ||||
-rw-r--r-- | src/home.js | 21 | ||||
-rw-r--r-- | 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; @@ -16,10 +16,28 @@ 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) { @@ -30,6 +48,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); |