MapComplete/UI/Base/VariableUIElement.ts

39 lines
1.1 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-10 01:36:20 +02:00
return
}
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) {
el.appendChild(content.ConstructElement())
}
}else{
2021-06-10 01:36:20 +02:00
el.appendChild(contents.ConstructElement())
}
})
}
2021-06-10 01:36:20 +02:00
protected InnerConstructElement(): HTMLElement {
return this._element;
}
}