馃悕馃悕馃悕
at main 122 lines 3.5 kB view raw
1 2"use strict"; 3 4window.userPreferences = { 5 setLoadButton: (show) => { 6 if (show === null) 7 show = "show"; 8 userPreferences.loadButton = show; 9 localStorage.setItem("loadButton", show); 10 }, 11 toggleLoadButton: () => { 12 if (userPreferences.loadButton === "show") { 13 userPreferences.setLoadButton("hide"); 14 } 15 else { 16 userPreferences.setLoadButton("show"); 17 } 18 } 19}; 20 21window.userPreferences.setLoadButton(localStorage.getItem("loadButton")); 22 23window.$mod = async function(moduleName, targetElement, args = []) { 24 const module = await import(`/code/${moduleName}.js`); 25 if ("main" in module) { 26 return await module.main(targetElement, ...args); 27 } 28 return null; 29}; 30 31window.$prepMod = async function(moduleName, args = []) { 32 const module = await import(`/code/${moduleName}.js`); 33 if ("main" in module) { 34 const initializer = async targetElement => await module.main(targetElement, ...args); 35 initializer.$isInitializer = true; 36 return initializer; 37 } 38 return null; 39} 40 41window.$css = async function(cssText) { 42 const sheet = new CSSStyleSheet(); 43 sheet.replaceSync(cssText); 44 document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet]; 45}; 46 47Object.defineProperty(Element.prototype, "$with", { 48 value: function(...children) { 49 for (const child of children) { 50 this.appendChild(child); 51 } 52 return this; 53 }, 54 enumerable: false 55}); 56 57 58Object.defineProperty(Element.prototype, "$attrs", { 59 get() { 60 if (this.__attrsProxy) return this.__attrsProxy; 61 62 const el = this; 63 this.__attrsProxy = new Proxy({}, { 64 get(_, prop) { 65 return el.getAttribute(prop.toString()); 66 }, 67 set(_, prop, val) { 68 el.setAttribute(prop.toString(), val); 69 return true; 70 }, 71 deleteProperty(_, prop) { 72 el.removeAttribute(prop.toString()); 73 return true; 74 }, 75 has(_, prop) { 76 return el.hasAttribute(prop.toString()); 77 }, 78 ownKeys() { 79 return Array.from(el.attributes).map(a => a.name); 80 }, 81 getOwnPropertyDescriptor(_, prop) { 82 if (el.hasAttribute(prop.toString())) { 83 return { 84 configurable: true, 85 enumerable: true, 86 value: el.getAttribute(prop.toString()) 87 }; 88 } 89 return undefined; 90 } 91 }); 92 }, 93 enumerable: false 94}); 95 96window.$element = (name) => document.createElement(name); 97window.$div = function (className = "") { 98 const div = $element("div"); 99 div.className = className; 100 return div; 101} 102 103window.$svgElement = (name) => 104 document.createElementNS("http://www.w3.org/2000/svg", name); 105window.$mathElement = (name) => 106 document.createElementNS("http://www.w3.org/1998/Math/MathML", name); 107 108window.$actualize = (maybeFunction) => { 109 if (typeof maybeFunction === "function") return maybeFunction(); 110 return maybeFunction; 111}; 112 113import('/code/control/menu.js'); 114 115$mod("theme", document.body); 116 117document.body.$contextMenu = { 118 items: Object.entries({ 119 "toggle theme": async () => $mod("theme", document.body, ["toggle"]), 120 }) 121}; 122