From 95eb2973364dfcc7798fc4f6a30d5528d6c96b9b Mon Sep 17 00:00:00 2001 From: Malte Bublitz Date: Sun, 2 Oct 2022 21:57:42 +0200 Subject: [PATCH] =?utf8?q?Einige=20=C3=84nderungen=20(u.a.=20JavaScripts?= =?utf8?q?=20in=20/js)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- js/greeting.js | 137 ++++++++++++++++++++++++++++++++++++++++++ main.js => js/main.js | 23 ++++--- mini/greeting.js | 73 ---------------------- mini/index.html | 4 +- template.inc.php | 14 ++--- 5 files changed, 157 insertions(+), 94 deletions(-) create mode 100644 js/greeting.js rename main.js => js/main.js (91%) delete mode 100644 mini/greeting.js diff --git a/js/greeting.js b/js/greeting.js new file mode 100644 index 0000000..cb6beb8 --- /dev/null +++ b/js/greeting.js @@ -0,0 +1,137 @@ +"use strict"; + +var greeting = { + /** + * Configuration + * + * Keys: + * storageKey {string} Prefix for localStorage keys + * greetingSelector {string} Selector for the element containing the greeting + * defaultHidden {boolean} Hidden by default? + */ + config: { + storageKey: "greeting", + greetingSelector: "header h1", + defaultHidden: false, + }, + + /** + * Current greeting + */ + text: "", + + /** + * Wether the greeting is hidden + */ + hidden: false, + + /** + * Initialize greeting.js + */ + init: function(greetingSelector = null, storageKey = null, hidden = null){ + if (greetingSelector !== null) { + greeting.config.greetingSelector = greetingSelector; + } + if (storageKey !== null) { + greeting.config.storageKey = storageKey; + } + if (hidden !== null) { + greeting.config.defaultHidden = hidden; + } + + // Read storage + greeting.readStorage(); + + // Update/set greeting element's content + greeting.update(); + }, + + /** + * Get the HTMLElement matching config.greetingSelector + */ + getElem: function() { + return document.querySelector(greeting.config.greetingSelector); + }, + + /** + * Set greeting.text to the value in localStorage. + * If the storage entry did not exist, default to getElem()'s + * innerText + */ + readStorage: function(){ + greeting.text = localStorage.getItem(greeting.config.storageKey + "_text"); + if (greeting.text == null) { + // migrate old config + greeting.text = localStorage.getItem(greeting.config.storageKey); + if (greeting.text !== null) { + localStorage.setItem(greeting.config.storageKey + "_text", greeting.text); + } else { + // No greeting stored until now + greeting.text = greeting.getElem().innerText; + if (greeting.text.length < 1) { + greeting.text = "hello"; + } + } + } + + greeting.hidden = localStorage.getItem(greeting.config.storageKey + "_hidden"); + if (greeting.hidden == null) { + // No greeting stored until now + greeting.hidden = greeting.config.defaultHidden; + } + }, + + /** + * Set a new greeting + */ + set: function(newGreeting){ + if (newGreeting === null || newGreeting.length < 1) + return false; + + greeting.text = newGreeting; + //localStorage.setItem(greeting.config.storageKey, greeting.text); + greeting.update(); + }, + + /** + * Update the greeting HTML element by setting it + * to greeting.text, and store it in localStorage + */ + update: function(){ + greeting.getElem().innerText = greeting.text; + localStorage.setItem( + greeting.config.storageKey + "_text", + greeting.text + ); + localStorage.setItem( + greeting.config.storageKey + "_hidden", + greeting.hidden + ); + }, + + /** + * Hide the greeting + */ + hide: function(permanent = false){ + console.debug('greeting.hide(): Hiding "'+greeting.config.greetingSelector+'"'); + greeting.getElem().style.display = "none"; + greeting.getElem().style.visibility = "hidden"; + if (permanent === true) { + greeting.hidden = true; + greeting.update(); + } + }, + + /** + * Un-Hide a.k.a. show the greeting + */ + unHide: function(permanent = false){ + console.debug('greeting.unHide(): Showing "'+greeting.config.greetingSelector+'"'); + greeting.getElem().style.display = ""; + greeting.getElem().style.visibility = ""; + if (permanent === true) { + greeting.hidden = false; + greeting.update(); + } + }, +}; diff --git a/main.js b/js/main.js similarity index 91% rename from main.js rename to js/main.js index e7aced2..88b483d 100644 --- a/main.js +++ b/js/main.js @@ -35,7 +35,6 @@ window.whoami = { this.whoami_user = urlParams.get('user'); this.whoami_host = "PLUTO"; }*/ - }, deinit: function() { localStorage.removeItem("whoami_user"); @@ -45,8 +44,8 @@ window.whoami = { settings: function() { console.log("whoami.settings()"); this.whoami_user = prompt("Your user name", localStorage.getItem("whoami_user")); - this.whoami_host = prompt("Hostname", localStorage.getItem("whoami_host")); - this.whoami_format = prompt("Format (unix|windows)", localStorage.getItem("whoami_format")); + this.whoami_host = prompt("Hostname", localStorage.getItem("whoami_host")); + this.whoami_format = prompt("Format (unix|windows)", localStorage.getItem("whoami_format")); if (this.whoami_user === null) this.whoami_user = "malte70"; @@ -60,15 +59,15 @@ window.whoami = { if (this.whoami_format === null) this.whoami_format = "windows"; - console.table( - [ - ["whoami_user", this.whoami_user], - ["whoami_host", this.whoami_host], - ["whoami_format", this.whoami_format], - ] - ); - - this.update(); + console.table( + [ + ["whoami_user", this.whoami_user], + ["whoami_host", this.whoami_host], + ["whoami_format", this.whoami_format], + ] + ); + + this.update(); }, update: function() { // Update localstorage diff --git a/mini/greeting.js b/mini/greeting.js deleted file mode 100644 index 93fb2ce..0000000 --- a/mini/greeting.js +++ /dev/null @@ -1,73 +0,0 @@ -var greeting = { - config: { - storageKey: "greeting", - greetingSelector: "header h1" - }, - // current greeting - text: "", - - /** - * Initialize greeting.js - */ - init: function(greetingSelector = null, storageKey = null){ - if (greetingSelector !== null) { - greeting.config.greetingSelector = greetingSelector; - } - if (storageKey !== null) { - greeting.config.storageKey = storageKey; - } - - // Read storage - greeting.readStorage(); - - // Update/set greeting element's content - greeting.update(); - }, - - /** - * Get the HTMLElement matching config.greetingSelector - */ - getElem: function() { - return document.querySelector(greeting.config.greetingSelector); - }, - - /** - * Set greeting.text to the value in localStorage. - * If the storage entry did not exist, default to getElem()'s - * innerText - */ - readStorage: function(){ - greeting.text = localStorage.getItem(greeting.config.storageKey); - if (greeting.text == null) { - // No greeting stored until now - greeting.text = greeting.getElem().innerText; - if (greeting.text.length < 1) { - greeting.text = "hello"; - } - } - }, - - /** - * Set a new greeting - */ - set: function(newGreeting){ - if (newGreeting.length < 1) - return false; - - greeting.text = newGreeting; - //localStorage.setItem(greeting.config.storageKey, greeting.text); - greeting.update(); - }, - - /** - * Update the greeting HTML element by setting it - * to greeting.text, and store it in localStorage - */ - update: function(){ - greeting.getElem().innerText = greeting.text; - localStorage.setItem( - greeting.config.storageKey, - greeting.text - ); - }, -}; diff --git a/mini/index.html b/mini/index.html index 3c720aa..70a34fe 100644 --- a/mini/index.html +++ b/mini/index.html @@ -7,7 +7,7 @@ Startseite - + @@ -26,7 +26,7 @@ - + -- 2.30.2