2021-09-09 00:05:51 +02:00
|
|
|
import {UIEventSource} from "../../Logic/UIEventSource";
|
2021-06-10 01:36:20 +02:00
|
|
|
import BaseUIElement from "../BaseUIElement";
|
2022-01-14 19:34:00 +01:00
|
|
|
import Combine from "./Combine";
|
2020-06-25 03:39:31 +02:00
|
|
|
|
2021-06-10 01:36:20 +02:00
|
|
|
export class VariableUiElement extends BaseUIElement {
|
2021-09-26 17:36:39 +02:00
|
|
|
private readonly _contents: UIEventSource<string | BaseUIElement | BaseUIElement[]>;
|
2020-06-25 03:39:31 +02:00
|
|
|
|
2021-09-26 17:36:39 +02:00
|
|
|
constructor(contents: UIEventSource<string | BaseUIElement | BaseUIElement[]>) {
|
2021-09-09 00:05:51 +02:00
|
|
|
super();
|
2021-09-26 17:36:39 +02:00
|
|
|
this._contents = contents;
|
|
|
|
}
|
2022-01-06 18:51:52 +01:00
|
|
|
|
|
|
|
Destroy() {
|
|
|
|
super.Destroy();
|
|
|
|
this.isDestroyed = true;
|
|
|
|
}
|
2021-09-26 17:36:39 +02:00
|
|
|
|
|
|
|
protected InnerConstructElement(): HTMLElement {
|
|
|
|
const el = document.createElement("span");
|
2022-01-06 18:51:52 +01:00
|
|
|
const self = this;
|
2021-09-26 17:36:39 +02:00
|
|
|
this._contents.addCallbackAndRun((contents) => {
|
2022-01-06 18:51:52 +01:00
|
|
|
if(self.isDestroyed){
|
|
|
|
return true;
|
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
while (el.firstChild) {
|
|
|
|
el.removeChild(el.lastChild);
|
|
|
|
}
|
2021-06-14 02:39:23 +02:00
|
|
|
|
2021-09-09 00:05:51 +02:00
|
|
|
if (contents === undefined) {
|
2021-09-23 20:45:57 +02:00
|
|
|
return
|
2021-09-09 00:05:51 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-09-26 17:36:39 +02:00
|
|
|
return el;
|
2021-09-09 00:05:51 +02:00
|
|
|
}
|
2022-01-14 19:34:00 +01:00
|
|
|
|
|
|
|
AsMarkdown(): string {
|
|
|
|
const d = this._contents.data;
|
|
|
|
if(typeof d === "string"){
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
if(d instanceof BaseUIElement){
|
|
|
|
return d.AsMarkdown()
|
|
|
|
}
|
|
|
|
return new Combine(<BaseUIElement[]>d).AsMarkdown()
|
|
|
|
}
|
2021-07-26 12:26:41 +02:00
|
|
|
}
|