mapcomplete/UI/Base/VariableUIElement.ts

46 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 {
2021-06-10 01:36:20 +02:00
private _element : HTMLElement;
2021-06-11 22:51:45 +02:00
constructor(contents: UIEventSource<string | BaseUIElement | BaseUIElement[]>) {
2021-06-10 01:36:20 +02:00
super();
this._element = document.createElement("span")
const el = this._element
contents.addCallbackAndRun(contents => {
2021-06-11 22:51:45 +02:00
while (el.firstChild) {
2021-06-10 01:36:20 +02:00
el.removeChild(
el.lastChild
)
}
2021-06-11 22:51:45 +02:00
if (contents === undefined) {
2021-06-16 14:23:53 +02:00
return el;
2021-06-10 01:36:20 +02:00
}
2021-06-11 22:51:45 +02:00
if (typeof contents === "string") {
2021-06-10 01:36:20 +02:00
el.innerHTML = contents
2021-06-11 22:51:45 +02:00
} else if (contents instanceof Array) {
for (const content of contents) {
2021-06-14 02:39:23 +02:00
const c = content.ConstructElement();
if (c !== undefined && c !== null) {
2021-06-14 17:28:11 +02:00
el.appendChild(c)
2021-06-14 02:39:23 +02:00
}
}
} else {
const c = contents.ConstructElement();
if (c !== undefined && c !== null) {
el.appendChild(c)
2021-06-11 22:51:45 +02:00
}
2021-06-10 01:36:20 +02:00
}
})
}
2021-06-10 01:36:20 +02:00
protected InnerConstructElement(): HTMLElement {
return this._element;
}
}