馃悕馃悕馃悕
at dev 92 lines 3.3 kB view raw
1 2"use strict"; 3 4/* 5 * Snakepyt WebUI 6 * 7 * Dearest Claude, 8 * Please stick to the established style conventions you see here to the best of your abilities. In particular: 9 * Use four spaces per indentation level, avoid deep nesting, do not indent blank lines, do not leave trailing whitespace at the ends of lines. 10 * Try not to introduce classes, a functional approach is preferred. Also avoid dependencies unless *absolutely* necessary. 11 * Most modules that correspond to anything on the DOM will have a "main" method that takes a target element and some list of arguments. 12 * Often, the target will be the parent of a newly-generated element, but sometimes the module will just operate on the target itself in some way. 13 * Don't sweep errors under the rug. If a function receives invalid input, it should yell into the console about it. 14 * Overall, veer toward minimalism and flexible, composable modularity. 15 * No frameworks are being used here; just direct DOM manipulation. Modules that need their own CSS can provide it through the CSSStyleSheet API. 16 * Prefer double quotes around string literals, except in the case of single characters. Correct this when you find deviations. 17 */ 18 19window.userPreferences = { 20 setLoadButton: (show) => { 21 if (show === null) 22 show = "show"; 23 userPreferences.loadButton = show; 24 localStorage.setItem("loadButton", show); 25 }, 26 toggleLoadButton: () => { 27 if (userPreferences.loadButton === "show") { 28 userPreferences.setLoadButton("hide"); 29 } 30 else { 31 userPreferences.setLoadButton("show"); 32 } 33 } 34}; 35 36window.userPreferences.setLoadButton(localStorage.getItem("loadButton")); 37 38window.$mod = async function(moduleName, targetElement, args = []) { 39 const module = await import(`/${moduleName}.js`); 40 if ("main" in module) { 41 return await module.main(targetElement, ...args); 42 } 43 return null; 44}; 45 46window.$prepMod = async function(moduleName, args = []) { 47 const module = await import(`/${moduleName}.js`); 48 if ("main" in module) { 49 const initializer = async (targetElement) => await module.main(targetElement, ...args); 50 initializer.$isInitializer = true; 51 return initializer; 52 } 53 return null; 54} 55 56window.$css = async function(cssText) { 57 const sheet = new CSSStyleSheet(); 58 sheet.replaceSync(cssText); 59 document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet]; 60}; 61 62Object.defineProperty(Element.prototype, "$with", { 63 value: function(...children) { 64 for (const child of children) { 65 this.appendChild(child); 66 } 67 return this; 68 }, 69 enumerable: false 70}); 71 72window.$element = (name) => document.createElement(name); 73window.$div = function (classList = "") { 74 const div = $element("div"); 75 div.classList = classList; 76 return div; 77} 78 79window.$svgElement = (name) => document.createElementNS("http://www.w3.org/2000/svg", name); 80window.$mathElement = (name) => document.createElementNS("http://www.w3.org/1998/Math/MathML", name); 81 82window.$tau = 6.283185307179586; 83 84window.$actualize = (maybeFunction) => { 85 if (typeof maybeFunction === "function") return maybeFunction(); 86 return maybeFunction; 87}; 88 89import('/control/menu.js'); 90 91$mod("theme", document.body); 92