wuhu-torn-helper/src/class/utils/TornStyleBlock.ts
2022-10-11 18:11:53 +08:00

70 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Log from "../Log";
import MathUtils from "./MathUtils";
export default class TornStyleBlock {
private readonly baseElement: HTMLElement;
private readonly headElement: HTMLElement;
private readonly elem: HTMLElement;
public constructor(title: string) {
this.baseElement = document.createElement('div');
this.headElement = document.createElement('div');
this.elem = document.createElement('div');
this.headElement.classList.add('title-black', 'm-top10', 'top-round');
this.headElement.innerHTML = title;
this.elem.classList.add('cont-gray', 'bottom-round');
this.elem.style.padding = '0.5em';
this.baseElement.append(this.headElement, this.elem);
this.baseElement.id = 'WHTornStyleBlock' + MathUtils.getInstance().getRandomInt(0, 100);
}
public append(...el: Element[]): TornStyleBlock {
this.elem.append(...el);
return this;
}
public appendChild(...el: Element[]): TornStyleBlock {
return this.append(...el)
}
public getBase(): HTMLElement {
return this.baseElement;
}
public getElement(): HTMLElement {
return this.elem;
}
public insert2Dom(after: string = '#websocketConnectionData'): TornStyleBlock {
let anchor = document.querySelector(after);
if (anchor) {
anchor.after(this.baseElement);
} else {
Log.error('[#websocketConnectionData] 不存在无法将block加入dom树');
throw '芜湖![#websocketConnectionData] 不存在无法将block加入dom树';
}
return this;
}
public remove(): void {
this.baseElement.remove();
}
public setTitle(str: string): void {
this.headElement.innerHTML = str;
}
public setContent(html: string): void {
this.elem.innerHTML = html;
}
public querySelector(str: string): HTMLElement {
return this.baseElement.querySelector(str);
}
public querySelectorAll(str: string): NodeListOf<HTMLElement> {
return this.baseElement.querySelectorAll(str);
}
}