Skip to content

Commit

Permalink
Add convenience function libX.dom.componentState()
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Jun 20, 2024
1 parent 0b06cd7 commit 10000da
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/js/lib-x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ const libXDom = {
libX.dom.forEach(clone.getElementsByClassName('libx-state'), copy);
return clone;
},
componentState(elem: Element) {
const component = libX.ui.getComponent(elem);
libX.util.assert(component, 'Component not found for element', elem);
return libX.dom.state(component!);
},
removeState(elem: Element): Element {
const data = (<HTMLElement>elem).dataset;
if (data.libXState)
libX.dom.stateDepot[Number(data.libXState)] = {};
return elem;
},
create<K extends keyof HTMLElementTagNameMap | string>(tag: K, options?: { id?: string, subTags?: string[], class?: string, href?: string, html?: string, name?: string, rel?: string, src?: string, text?: string, type?: string }) {
const elem = globalThis.document.createElement(tag);
if (options?.id)
Expand All @@ -93,12 +104,6 @@ const libXDom = {
subTag => elem.appendChild(globalThis.document.createElement(subTag)));
return <K extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[K] : HTMLElement>elem;
},
removeState(elem: Element): Element {
const data = (<HTMLElement>elem).dataset;
if (data.libXState)
libX.dom.stateDepot[Number(data.libXState)] = {};
return elem;
},
select(selector: string): HTMLElement | null {
return globalThis.document.body.querySelector(selector);
},
Expand Down Expand Up @@ -632,6 +637,11 @@ const libXUi = {
};
return forkMe ? wrap() : null;
},
getComponent(elem: Element): Element | null {
// Returns the component (container element with a <code>data-component</code> attribute) to
// which the element belongs.
return elem?.closest('[data-component]') ?? null;
},
};

const libXUtil = {
Expand All @@ -650,6 +660,12 @@ const libXUtil = {
// libX.util.removeWhitespace('a b \t\n c') === 'abc';
return text.replace(/\s/g, '');
},
assert(ok: boolean | unknown, message: string, info: unknown): void {
// Oops, file a tps report.
const quoteStr = (info: unknown) => typeof info === 'string' ? `"${info}"` : String(info);
if (!ok)
throw Error(`[dna-engine] ${message} --> ${quoteStr(info)}`);
},
};

const libXNav = {
Expand Down

0 comments on commit 10000da

Please sign in to comment.