mapcomplete/UI/Base/VariableUIElement.ts

44 lines
1.3 KiB
TypeScript
Raw Normal View History

import {UIEventSource} from "../../Logic/UIEventSource";
2021-06-10 01:36:20 +02:00
import BaseUIElement from "../BaseUIElement";
2021-06-10 01:36:20 +02:00
export class VariableUiElement extends BaseUIElement {
private _element: HTMLElement;
constructor(
contents: UIEventSource<string | BaseUIElement | BaseUIElement[]>
) {
super();
2021-06-11 22:51:45 +02:00
this._element = document.createElement("span");
const el = this._element;
contents.addCallbackAndRun((contents) => {
while (el.firstChild) {
el.removeChild(el.lastChild);
}
2021-06-14 02:39:23 +02:00
if (contents === undefined) {
return el;
}
if (typeof contents === "string") {
el.innerHTML = contents;
} else if (contents instanceof Array) {
for (const content of contents) {
const c = content?.ConstructElement();
if (c !== undefined && c !== null) {
el.appendChild(c);
}
}
} else {
const c = contents.ConstructElement();
if (c !== undefined && c !== null) {
el.appendChild(c);
}
}
});
}
protected InnerConstructElement(): HTMLElement {
return this._element;
}
2021-07-26 12:26:41 +02:00
}