mapcomplete/UI/Base/Combine.ts

64 lines
2 KiB
TypeScript
Raw Normal View History

2020-09-12 23:15:17 +02:00
import {FixedUiElement} from "./FixedUiElement";
import {Utils} from "../../Utils";
2021-06-10 01:36:20 +02:00
import BaseUIElement from "../BaseUIElement";
2021-11-25 22:16:00 +01:00
import Title from "./Title";
2020-07-21 01:37:48 +02:00
2021-06-10 01:36:20 +02:00
export default class Combine extends BaseUIElement {
private readonly uiElements: BaseUIElement[];
2020-07-21 01:37:48 +02:00
2021-06-10 01:36:20 +02:00
constructor(uiElements: (string | BaseUIElement)[]) {
2020-09-12 23:15:17 +02:00
super();
this.uiElements = Utils.NoNull(uiElements)
.map(el => {
if (typeof el === "string") {
return new FixedUiElement(el);
}
return el;
});
2020-07-21 01:37:48 +02:00
}
AsMarkdown(): string {
return this.uiElements.map(el => el.AsMarkdown()).join(this.HasClass("flex-col") ? "\n\n" : " ");
}
2021-06-10 01:36:20 +02:00
protected InnerConstructElement(): HTMLElement {
const el = document.createElement("span")
try {
for (const subEl of this.uiElements) {
if (subEl === undefined || subEl === null) {
continue;
}
2021-11-07 16:34:51 +01:00
try {
const subHtml = subEl.ConstructElement()
if (subHtml !== undefined) {
el.appendChild(subHtml)
}
} catch (e) {
2021-10-08 04:33:39 +02:00
console.error("Could not generate subelement in combine due to ", e)
}
}
} catch (e) {
2021-06-16 14:23:53 +02:00
const domExc = e as DOMException
console.error("DOMException: ", domExc.name)
el.appendChild(new FixedUiElement("Could not generate this combine!").SetClass("alert").ConstructElement())
}
2021-06-10 01:36:20 +02:00
return el;
2020-07-21 01:37:48 +02:00
}
2021-11-25 22:16:00 +01:00
public getToC(): Title[]{
const titles = []
for (const uiElement of this.uiElements) {
if(uiElement instanceof Combine){
titles.push(...uiElement.getToC())
}else if(uiElement instanceof Title){
titles.push(uiElement)
}
}
return titles
}
2020-07-21 01:37:48 +02:00
}