mapcomplete/UI/SubstitutedTranslation.ts

84 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-02-05 16:32:37 +01:00
import {UIEventSource} from "../Logic/UIEventSource";
import {Translation} from "./i18n/Translation";
import Locale from "./i18n/Locale";
import State from "../State";
import {FixedUiElement} from "./Base/FixedUiElement";
import SpecialVisualizations from "./SpecialVisualizations";
2021-06-11 22:51:45 +02:00
import BaseUIElement from "./BaseUIElement";
2021-06-14 02:39:23 +02:00
import {Utils} from "../Utils";
import {VariableUiElement} from "./Base/VariableUIElement";
import Combine from "./Base/Combine";
2021-02-05 16:32:37 +01:00
2021-06-14 02:39:23 +02:00
export class SubstitutedTranslation extends VariableUiElement {
2021-02-05 16:32:37 +01:00
2021-06-14 02:39:23 +02:00
public constructor(
2021-02-05 16:32:37 +01:00
translation: Translation,
2021-06-15 01:24:04 +02:00
tagsSource: UIEventSource<any>) {
2021-06-14 02:39:23 +02:00
super(
2021-06-15 01:24:04 +02:00
tagsSource.map(tags => {
2021-06-14 02:39:23 +02:00
const txt = Utils.SubstituteKeys(translation.txt, tags)
if (txt === undefined) {
2021-06-15 01:24:04 +02:00
return undefined
2021-06-14 02:39:23 +02:00
}
2021-06-15 01:24:04 +02:00
return new Combine(SubstitutedTranslation.EvaluateSpecialComponents(txt, tagsSource))
2021-06-14 02:39:23 +02:00
}, [Locale.language])
)
this.SetClass("w-full")
2021-02-05 16:32:37 +01:00
}
2021-06-14 02:39:23 +02:00
private static EvaluateSpecialComponents(template: string, tags: UIEventSource<any>): BaseUIElement[] {
2021-02-05 16:32:37 +01:00
for (const knownSpecial of SpecialVisualizations.specialVisualizations) {
// Note: the '.*?' in the regex reads as 'any character, but in a non-greedy way'
const matched = template.match(`(.*){${knownSpecial.funcName}\\((.*?)\\)}(.*)`);
if (matched != null) {
// We found a special component that should be brought to live
2021-06-14 02:39:23 +02:00
const partBefore = SubstitutedTranslation.EvaluateSpecialComponents(matched[1], tags);
2021-02-05 16:32:37 +01:00
const argument = matched[2].trim();
2021-06-14 02:39:23 +02:00
const partAfter = SubstitutedTranslation.EvaluateSpecialComponents(matched[3], tags);
2021-02-05 16:32:37 +01:00
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];
}
}
}
2021-06-14 02:39:23 +02:00
let element: BaseUIElement = new FixedUiElement(`Constructing ${knownSpecial}(${args.join(", ")})`)
try{
element = knownSpecial.constr(State.state, tags, args);
}catch(e){
2021-06-15 01:24:04 +02:00
console.error("SPECIALRENDERING FAILED for", tags.data.id, e)
2021-06-14 02:39:23 +02:00
element = new FixedUiElement(`Could not generate special renering for ${knownSpecial}(${args.join(", ")}) ${e}`).SetClass("alert")
}
2021-02-05 16:32:37 +01:00
return [...partBefore, element, ...partAfter]
} catch (e) {
console.error(e);
return [...partBefore, new FixedUiElement(`Failed loading ${knownSpecial.funcName}(${matched[2]}): ${e}`), ...partAfter]
}
}
}
2021-04-23 17:22:01 +02:00
// Let's to a small sanity check to help the theme designers:
2021-06-10 01:36:20 +02:00
if (template.search(/{[^}]+\([^}]*\)}/) >= 0) {
2021-04-23 17:22:01 +02:00
// Hmm, we might have found an invalid rendering name
2021-06-10 01:36:20 +02:00
console.warn("Found a suspicious special rendering value in: ", template, " did you mean one of: ", SpecialVisualizations.specialVisualizations.map(sp => sp.funcName + "()").join(", "))
2021-04-23 17:22:01 +02:00
}
2021-06-10 01:36:20 +02:00
2021-04-06 21:10:18 +02:00
// IF we end up here, no changes have to be made - except to remove any resting {}
return [new FixedUiElement(template.replace(/{.*}/g, ""))];
2021-02-05 16:32:37 +01:00
}
}