mapcomplete/UI/Popup/TagRenderingAnswer.ts

62 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-10-27 01:01:34 +01:00
import {UIEventSource} from "../../Logic/UIEventSource";
import {Utils} from "../../Utils";
2021-06-11 22:51:45 +02:00
import BaseUIElement from "../BaseUIElement";
2021-06-14 02:39:23 +02:00
import {VariableUiElement} from "../Base/VariableUIElement";
import List from "../Base/List";
2021-06-14 17:28:11 +02:00
import {SubstitutedTranslation} from "../SubstitutedTranslation";
import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig";
import Combine from "../Base/Combine";
import Img from "../Base/Img";
2020-10-27 01:01:34 +01:00
/***
* Displays the correct value for a known tagrendering
*/
2021-06-14 02:39:23 +02:00
export default class TagRenderingAnswer extends VariableUiElement {
2020-10-27 01:01:34 +01:00
constructor(tagsSource: UIEventSource<any>, configuration: TagRenderingConfig,
2021-12-21 18:35:31 +01:00
state: any,
2022-01-26 21:40:38 +01:00
contentClasses: string = "", contentStyle: string = "", options?: {
specialViz: Map<string, BaseUIElement>
}) {
if (configuration === undefined) {
2020-11-17 02:22:48 +01:00
throw "Trying to generate a tagRenderingAnswer without configuration..."
}
2021-11-08 14:18:45 +01:00
if (tagsSource === undefined) {
throw "Trying to generate a tagRenderingAnswer without tagSource..."
}
2021-06-14 02:39:23 +02:00
super(tagsSource.map(tags => {
2021-07-16 02:13:41 +02:00
if (tags === undefined) {
return undefined;
}
2021-07-16 02:13:41 +02:00
if (configuration.condition) {
if (!configuration.condition.matchesProperties(tags)) {
2021-06-15 01:24:04 +02:00
return undefined;
}
}
2021-07-16 02:13:41 +02:00
2021-06-14 02:39:23 +02:00
const trs = Utils.NoNull(configuration.GetRenderValues(tags));
2021-07-16 02:13:41 +02:00
if (trs.length === 0) {
return undefined;
}
const valuesToRender: BaseUIElement[] = trs.map(tr => {
const text = new SubstitutedTranslation(tr.then, tagsSource, state, options?.specialViz);
if(tr.icon === undefined){
return text
}
2022-02-17 23:54:14 +01:00
return new Combine([new Img(tr.icon).SetClass("mapping-icon-"+(tr.iconClass ?? "small")), text]).SetClass("flex items-center")
})
2021-07-16 02:13:41 +02:00
if (valuesToRender.length === 1) {
return valuesToRender[0];
} else if (valuesToRender.length > 1) {
return new Combine(valuesToRender).SetClass("flex flex-col")
}
2021-07-16 02:13:41 +02:00
return undefined;
}).map((element: BaseUIElement) => element?.SetClass(contentClasses)?.SetStyle(contentStyle)))
2021-07-16 02:13:41 +02:00
this.SetClass("flex items-center flex-row text-lg link-underline")
2021-06-14 02:39:23 +02:00
this.SetStyle("word-wrap: anywhere;");
2020-10-27 01:01:34 +01:00
}
}