2021-06-23 14:21:33 +00:00
|
|
|
/*
|
|
|
|
A functional HTML generation library.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
html.label(
|
|
|
|
html.span("Delete everything", {class: ["warning", "important"]}),
|
|
|
|
html.button("Click", {onClick: e => document.body.innerHTML=""}),
|
|
|
|
)
|
|
|
|
or
|
|
|
|
html.ul([1, 2, 3, 4, 5].map(x => html.li(x)), {class: "numbers"})
|
|
|
|
*/
|
|
|
|
|
2021-08-16 15:57:10 +00:00
|
|
|
const keyToPropName = key => key.replace(/^[A-Z]/, a => "-"+a).replace(/[A-Z]/g, a => '-'+a.toLowerCase())
|
|
|
|
|
2022-03-26 11:40:50 +00:00
|
|
|
export const empty = Symbol("Explicit empty argument for Skooma")
|
|
|
|
|
2021-08-16 15:57:10 +00:00
|
|
|
const insertStyles = (rule, styles) => {
|
|
|
|
for (let [key, value] of Object.entries(styles))
|
|
|
|
if (typeof value == "undefined")
|
|
|
|
rule.removeProperty(keyToPropName(key))
|
|
|
|
else
|
|
|
|
rule.setProperty(keyToPropName(key), value.toString())
|
|
|
|
}
|
|
|
|
|
2021-02-19 20:37:13 +00:00
|
|
|
const parseAttribute = (attribute) => {
|
2021-05-22 12:34:17 +00:00
|
|
|
if (typeof attribute == "string" || typeof attribute == "number")
|
2021-02-19 20:37:13 +00:00
|
|
|
return attribute
|
|
|
|
else if ("join" in attribute)
|
|
|
|
return attribute.join(" ")
|
|
|
|
else
|
|
|
|
return JSON.stringify(attribute)
|
|
|
|
}
|
|
|
|
|
2021-08-27 14:02:49 +00:00
|
|
|
const parseArgs = (element, before, ...args) => {
|
2021-02-23 17:55:32 +00:00
|
|
|
if (element.content) element = element.content
|
2022-03-26 11:40:50 +00:00
|
|
|
for (let arg of args) if (arg !== empty)
|
2021-05-22 12:34:17 +00:00
|
|
|
if (typeof arg == "string" || typeof arg == "number")
|
2021-08-27 14:02:49 +00:00
|
|
|
element.insertBefore(document.createTextNode(arg), before)
|
2022-03-26 11:40:50 +00:00
|
|
|
else if (arg === undefined || arg == null)
|
2022-03-26 11:36:39 +00:00
|
|
|
console.warn(`Argument is ${typeof arg}`, element)
|
2021-09-13 16:54:07 +00:00
|
|
|
else if (typeof arg == "function")
|
|
|
|
arg(element)
|
2021-02-17 10:03:21 +00:00
|
|
|
else if ("nodeName" in arg)
|
2021-08-27 14:02:49 +00:00
|
|
|
element.insertBefore(arg, before)
|
2021-02-17 10:03:21 +00:00
|
|
|
else if ("length" in arg)
|
2021-08-27 14:02:49 +00:00
|
|
|
parseArgs(element, before, ...arg)
|
2021-02-17 10:03:21 +00:00
|
|
|
else
|
2021-05-17 15:19:32 +00:00
|
|
|
for (let key in arg)
|
2021-08-16 17:26:45 +00:00
|
|
|
if (key == "style" && typeof(arg[key])=="object")
|
2021-08-16 15:57:10 +00:00
|
|
|
insertStyles(element.style, arg[key])
|
2021-09-13 12:14:42 +00:00
|
|
|
else if (key == "dataset" && typeof(arg[key])=="object")
|
|
|
|
for (let [key2, value] of Object.entries(arg[key]))
|
|
|
|
element.dataset[key2] = parseAttribute(value)
|
2021-08-16 15:57:10 +00:00
|
|
|
else if (key == "shadowRoot")
|
2021-08-27 14:02:49 +00:00
|
|
|
parseArgs((element.shadowRoot || element.attachShadow({mode: "open"})), null, arg[key])
|
|
|
|
else if (typeof arg[key] === "function")
|
2021-09-10 11:31:44 +00:00
|
|
|
element.addEventListener(key.replace(/^on[A-Z]/, x => x.charAt(x.length-1).toLowerCase()), arg[key])
|
2021-08-16 16:03:43 +00:00
|
|
|
else if (arg[key] === true)
|
|
|
|
{if (!element.hasAttribute(key)) element.setAttribute(key, '')}
|
|
|
|
else if (arg[key] === false)
|
|
|
|
element.removeAttribute(key)
|
2021-05-26 09:26:44 +00:00
|
|
|
else
|
|
|
|
element.setAttribute(key, parseAttribute(arg[key]))
|
2021-02-17 10:03:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 13:12:09 +00:00
|
|
|
const nop = object => object
|
|
|
|
const node = (_name, args, options) => {
|
2021-03-05 12:29:21 +00:00
|
|
|
let element
|
2022-03-26 13:12:09 +00:00
|
|
|
const [name, custom] = _name
|
|
|
|
.match(/[^$]+/g)
|
|
|
|
.map(options.nameFilter ?? nop)
|
2021-07-30 10:12:00 +00:00
|
|
|
if (options.xmlns)
|
2022-03-26 13:12:09 +00:00
|
|
|
element = document.createElementNS(options.xmlns, name, {is: custom})
|
2021-02-23 18:11:25 +00:00
|
|
|
else
|
2022-03-26 13:12:09 +00:00
|
|
|
element = document.createElement(name, {is: custom})
|
2021-08-27 14:02:49 +00:00
|
|
|
parseArgs(element, null, args)
|
2021-02-17 10:03:21 +00:00
|
|
|
return element
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:00:00 +00:00
|
|
|
const nameSpacedProxy = (options={}) => new Proxy(Window, {
|
|
|
|
get: (target, prop, receiver) => { return (...args) => node(prop, args, options) },
|
|
|
|
has: (target, prop) => true,
|
|
|
|
})
|
2021-02-23 18:11:25 +00:00
|
|
|
|
2022-02-06 11:56:54 +00:00
|
|
|
export const bind = transform => {
|
2021-11-25 09:08:59 +00:00
|
|
|
let element
|
2022-02-06 11:56:54 +00:00
|
|
|
const inject = next => Object.defineProperty(next, 'current', {get: () => element})
|
|
|
|
const update = (...data) => {
|
|
|
|
const next = transform(...data)
|
|
|
|
if (next) {
|
|
|
|
if (element) element.replaceWith(next)
|
|
|
|
element = inject(next)
|
|
|
|
return element
|
2022-01-14 10:56:15 +00:00
|
|
|
}
|
2022-02-06 11:56:54 +00:00
|
|
|
}
|
|
|
|
return update
|
2021-11-25 09:08:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 17:28:36 +00:00
|
|
|
export const handle = fn => event => { event.preventDefault(); return fn(event) }
|
|
|
|
|
2021-07-30 10:12:00 +00:00
|
|
|
export const html = nameSpacedProxy({nameFilter: name => name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()})
|
|
|
|
export const svg = nameSpacedProxy({xmlns: "http://www.w3.org/2000/svg"})
|
2021-11-23 15:27:25 +00:00
|
|
|
|
2021-12-26 16:54:04 +00:00
|
|
|
const textFromTemplate = (literals, items) => {
|
|
|
|
const fragment = new DocumentFragment()
|
|
|
|
for (const key in items) {
|
|
|
|
fragment.append(document.createTextNode(literals[key]))
|
|
|
|
fragment.append(items[key])
|
|
|
|
}
|
|
|
|
fragment.append(document.createTextNode(literals.at(-1)))
|
|
|
|
return fragment
|
|
|
|
}
|
|
|
|
|
|
|
|
export const text = (data="", ...items) =>
|
2022-02-06 11:56:54 +00:00
|
|
|
typeof data == "object" && "at" in data
|
|
|
|
? textFromTemplate(data, items)
|
|
|
|
: document.createTextNode(data)
|