Seperate logic to detect special renderings and to actually render them, add a minimap to all popups (if no minimap is defined seperately)

This commit is contained in:
Pieter Vander Vennet 2021-06-27 19:21:31 +02:00
parent ab0d510cdd
commit 284102c2f1
6 changed files with 118 additions and 57 deletions

View file

@ -3,8 +3,7 @@ import {Translation} from "./i18n/Translation";
import Locale from "./i18n/Locale";
import State from "../State";
import {FixedUiElement} from "./Base/FixedUiElement";
import SpecialVisualizations from "./SpecialVisualizations";
import BaseUIElement from "./BaseUIElement";
import SpecialVisualizations, {SpecialVisualization} from "./SpecialVisualizations";
import {Utils} from "../Utils";
import {VariableUiElement} from "./Base/VariableUIElement";
import Combine from "./Base/Combine";
@ -15,13 +14,26 @@ export class SubstitutedTranslation extends VariableUiElement {
translation: Translation,
tagsSource: UIEventSource<any>) {
super(
tagsSource.map(tags => {
const txt = Utils.SubstituteKeys(translation.txt, tags)
Locale.language.map(language => {
const txt = translation.textFor(language)
if (txt === undefined) {
return undefined
}
return new Combine(SubstitutedTranslation.EvaluateSpecialComponents(txt, tagsSource))
}, [Locale.language])
return new Combine(SubstitutedTranslation.ExtractSpecialComponents(txt).map(
proto => {
if (proto.fixed !== undefined) {
return new VariableUiElement(tagsSource.map(tags => Utils.SubstituteKeys(proto.fixed, tags)));
}
const viz = proto.special;
try {
return viz.func.constr(State.state, tagsSource, proto.special.args).SetStyle(proto.special.style);
} catch (e) {
console.error("SPECIALRENDERING FAILED for", tagsSource.data?.id, e)
return new FixedUiElement(`Could not generate special rendering for ${viz.func}(${viz.args.join(", ")}) ${e}`).SetStyle("alert")
}
}
))
})
)
@ -29,7 +41,13 @@ export class SubstitutedTranslation extends VariableUiElement {
}
private static EvaluateSpecialComponents(template: string, tags: UIEventSource<any>): BaseUIElement[] {
public static ExtractSpecialComponents(template: string): {
fixed?: string, special?: {
func: SpecialVisualization,
args: string[],
style: string
}
}[] {
for (const knownSpecial of SpecialVisualizations.specialVisualizations) {
@ -38,38 +56,29 @@ export class SubstitutedTranslation extends VariableUiElement {
if (matched != null) {
// We found a special component that should be brought to live
const partBefore = SubstitutedTranslation.EvaluateSpecialComponents(matched[1], tags);
const partBefore = SubstitutedTranslation.ExtractSpecialComponents(matched[1]);
const argument = matched[2].trim();
const style = matched[3]?.substring(1) ?? ""
const partAfter = SubstitutedTranslation.EvaluateSpecialComponents(matched[4], tags);
try {
const args = knownSpecial.args.map(arg => arg.defaultValue ?? "");
if (argument.length > 0) {
const realArgs = argument.split(",").map(str => str.trim());
for (let i = 0; i < realArgs.length; i++) {
if (args.length <= i) {
args.push(realArgs[i]);
} else {
args[i] = realArgs[i];
}
const partAfter = SubstitutedTranslation.ExtractSpecialComponents(matched[4]);
const args = knownSpecial.args.map(arg => arg.defaultValue ?? "");
if (argument.length > 0) {
const realArgs = argument.split(",").map(str => str.trim());
for (let i = 0; i < realArgs.length; i++) {
if (args.length <= i) {
args.push(realArgs[i]);
} else {
args[i] = realArgs[i];
}
}
let element: BaseUIElement = new FixedUiElement(`Constructing ${knownSpecial}(${args.join(", ")})`)
try {
element = knownSpecial.constr(State.state, tags, args);
element.SetStyle(style)
} catch (e) {
console.error("SPECIALRENDERING FAILED for", tags.data.id, e)
element = new FixedUiElement(`Could not generate special rendering for ${knownSpecial}(${args.join(", ")}) ${e}`).SetClass("alert")
}
return [...partBefore, element, ...partAfter]
} catch (e) {
console.error(e);
return [...partBefore, new FixedUiElement(`Failed loading ${knownSpecial.funcName}(${matched[2]}): ${e}`), ...partAfter]
}
let element;
element = {special:{
args: args,
style: style,
func: knownSpecial
}}
return [...partBefore, element, ...partAfter]
}
}
@ -80,7 +89,7 @@ export class SubstitutedTranslation extends VariableUiElement {
}
// IF we end up here, no changes have to be made - except to remove any resting {}
return [new FixedUiElement(template.replace(/{.*}/g, ""))];
return [{fixed: template}];
}
}