From 9383a2fb09ffb60cfe63683106945bd688affa59 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Wed, 1 Jun 2022 21:13:43 +0200 Subject: feat: Initial commit after clean slate --- src/wwwroot/scripts/base.js | 84 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/wwwroot/scripts/base.js (limited to 'src/wwwroot/scripts/base.js') diff --git a/src/wwwroot/scripts/base.js b/src/wwwroot/scripts/base.js new file mode 100644 index 0000000..860cd34 --- /dev/null +++ b/src/wwwroot/scripts/base.js @@ -0,0 +1,84 @@ +import {Toaster} from "./toaster"; +import {configuration} from "./configuration"; + +export const toaster = new Toaster(); +export const doc = document, + $$ = (s, o = doc) => o.querySelectorAll(s), + $ = (s, o = doc) => o.querySelector(s); + +function toggleDarkTheme() { + if (localStorage["dark-theme"] === "true") localStorage["dark-theme"] = "false"; + else localStorage["dark-theme"] = "true"; + setTheme(); +} + +function setTheme() { + if (localStorage["dark-theme"] === "true") { + document.getElementsByTagName("html")[0].setAttribute("dark-theme", ""); + } else { + document.getElementsByTagName("html")[0].removeAttribute("dark-theme"); + } +} + +window.addEventListener("error", (e) => { + if (configuration.analytics.error) { + (async () => { + const error = { + msg: e.message, + line: e.lineno, + path: location.pathname, + filename: e.filename, + }; + await fetch("/api/analytics/error", { + method: "post", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(error), + }); + })(); + } +}); + +window.addEventListener("load", () => { + if (configuration.analytics.pageload && !location.pathname.startsWith("/kontoret")) { + (async () => { + //const loadTime = window.performance.timing.domContentLoadedEventEnd - + // window.performance.timing.navigationStart; + const loadTime = window.performance.timeOrigin - window.performance.now(); + await fetch("/api/analytics/pageload", { + method: "post", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({time: loadTime, path: location.pathname}), + }); + })(); + } +}); + +// https://stackoverflow.com/a/18234317/11961742 +String.prototype.formatUnicorn = String.prototype.formatUnicorn || function () { + "use strict"; + let str = this.toString(); + if (arguments.length) { + const t = typeof arguments[0]; + const args = ("string" === t || "number" === t) ? + Array.prototype.slice.call(arguments) + : arguments[0]; + + for (const key in args) { + str = str.replace(new RegExp("\\{" + key + "\\}", "gi"), args[key]); + } + } + + return str; +}; + +setTheme(); + +export function pageInit(cb) { + if (typeof cb === "function") cb(); +} + +doc.addEventListener("DOMContentLoaded", pageInit); -- cgit v1.3