From 5e662b856baff0714784a91551ef961cd5214542 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 5 Sep 2021 14:01:13 +0200 Subject: Separate HTML and Javascript --- js/xengineering.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 js/xengineering.js (limited to 'js') diff --git a/js/xengineering.js b/js/xengineering.js new file mode 100644 index 0000000..9d10026 --- /dev/null +++ b/js/xengineering.js @@ -0,0 +1,50 @@ +// vim: shiftwidth=2 tabstop=2 expandtab + +// define menu state +const MenuState = { + Desktop: "Desktop", + MobileCollapsed: "MobileCollapsed", + MobileExpanded: "MobileExpanded" +} +var state; + +// init menu state and add mediaChange handler +var isMobile = window.matchMedia("(max-width: 767px)"); +mediaChange(isMobile); +isMobile.addListener(mediaChange) + +function mediaChange(isMobile) { + if (isMobile.matches) { + state = MenuState.MobileCollapsed; + handleState(state); + } else { + state = MenuState.Desktop; + handleState(state); + } +} + +function toggleMenu() { + switch (state) { + case MenuState.MobileCollapsed: + state = MenuState.MobileExpanded; + handleState(state); + break; + case MenuState.MobileExpanded: + state = MenuState.MobileCollapsed; + handleState(state); + break; + } +} + +function handleState(state) { + var items = document.getElementsByTagName("a"); + for(var i=0; i