mapcomplete/UI/Popup/TagRenderingAnswer.ts

97 lines
3.8 KiB
TypeScript
Raw Permalink Normal View History

2020-10-27 00:01:34 +00:00
import {UIEventSource} from "../../Logic/UIEventSource";
import TagRenderingConfig from "../../Customizations/JSON/TagRenderingConfig";
import {UIElement} from "../UIElement";
import {Utils} from "../../Utils";
import Combine from "../Base/Combine";
2021-02-05 15:32:37 +00:00
import {SubstitutedTranslation} from "../SubstitutedTranslation";
import {Translation} from "../i18n/Translation";
2021-03-28 22:41:53 +00:00
import {TagUtils} from "../../Logic/Tags/TagUtils";
2020-10-27 00:01:34 +00:00
/***
* Displays the correct value for a known tagrendering
*/
export default class TagRenderingAnswer extends UIElement {
private readonly _tags: UIEventSource<any>;
2020-10-27 00:01:34 +00:00
private _configuration: TagRenderingConfig;
private _content: UIElement;
private readonly _contentClass: string;
private _contentStyle: string;
2020-10-27 00:01:34 +00:00
constructor(tags: UIEventSource<any>, configuration: TagRenderingConfig, contentClasses: string = "", contentStyle: string = "") {
2020-10-27 00:01:34 +00:00
super(tags);
this._tags = tags;
this._configuration = configuration;
this._contentClass = contentClasses;
this._contentStyle = contentStyle;
if (configuration === undefined) {
2020-11-17 01:22:48 +00:00
throw "Trying to generate a tagRenderingAnswer without configuration..."
}
2021-04-12 11:05:30 +00:00
this.SetClass("flex items-center flex-row text-lg link-underline")
this.SetStyle("word-wrap: anywhere;");
2020-10-27 00:01:34 +00:00
}
InnerRender(): string {
2020-11-02 17:59:21 +00:00
if (this._configuration.condition !== undefined) {
if (!this._configuration.condition.matchesProperties(this._tags.data)) {
return "";
}
}
2020-11-13 22:58:11 +00:00
const tags = this._tags.data;
if (tags === undefined) {
return "";
}
// The render value doesn't work well with multi-answers (checkboxes), so we have to check for them manually
if (this._configuration.multiAnswer) {
let freeformKeyUsed = this._configuration.freeform?.key === undefined; // If it is undefined, it is "used" already, or at least we don't have to check for it anymore
2021-04-18 12:24:30 +00:00
const applicableThens: Translation[] = Utils.NoNull(this._configuration.mappings?.map(mapping => {
if (mapping.if === undefined) {
return mapping.then;
}
if (TagUtils.MatchesMultiAnswer(mapping.if, tags)) {
if(!freeformKeyUsed){
if(mapping.if.usedKeys().indexOf(this._configuration.freeform.key) >= 0){
freeformKeyUsed = true;
}
}
return mapping.then;
}
return undefined;
2021-04-18 12:24:30 +00:00
}) ?? [])
if (!freeformKeyUsed
&& tags[this._configuration.freeform.key] !== undefined) {
applicableThens.push(this._configuration.render)
}
const self = this
const valuesToRender: UIElement[] = applicableThens.map(tr => SubstitutedTranslation.construct(tr, self._tags))
if (valuesToRender.length >= 0) {
if (valuesToRender.length === 1) {
this._content = valuesToRender[0];
} else {
this._content = new Combine(["<ul>",
...valuesToRender.map(tr => new Combine(["<li>", tr, "</li>"]))
,
"</ul>"
])
}
return this._content.SetClass(this._contentClass).SetStyle(this._contentStyle).Render();
}
2020-10-27 00:01:34 +00:00
}
2021-01-21 22:39:31 +00:00
const tr = this._configuration.GetRenderValue(tags);
if (tr !== undefined) {
2021-02-05 15:32:37 +00:00
this._content = SubstitutedTranslation.construct(tr, this._tags);
return this._content.SetClass(this._contentClass).SetStyle(this._contentStyle).Render();
2021-01-21 22:39:31 +00:00
}
return "";
2020-10-27 00:01:34 +00:00
}
}