|
| 1 | +/** |
| 2 | + * Creates a DOM element, assigns the properties of `data` to it, and appends all `children`. |
| 3 | + * |
| 4 | + * @type{function(string|Function, Object=, Node|Array.<Node|string>=)} |
| 5 | + */ |
| 6 | +const h = (name, data = {}, children = []) => { |
| 7 | + const result = |
| 8 | + typeof name == "function" ? name(data) : Object.assign(document.createElement(name), data); |
| 9 | + if (!Array.isArray(children)) { |
| 10 | + children = [children]; |
| 11 | + } |
| 12 | + result.append(...children); |
| 13 | + return result; |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * Create a text node. |
| 18 | + * |
| 19 | + * Equivalent to `document.createTextNode(text)` |
| 20 | + * |
| 21 | + * @type{function(string): Text} |
| 22 | + */ |
| 23 | +const t = (text) => document.createTextNode(text); |
| 24 | + |
| 25 | +/** |
| 26 | + * Remove all child nodes from a DOM element. |
| 27 | + * |
| 28 | + * @type{function(Node)} |
| 29 | + */ |
| 30 | +const x = (elem) => { |
| 31 | + while (elem.lastChild) { |
| 32 | + elem.removeChild(elem.lastChild); |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Get all elements with the given ID. |
| 38 | + * |
| 39 | + * Equivalent to `document.getElementById(name)` |
| 40 | + * |
| 41 | + * @type{function(string): HTMLElement} |
| 42 | + */ |
| 43 | +const g = (name) => document.getElementById(name); |
| 44 | + |
| 45 | +/** |
| 46 | + * Get all elements with the given class name. |
| 47 | + * |
| 48 | + * Equivalent to `document.getElementsByClassName(name)` |
| 49 | + * |
| 50 | + * @type{function(string): HTMLCollectionOf.<Element>} |
| 51 | + */ |
| 52 | +const c = (name) => document.getElementsByClassName(name); |
| 53 | + |
| 54 | +/** @type{function(string): HTMLCollectionOf.<Element>} */ |
| 55 | +const n = (name) => document.getElementsByName(name); |
| 56 | + |
| 57 | +/** |
| 58 | + * Get all elements matching the given HTML selector. |
| 59 | + * |
| 60 | + * Matches selectors with `document.querySelectorAll(selector)` |
| 61 | + * |
| 62 | + * @type{function(string): Array.<HTMLElement>} |
| 63 | + */ |
| 64 | +const s = (selector) => Array.from(document.querySelectorAll(selector)); |
| 65 | + |
| 66 | +/** |
| 67 | + * Generate a relative URL from `url`, appending all key-value pairs from `params` as URL-encoded parameters. |
| 68 | + * |
| 69 | + * @type{function(string=, Object=): string} |
| 70 | + */ |
| 71 | +const u = (url = "", params = {}) => { |
| 72 | + let result = new URL(url, window.location.href); |
| 73 | + Object.entries(params).forEach((kv) => { |
| 74 | + let [k, v] = kv; |
| 75 | + result.searchParams.set(k, v); |
| 76 | + }); |
| 77 | + return result.toString(); |
| 78 | +}; |
| 79 | + |
| 80 | +/** |
| 81 | + * Takes a callback to run when all DOM content is loaded. |
| 82 | + * |
| 83 | + * Equivalent to `window.addEventListener('DOMContentLoaded', callback)` |
| 84 | + * |
| 85 | + * @type{function(function())} |
| 86 | + */ |
| 87 | +const r = (callback) => window.addEventListener("DOMContentLoaded", callback); |
| 88 | + |
| 89 | +/** |
| 90 | + * Allows a stateful value to be tracked by consumers. |
| 91 | + * |
| 92 | + * This is the Xeact version of the React useState hook. |
| 93 | + * |
| 94 | + * @type{function(any): [function(): any, function(any): void]} |
| 95 | + */ |
| 96 | +const useState = (value = undefined) => { |
| 97 | + return [ |
| 98 | + () => value, |
| 99 | + (x) => { |
| 100 | + value = x; |
| 101 | + }, |
| 102 | + ]; |
| 103 | +}; |
| 104 | + |
| 105 | +/** |
| 106 | + * Debounce an action for up to ms milliseconds. |
| 107 | + * |
| 108 | + * @type{function(number): function(function(any): void)} |
| 109 | + */ |
| 110 | +const d = (ms) => { |
| 111 | + let debounceTimer = null; |
| 112 | + return (f) => { |
| 113 | + clearTimeout(debounceTimer); |
| 114 | + debounceTimer = setTimeout(f, ms); |
| 115 | + }; |
| 116 | +}; |
| 117 | + |
| 118 | +/** |
| 119 | + * Parse the contents of a given HTML page element as JSON and |
| 120 | + * return the results. |
| 121 | + * |
| 122 | + * This is useful when using templ to pass complicated data from |
| 123 | + * the server to the client via HTML[1]. |
| 124 | + * |
| 125 | + * [1]: https://templ.guide/syntax-and-usage/script-templates/#pass-server-side-data-to-the-client-in-a-html-attribute |
| 126 | + */ |
| 127 | +const j = (id) => JSON.parse(g(id).textContent); |
| 128 | + |
| 129 | +export { h, t, x, g, j, c, n, u, s, r, useState, d }; |
0 commit comments