import { Binding } from "@akala/core"; import { DataBind } from "./common.js"; /** * Creates a new HTML element. * @param {string} tagName - The name of the HTML element to create. * @param {ElementCreationOptions} [options] - Optional element creation options. * @returns {HTMLElement} The created HTML element. */ export function e(tagName: K, options?: ElementCreationOptions): HTMLElementTagNameMap[K] export function e(tagName: string, options?: ElementCreationOptions): HTMLElement export function e(tagName: string, options?: ElementCreationOptions): HTMLElement { return document.createElement(tagName, options); } /** * Extends the target object with the provided values. * @param {object} target - The target object to extend. * @param {Partial} values - The values to extend the target object with. * @returns {object} The extended target object. */ export function s(target: T, values: Partial) { return DataBind.extend(target, values); } /** * Gets or sets attributes on an element. * @param {Element} el - The element to get or set attributes on. * @param {string | string[] | Record} name - The attribute name(s) or a record of attribute names and values. * @param {string | null} [value] - The attribute value to set. * @returns {string | string[] | Element} The attribute value(s) or the element. */ export function a(el: T, name: string): string export function a(el: T, name: string, value: string): T export function a(el: T, record: Record): T export function a(el: T, record: string[]): string[] export function a(el: T, name: string | string[] | Record, value?: string | null) export function a(el: T, name: string | string[] | Record, value?: string | null) { if (typeof name == 'string') if (typeof (value) !== 'undefined') { if (value === null) el.removeAttribute(name); else el.setAttribute(name, value); } else return el.getAttribute(name); else if (Array.isArray(name)) return name.map(n => el.getAttribute(n)); else Object.entries(name).forEach(([name, value]) => el.setAttribute(name, value)); return el; } /** * Adds classes to an element. * @param {Element} el - The element to add classes to. * @param {...string} classes - The classes to add. * @returns {Element} The element with the added classes. */ export function c(el: T, ...classes: string[]): T { el.classList.add(...classes); return el; } /** * Replaces the children of an element with the provided nodes or bindings. * @param {Element} el - The element to replace children of. * @param {...(Node | Binding)} children - The nodes or bindings to replace the children with. * @returns {Element} The element with the replaced children. */ export function content(el: T, ...children: (Node | Binding)[]): T { el.replaceChildren(...children.map(c => c instanceof Binding ? c.getValue() : c)); Binding.combine(...children).onChanged(ev => el.replaceChildren(...ev.value) ) return el; } /** * Appends children to an element. * @param {Element} el - The element to append children to. * @param {...Node} children - The children to append. * @returns {Element} The element with the appended children. */ content.p = function cp(el: T, ...children: Node[]): T { el.append(...children); return el; } /** * Creates a new text node. * @param {string | number | boolean | bigint} content - The content of the text node. * @returns {Text} The created text node. */ export function t(content: string | number | boolean | bigint) { return document.createTextNode(content?.toString()); }