mapcomplete/UI/Popup/TagRenderingAnswer.ts

47 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-10-27 01:01:34 +01:00
import {UIEventSource} from "../../Logic/UIEventSource";
import TagRenderingConfig from "../../Customizations/JSON/TagRenderingConfig";
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";
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
2021-06-14 02:39:23 +02:00
constructor(tagsSource: UIEventSource<any>, configuration: TagRenderingConfig, contentClasses: string = "", contentStyle: string = "") {
if (configuration === undefined) {
2020-11-17 02:22:48 +01:00
throw "Trying to generate a tagRenderingAnswer without configuration..."
}
2021-06-14 02:39:23 +02:00
super(tagsSource.map(tags => {
if(tags === undefined){
return undefined;
}
2021-06-15 01:24:04 +02:00
if(configuration.condition){
if(!configuration.condition.matchesProperties(tags)){
return undefined;
}
}
2021-06-14 02:39:23 +02:00
const trs = Utils.NoNull(configuration.GetRenderValues(tags));
if(trs.length === 0){
return undefined;
}
2021-06-14 17:28:11 +02:00
const valuesToRender: BaseUIElement[] = trs.map(tr => new SubstitutedTranslation(tr, tagsSource))
if(valuesToRender.length === 1){
return valuesToRender[0];
}else if(valuesToRender.length > 1){
return new List(valuesToRender)
}
return undefined;
}).map((element : BaseUIElement) => element?.SetClass(contentClasses)?.SetStyle(contentStyle)))
2021-06-14 17:28:11 +02:00
this.SetClass("flex items-center flex-row text-lg link-underline tag-renering-answer")
2021-06-14 02:39:23 +02:00
this.SetStyle("word-wrap: anywhere;");
2020-10-27 01:01:34 +01:00
}
}