From 279bc8528bf1afaa332ff3a20ffa65b874ff8f8e Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Sun, 28 Jun 2020 11:42:20 -0500 Subject: [PATCH] init form functionality --- src/public/JuiceUI/Libraries/js/app.js | 77 ++++++++++++++++---------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/src/public/JuiceUI/Libraries/js/app.js b/src/public/JuiceUI/Libraries/js/app.js index dab8d52..f2bb260 100644 --- a/src/public/JuiceUI/Libraries/js/app.js +++ b/src/public/JuiceUI/Libraries/js/app.js @@ -1,59 +1,78 @@ -let currentDisplayMode = "dark"; +let currentDisplayMode = 'dark'; const updateDisplayModeIcons = () => { - document.querySelectorAll(".display-mode-icon").forEach((el) => { - if (currentDisplayMode == "dark") { - el.innerHTML = ``; - } else if (currentDisplayMode == "moonlight") { - el.innerHTML = ``; + document.querySelectorAll('.display-mode-icon').forEach((el) => { + if (currentDisplayMode == 'dark') { + el.innerHTML = ``; + } else if (currentDisplayMode == 'moonlight') { + el.innerHTML = ``; } else { - el.innerHTML = ``; + el.innerHTML = ``; } }); }; const toggleDisplayMode = () => { - if (currentDisplayMode == "dark") { - window.localStorage.setItem("displayMode", "moonlight"); - currentDisplayMode = "moonlight"; - document.getElementById("themekit-css").href = "./Libraries/sass/themekit/main-moonlight.css"; - } else if (currentDisplayMode == "moonlight") { - window.localStorage.setItem("displayMode", "light"); - currentDisplayMode = "light"; - document.getElementById("themekit-css").href = "./Libraries/sass/themekit/main-light.css"; + if (currentDisplayMode == 'dark') { + window.localStorage.setItem('displayMode', 'moonlight'); + currentDisplayMode = 'moonlight'; + document.getElementById('themekit-css').href = '/JuiceUI/Libraries/sass/themekit/main-moonlight.css'; + } else if (currentDisplayMode == 'moonlight') { + window.localStorage.setItem('displayMode', 'light'); + currentDisplayMode = 'light'; + document.getElementById('themekit-css').href = '/JuiceUI/Libraries/sass/themekit/main-light.css'; } else { - window.localStorage.setItem("displayMode", "dark"); - currentDisplayMode = "dark"; - document.getElementById("themekit-css").href = "./Libraries/sass/themekit/main-dark.css"; + window.localStorage.setItem('displayMode', 'dark'); + currentDisplayMode = 'dark'; + document.getElementById('themekit-css').href = '/JuiceUI/Libraries/sass/themekit/main-dark.css'; } updateDisplayModeIcons(); }; -window.addEventListener("DOMContentLoaded", (evt) => { - if (window.localStorage.getItem("displayMode") == null) { +const initForm = () => { + const form = document.forms[0]; + const urlIn = document.getElementById('url'); + form.addEventListener('submit', async (evt) => { + evt.preventDefault(); + try { + const resp = await fetch(`/add?url=${urlIn.value}`, { + method: 'POST', + }) + const json = await resp.json(); + urlIn.value =`${location.origin}/${json.shortid}` + } catch (ex) { + alert(`There was an error processing your request: ${ex}`) + console.error(ex); + } + }); +} + +window.addEventListener('DOMContentLoaded', (evt) => { + if (window.localStorage.getItem('displayMode') == null) { // Match with system dark mode if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { - window.localStorage.setItem("displayMode", "dark"); + window.localStorage.setItem('displayMode', 'dark'); } // Match with system light mode else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) { - window.localStorage.setItem("displayMode", "light"); + window.localStorage.setItem('displayMode', 'light'); } // Literally choose the middle option else { - window.localStorage.setItem("displayMode", "moonlight"); + window.localStorage.setItem('displayMode', 'moonlight'); } } - currentDisplayMode = window.localStorage.getItem("displayMode"); + currentDisplayMode = window.localStorage.getItem('displayMode'); - if (currentDisplayMode == "dark") { - document.getElementById("themekit-css").href = "./Libraries/sass/themekit/main-dark.css"; - } else if (currentDisplayMode == "moonlight") { - document.getElementById("themekit-css").href = "./Libraries/sass/themekit/main-moonlight.css"; + if (currentDisplayMode == 'dark') { + document.getElementById('themekit-css').href = '/JuiceUI/Libraries/sass/themekit/main-dark.css'; + } else if (currentDisplayMode == 'moonlight') { + document.getElementById('themekit-css').href = '/JuiceUI/Libraries/sass/themekit/main-moonlight.css'; } else { - document.getElementById("themekit-css").href = "./Libraries/sass/themekit/main-light.css"; + document.getElementById('themekit-css').href = '/JuiceUI/Libraries/sass/themekit/main-light.css'; } updateDisplayModeIcons(); + initForm(); }); \ No newline at end of file