From 57bb986524388b6fc9441aea7a53ff9852380663 Mon Sep 17 00:00:00 2001 From: Petri Hienonen Date: Sun, 16 Nov 2025 09:15:03 +0200 Subject: Change dom access pattern --- app/dom.js | 558 ++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 295 insertions(+), 263 deletions(-) (limited to 'app/dom.js') diff --git a/app/dom.js b/app/dom.js index 9473bf1..6f693c8 100644 --- a/app/dom.js +++ b/app/dom.js @@ -1,27 +1,3 @@ -export class DomOptions { - attributes; - children; - classes; - id; - styles; - - /** - * @param {Object} [options] - * @param {Partial} [options.styles] - * @param {string|null} [options.id] - * @param {string[]} [options.classes] - * @param {HTMLElement[]} [options.children] - * @param {Record} [options.attributes] - */ - constructor({ id = "", styles = {}, classes = [], children = [], attributes = {} } = {}) { - this.attributes = attributes; - this.children = children; - this.classes = classes; - this.id = id; - this.styles = styles; - } -} - /** * Toast notification types * @enum {string} @@ -35,351 +11,432 @@ export const ToastType = { export class Dom { /** * Create a `
` - * @param {DomOptions} options + * @param {Object} [o] + * @param {Partial} [o.styles] + * @param {string} [o.id] + * @param {string[]} [o.classes] + * @param {HTMLElement[]} [o.children] + * @param {Record} [o.attributes] * @returns {HTMLDivElement} */ - static div(options = new DomOptions()) { + static div(o = {}) { const div = document.createElement("div"); - Object.assign(div.style, options.styles); - if (options.id) div.id = options.id; - for (const cls of options.classes) div.classList.add(cls); - for (const [k, v] of Object.entries(options.attributes)) div.setAttribute(k, v); - if (options.children) div.append(...options.children); + if (o.styles) Object.assign(div.style, o.styles); + if (o.id) div.id = o.id; + if (o.classes) for (const cls of o.classes) div.classList.add(cls); + if (o.attributes) for (const [k, v] of Object.entries(o.attributes)) div.setAttribute(k, v); + if (o.children) div.append(...o.children); return div; } /** * Create a `` - * @param {string} text - * @param {DomOptions} options + * @param {Object} o + * @param {string} o.text + * @param {Partial} [o.styles] + * @param {string} [o.id] + * @param {string[]} [o.classes] + * @param {HTMLElement[]} [o.children] + * @param {Record} [o.attributes] * @returns {HTMLSpanElement} */ - static span(text, options = new DomOptions()) { + static span(o) { const span = document.createElement("span"); - Object.assign(span.style, options.styles); - if (options.id) span.id = options.id; - for (const cls of options.classes) span.classList.add(cls); - span.textContent = text; - for (const [k, v] of Object.entries(options.attributes)) span.setAttribute(k, v); - if (options.children) span.append(...options.children); + if (o.styles) Object.assign(span.style, o.styles); + if (o.id) span.id = o.id; + if (o.classes) for (const cls of o.classes) span.classList.add(cls); + span.textContent = o.text; + if (o.attributes) for (const [k, v] of Object.entries(o.attributes)) span.setAttribute(k, v); + if (o.children) span.append(...o.children); return span; } /** * Create a `