|
| 1 | +/** |
| 2 | + * ZnetDK, Starter Web Application for rapid & easy development |
| 3 | + * See official website https://mobile.znetdk.fr |
| 4 | + * Copyright (C) 2025 Pascal MARTINEZ (contact@znetdk.fr) |
| 5 | + * License GNU GPL https://www.gnu.org/licenses/gpl-3.0.html GNU GPL |
| 6 | + * -------------------------------------------------------------------- |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + * -------------------------------------------------------------------- |
| 18 | + * Toggle menu module JS library |
| 19 | + * |
| 20 | + * File version: 1.0 |
| 21 | + * Last update: 07/27/2025 |
| 22 | + */ |
| 23 | + |
| 24 | +// Once the document is fully loaded |
| 25 | +window.addEventListener("load", function() { |
| 26 | + if (!z4m.authentication.isRequired()) { |
| 27 | + new Z4M_ToggleMenu(); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +/* global z4m */ |
| 32 | +class Z4M_ToggleMenu { |
| 33 | + #logoutLink |
| 34 | + #logoutButton |
| 35 | + #menuButton |
| 36 | + #sideMenuEl |
| 37 | + #sideMenuElWidth |
| 38 | + #adjustCenteringEls |
| 39 | + #workingAreaEl |
| 40 | + #footerContentNotLarge |
| 41 | + #workingAreaMarginLeft |
| 42 | + #mediaQueryObj |
| 43 | + #largeViewPortWidth = '993px' |
| 44 | + #localStorageisVisibleKey = 'Z4M_ToggleMenu_isVisible' |
| 45 | + constructor() { |
| 46 | + this.#logoutLink = document.getElementById('zdk-logout'); |
| 47 | + this.#logoutButton = document.querySelector('#zdk-userpanel-modal button.logout'); |
| 48 | + this.#menuButton = document.getElementById('zdk-side-nav-button'); |
| 49 | + this.#sideMenuEl = document.getElementById('zdk-side-nav-menu'); |
| 50 | + this.#workingAreaEl = document.querySelector('.w3-main'); |
| 51 | + this.#adjustCenteringEls = document.querySelectorAll('.adjust-centering'); |
| 52 | + this.#footerContentNotLarge = document.querySelector('#zdk-footer > div:first-of-type'); |
| 53 | + |
| 54 | + this.#sideMenuElWidth = this.#sideMenuEl.style.width; |
| 55 | + this.#workingAreaMarginLeft = this.#workingAreaEl.style.marginLeft; |
| 56 | + |
| 57 | + this.#mediaQueryObj = window.matchMedia('(min-width: ' + this.#largeViewPortWidth + ')'); |
| 58 | + this.#adjustMenuButtonPosition(); |
| 59 | + |
| 60 | + this.#setMenuButtonAlwaysVisible(); |
| 61 | + this.#handleMenuButtonClickEvents(); |
| 62 | + this.#handleLogoutEvents(); |
| 63 | + this.#handleViewPortWidthChange(); |
| 64 | + this.#applyMemorizedState(); |
| 65 | + } |
| 66 | + #isLargeViewPort() { |
| 67 | + return this.#mediaQueryObj ? this.#mediaQueryObj.matches : false; |
| 68 | + } |
| 69 | + #setMenuButtonAlwaysVisible() { |
| 70 | + const largeTitleEl = document.querySelector('#zdk-header .banner-title-large'); |
| 71 | + largeTitleEl.style.width = 'calc(100% - 50px)'; |
| 72 | + this.#menuButton.classList.remove('w3-hide-large'); |
| 73 | + this.#menuButton.style.position = 'relative'; |
| 74 | + } |
| 75 | + #adjustMenuButtonPosition() { |
| 76 | + this.#menuButton.style.top = this.#isLargeViewPort() ? '4px' : '0'; |
| 77 | + } |
| 78 | + #handleMenuButtonClickEvents() { |
| 79 | + const $this = this; |
| 80 | + this.#menuButton.addEventListener('click', function(){ |
| 81 | + $this.#toggle(); |
| 82 | + }); |
| 83 | + } |
| 84 | + #handleViewPortWidthChange() { |
| 85 | + const $this = this; |
| 86 | + this.#mediaQueryObj.addEventListener('change', function() { |
| 87 | + $this.#adjustMenuButtonPosition(); |
| 88 | + }); |
| 89 | + } |
| 90 | + #handleLogoutEvents() { |
| 91 | + const $this = this; |
| 92 | + this.#logoutLink.addEventListener('click', hideMenuButton); |
| 93 | + this.#logoutButton.addEventListener('click', hideMenuButton); |
| 94 | + function hideMenuButton() { |
| 95 | + $this.#menuButton.classList.add('w3-hide-large'); |
| 96 | + } |
| 97 | + } |
| 98 | + #memorizeState(isVisible) { |
| 99 | + z4m.browser.storeLocalData(this.#localStorageisVisibleKey, isVisible ? '1' : '0'); |
| 100 | + } |
| 101 | + #applyMemorizedState() { |
| 102 | + const isVisible = z4m.browser.readLocalData(this.#localStorageisVisibleKey); |
| 103 | + if (isVisible === '0') { |
| 104 | + this.#toggle(); |
| 105 | + } |
| 106 | + } |
| 107 | + #toggle() { |
| 108 | + if (this.#isLargeViewPort() |
| 109 | + && this.#workingAreaEl.style.marginLeft === this.#workingAreaMarginLeft) { |
| 110 | + // Is visible so is set hidden |
| 111 | + this.#sideMenuEl.style.width = '0'; |
| 112 | + this.#workingAreaEl.style.marginLeft = '0'; |
| 113 | + this.#adjustCenteringEls.forEach((el) => { |
| 114 | + el.classList.add('w3-hide-large'); |
| 115 | + }); |
| 116 | + this.#footerContentNotLarge.classList.remove('w3-hide-large'); |
| 117 | + this.#triggerCustomEvent(false); |
| 118 | + this.#memorizeState(false); |
| 119 | + } else { |
| 120 | + // Is hidden so is set visible |
| 121 | + this.#sideMenuEl.style.width = this.#sideMenuElWidth; |
| 122 | + this.#workingAreaEl.style.marginLeft = this.#workingAreaMarginLeft; |
| 123 | + this.#adjustCenteringEls.forEach((el) => { |
| 124 | + el.classList.remove('w3-hide-large'); |
| 125 | + }); |
| 126 | + this.#footerContentNotLarge.classList.add('w3-hide-large'); |
| 127 | + if (this.#isLargeViewPort()) { |
| 128 | + this.#triggerCustomEvent(true); |
| 129 | + } |
| 130 | + this.#memorizeState(true); |
| 131 | + } |
| 132 | + } |
| 133 | + #triggerCustomEvent(isMenuVisible) { |
| 134 | + const customEvent = new CustomEvent("z4mtogglemenu", { |
| 135 | + detail: { |
| 136 | + bubbles: true, |
| 137 | + isMenuVisible: isMenuVisible |
| 138 | + } |
| 139 | + }); |
| 140 | + return this.#sideMenuEl.dispatchEvent(customEvent); |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | + |
0 commit comments