Files
t/ts/elemwrapper.ts

63 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

import { waitForAnimationFrame } from "./event";
2024-12-21 07:26:27 -08:00
export class ElemWrapper {
2024-12-21 21:42:54 -08:00
public elem: HTMLElement;
2024-12-22 10:23:37 -08:00
2024-12-21 21:42:54 -08:00
public style: CSSStyleDeclaration;
public classList: DOMTokenList;
2024-12-22 10:23:37 -08:00
2024-12-21 21:42:54 -08:00
public addEventListener: (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined) => void;
2024-12-22 10:23:37 -08:00
public appendChild: (node: Node) => void;
2024-12-21 21:42:54 -08:00
public cloneNode: (deep?: boolean | undefined) => Node;
public contains: (node: Node) => boolean;
2024-12-21 21:49:43 -08:00
public querySelector: (selectors: string) => Element | null;
2024-12-22 10:23:37 -08:00
public querySelectorAll: (selectors: string) => NodeListOf<Element>;
public remove: (node: Node) => void;
public setAttribute: (name: string, value: string) => void;
2024-12-21 21:49:43 -08:00
2024-12-21 21:42:54 -08:00
constructor(elem: HTMLElement) {
this.elem = elem;
2024-12-22 10:23:37 -08:00
2024-12-21 21:42:54 -08:00
this.style = this.elem.style;
this.classList = this.elem.classList;
2024-12-22 10:23:37 -08:00
2024-12-21 21:42:54 -08:00
this.addEventListener = this.elem.addEventListener.bind(this.elem);
2024-12-22 10:23:37 -08:00
this.appendChild = this.elem.appendChild.bind(this.elem);
2024-12-21 21:42:54 -08:00
this.cloneNode = this.elem.cloneNode.bind(this.elem);
2024-12-22 10:23:37 -08:00
this.contains = this.elem.contains.bind(this.elem);
2024-12-21 21:49:43 -08:00
this.querySelector = this.elem.querySelector.bind(this.elem);
2024-12-22 10:23:37 -08:00
this.querySelectorAll = this.elem.querySelectorAll.bind(this.elem);
this.remove = this.elem.remove.bind(this.elem);
this.setAttribute = this.elem.setAttribute.bind(this.elem);
2024-12-21 21:42:54 -08:00
}
static create(tagName: string) {
return new ElemWrapper(document.createElement(tagName));
}
append(elem: ElemWrapper) {
this.appendChild(elem.elem);
}
2024-12-21 07:26:27 -08:00
add(name: string, ...attrs: string[]): ElemWrapper {
2024-12-21 21:42:54 -08:00
const elem = ElemWrapper.create(name);
this.append(elem);
2024-12-21 07:26:27 -08:00
for (let i = 0; i < attrs.length; i += 2) {
elem.setAttribute(attrs[i], attrs[i + 1]);
}
2024-12-21 21:42:54 -08:00
return elem;
2024-12-21 07:26:27 -08:00
}
2024-12-22 10:23:37 -08:00
setID(id: string) {
this.elem.id = id;
}
async waitForShadowQuerySelector(selector: string) {
while (this.elem.shadowRoot?.querySelector(selector) === undefined) {
await waitForAnimationFrame();
}
}
}