summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2021-09-05 14:01:13 +0200
committerxengineering <me@xengineering.eu>2021-09-05 14:01:13 +0200
commit5e662b856baff0714784a91551ef961cd5214542 (patch)
tree27b70ee121239d113387301e5f9ee4d4ee854109
parent437bf22078af95f4576fec452bcbc88c1a9a09ec (diff)
downloadlibweb-5e662b856baff0714784a91551ef961cd5214542.tar
libweb-5e662b856baff0714784a91551ef961cd5214542.tar.zst
libweb-5e662b856baff0714784a91551ef961cd5214542.zip
Separate HTML and Javascript
-rw-r--r--html/template.html53
-rw-r--r--js/xengineering.js50
2 files changed, 51 insertions, 52 deletions
diff --git a/html/template.html b/html/template.html
index 080c07d..477539e 100644
--- a/html/template.html
+++ b/html/template.html
@@ -51,58 +51,7 @@ print("This is just some example code!")
</a>
</main>
- <script>
- // 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) {
- console.log("State is now " + state)
-
- var items = document.getElementsByTagName("a");
- for(var i=0; i<items.length; i++) {
- if (items[i].parentNode.tagName == "NAV") {
- if (state == MenuState.MobileCollapsed) {
- items[i].style.display = "none";
- } else {
- items[i].style.display = "block";
- }
- }
- }
- }
- </script>
+ <script src="../js/xengineering.js"></script>
</body>
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<items.length; i++) {
+ if (items[i].parentNode.tagName == "NAV") {
+ if (state == MenuState.MobileCollapsed) {
+ items[i].style.display = "none";
+ } else {
+ items[i].style.display = "block";
+ }
+ }
+ }
+}