From: Malte Bublitz Date: Sun, 2 Oct 2022 19:57:42 +0000 (+0200) Subject: Einige Änderungen (u.a. JavaScripts in /js) X-Git-Url: https://git.rt3x.de/?a=commitdiff_plain;h=95eb2973364dfcc7798fc4f6a30d5528d6c96b9b;p=startseite.malte70.de.git Einige Änderungen (u.a. JavaScripts in /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/js/main.js b/js/main.js new file mode 100644 index 0000000..88b483d --- /dev/null +++ b/js/main.js @@ -0,0 +1,138 @@ +window.whoami = { + init: function() { + console.log("whoami.init()"); + if (!this.section) { + this.section = document.getElementById("whoami") + } + + this.whoami_user = localStorage.getItem("whoami_user"); + this.whoami_host = localStorage.getItem("whoami_host"); + this.whoami_format = localStorage.getItem("whoami_format"); + + if (!this.whoami_user) { + console.log("Asking for username"); + this.whoami_user = prompt("Your user name", "malte70"); + if (this.whoami_user === null) + this.whoami_user = "malte70"; + } + if (!this.whoami_host) { + console.log("Asking for hostname"); + this.whoami_host = prompt("Hostname", "localhost"); + if (this.whoami_host === null) + this.whoami_host = "pc"; + } + if (!this.whoami_format) { + console.log("Asking for format"); + this.whoami_format = prompt("Format (unix|windows)", "unix"); + if (this.whoami_format === null) + this.whoami_format = "windows"; + } + + /*var urlParams = new URLSearchParams(window.location.search); + if (!urlParams.has('user')) { + this.whoami_user = null; + } else { + this.whoami_user = urlParams.get('user'); + this.whoami_host = "PLUTO"; + }*/ + }, + deinit: function() { + localStorage.removeItem("whoami_user"); + localStorage.removeItem("whoami_host"); + localStorage.removeItem("whoami_format"); + }, + 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")); + + if (this.whoami_user === null) + this.whoami_user = "malte70"; + if (this.whoami_host === null) { + if (localStorage.getItem("whoami_host") !== null) { + this.whoami_host = localStorage.getItem("whoami_host"); + } else { + this.whoami_host = "pc"; + } + } + 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(); + }, + update: function() { + // Update localstorage + localStorage.setItem("whoami_user", this.whoami_user); + localStorage.setItem("whoami_host", this.whoami_host); + localStorage.setItem("whoami_format", this.whoami_format); + + if (this.whoami_format.toLowerCase() == "windows") { + if (this.whoami_user == "root" || this.whoami_user == "admin") + this.whoami_user = "Administrator"; + + if (this.whoami_host == this.whoami_host.toLowerCase() && this.whoami_host != "localhost") + this.whoami_host = this.whoami_host.toUpperCase() + + // strip domain from fqdn + this.whoami_host = this.whoami_host.substr( + 0, + this.whoami_host.indexOf(".") + ); + user_login = '\\\\' + + this.whoami_host + + '\\' + + this.whoami_user; + + } else { + user_login = this.whoami_user + + '@' + + this.whoami_host; + + } + + this.section.innerHTML = '

'+user_login+'

'; + }, +}; +window.notepad = function(selector, storageKey) { + //console.log("window.notepad(\""+selector+"\", \""+storageKey+"\")") + + elem = document.querySelector(selector); + if (localStorage.getItem(storageKey) !== null) { + console.log("window.notepad(): Loading from LocalStorage") + elem.innerHTML = localStorage.getItem("notepad"); + } + elem.addEventListener("focusout", function() { + console.log("window.notepad(): Focus lost. Storing on LocalStorage"); + localStorage.setItem(storageKey, document.querySelector(selector).innerHTML); + }); + //document.querySelector(selector).onclick = function() { + // localStorage.setItem(storageKey, document.querySelector(selector).innerHTML); + //}; +}; +window.onload = function() { + window.whoami.init(); + window.whoami.update(); + + document.querySelector( + "button#settings" + ).onclick = function() { + window.whoami.settings(); + //window.whoami.update(); + }; + + window.notepad("#notepadContent", "notepad"); + + /** + * Hide footer + */ + //document.getElementsByTagName("footer")[0].remove() +}; diff --git a/main.js b/main.js deleted file mode 100644 index e7aced2..0000000 --- a/main.js +++ /dev/null @@ -1,139 +0,0 @@ -window.whoami = { - init: function() { - console.log("whoami.init()"); - if (!this.section) { - this.section = document.getElementById("whoami") - } - - this.whoami_user = localStorage.getItem("whoami_user"); - this.whoami_host = localStorage.getItem("whoami_host"); - this.whoami_format = localStorage.getItem("whoami_format"); - - if (!this.whoami_user) { - console.log("Asking for username"); - this.whoami_user = prompt("Your user name", "malte70"); - if (this.whoami_user === null) - this.whoami_user = "malte70"; - } - if (!this.whoami_host) { - console.log("Asking for hostname"); - this.whoami_host = prompt("Hostname", "localhost"); - if (this.whoami_host === null) - this.whoami_host = "pc"; - } - if (!this.whoami_format) { - console.log("Asking for format"); - this.whoami_format = prompt("Format (unix|windows)", "unix"); - if (this.whoami_format === null) - this.whoami_format = "windows"; - } - - /*var urlParams = new URLSearchParams(window.location.search); - if (!urlParams.has('user')) { - this.whoami_user = null; - } else { - this.whoami_user = urlParams.get('user'); - this.whoami_host = "PLUTO"; - }*/ - - }, - deinit: function() { - localStorage.removeItem("whoami_user"); - localStorage.removeItem("whoami_host"); - localStorage.removeItem("whoami_format"); - }, - 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")); - - if (this.whoami_user === null) - this.whoami_user = "malte70"; - if (this.whoami_host === null) { - if (localStorage.getItem("whoami_host") !== null) { - this.whoami_host = localStorage.getItem("whoami_host"); - } else { - this.whoami_host = "pc"; - } - } - 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(); - }, - update: function() { - // Update localstorage - localStorage.setItem("whoami_user", this.whoami_user); - localStorage.setItem("whoami_host", this.whoami_host); - localStorage.setItem("whoami_format", this.whoami_format); - - if (this.whoami_format.toLowerCase() == "windows") { - if (this.whoami_user == "root" || this.whoami_user == "admin") - this.whoami_user = "Administrator"; - - if (this.whoami_host == this.whoami_host.toLowerCase() && this.whoami_host != "localhost") - this.whoami_host = this.whoami_host.toUpperCase() - - // strip domain from fqdn - this.whoami_host = this.whoami_host.substr( - 0, - this.whoami_host.indexOf(".") - ); - user_login = '\\\\' - + this.whoami_host - + '\\' - + this.whoami_user; - - } else { - user_login = this.whoami_user - + '@' - + this.whoami_host; - - } - - this.section.innerHTML = '

'+user_login+'

'; - }, -}; -window.notepad = function(selector, storageKey) { - //console.log("window.notepad(\""+selector+"\", \""+storageKey+"\")") - - elem = document.querySelector(selector); - if (localStorage.getItem(storageKey) !== null) { - console.log("window.notepad(): Loading from LocalStorage") - elem.innerHTML = localStorage.getItem("notepad"); - } - elem.addEventListener("focusout", function() { - console.log("window.notepad(): Focus lost. Storing on LocalStorage"); - localStorage.setItem(storageKey, document.querySelector(selector).innerHTML); - }); - //document.querySelector(selector).onclick = function() { - // localStorage.setItem(storageKey, document.querySelector(selector).innerHTML); - //}; -}; -window.onload = function() { - window.whoami.init(); - window.whoami.update(); - - document.querySelector( - "button#settings" - ).onclick = function() { - window.whoami.settings(); - //window.whoami.update(); - }; - - window.notepad("#notepadContent", "notepad"); - - /** - * Hide footer - */ - //document.getElementsByTagName("footer")[0].remove() -}; 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 @@ - +