mapcomplete/UI/SubstitutedTranslation.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-08-20 12:46:33 +02:00
import { UIEventSource } from "../Logic/UIEventSource"
2021-02-05 16:32:37 +01:00
import { Translation } from "./i18n/Translation"
import Locale from "./i18n/Locale"
import { FixedUiElement } from "./Base/FixedUiElement"
2023-03-28 05:13:48 +02:00
// import SpecialVisualizations from "./SpecialVisualizations"
2021-06-14 02:39:23 +02:00
import { Utils } from "../Utils"
import { VariableUiElement } from "./Base/VariableUIElement"
import Combine from "./Base/Combine"
import BaseUIElement from "./BaseUIElement"
2022-04-01 12:51:55 +02:00
import LinkToWeblate from "./Base/LinkToWeblate"
2023-03-28 05:13:48 +02:00
import { SpecialVisualization, SpecialVisualizationState } from "./SpecialVisualization"
2023-03-29 17:21:20 +02:00
import SpecialVisualizations from "./SpecialVisualizations"
import { Feature } from "geojson"
import LayerConfig from "../Models/ThemeConfig/LayerConfig"
2021-02-05 16:32:37 +01:00
2021-06-14 02:39:23 +02:00
export class SubstitutedTranslation extends VariableUiElement {
public constructor(
2021-02-05 16:32:37 +01:00
translation: Translation,
2022-08-20 12:46:33 +02:00
tagsSource: UIEventSource<Record<string, string>>,
2023-03-28 05:13:48 +02:00
state: SpecialVisualizationState,
mapping: Map<
string,
| BaseUIElement
2022-08-20 12:46:33 +02:00
| ((
2023-03-29 17:21:20 +02:00
state: SpecialVisualizationState,
2022-08-20 12:46:33 +02:00
tagSource: UIEventSource<Record<string, string>>,
argument: string[],
feature: Feature,
layer: LayerConfig
2022-08-20 12:46:33 +02:00
) => BaseUIElement)
> = undefined
) {
const extraMappings: SpecialVisualization[] = []
mapping?.forEach((value, key) => {
extraMappings.push({
funcName: key,
constr: typeof value === "function" ? value : () => value,
docs: "Dynamically injected input element",
args: [],
example: "",
})
})
2022-04-06 19:16:55 +02:00
const linkToWeblate =
translation !== undefined
? new LinkToWeblate(translation.context, translation.translations)
: undefined
2022-09-08 21:40:48 +02:00
2021-06-14 02:39:23 +02:00
super(
Locale.language.map((language) => {
let txt = translation?.textFor(language)
if (txt === undefined) {
2021-06-15 01:24:04 +02:00
return undefined
2021-06-14 02:39:23 +02:00
}
mapping?.forEach((_, key) => {
txt = txt.replace(new RegExp(`{${key}}`, "g"), `{${key}()}`)
})
2023-03-29 17:21:20 +02:00
const allElements = SpecialVisualizations.constructSpecification(
2022-04-01 12:51:55 +02:00
txt,
extraMappings
).map((proto) => {
2023-03-29 17:21:20 +02:00
if (typeof proto === "string") {
2022-07-08 03:14:55 +02:00
if (tagsSource === undefined) {
2023-03-29 17:21:20 +02:00
return Utils.SubstituteKeys(proto, undefined)
}
return new VariableUiElement(
2023-03-29 17:21:20 +02:00
tagsSource.map((tags) => Utils.SubstituteKeys(proto, tags))
2022-09-08 21:40:48 +02:00
)
}
2023-03-29 17:21:20 +02:00
const viz: {
func: SpecialVisualization
args: string[]
style: string
} = proto
if (viz === undefined) {
console.error(
"SPECIALRENDERING UNDEFINED for",
tagsSource.data?.id,
"THIS IS REALLY WEIRD"
)
return undefined
}
try {
2023-03-29 17:21:20 +02:00
const feature = state.indexedFeatures.featuresById.data.get(
tagsSource.data.id
)
return viz.func
.constr(
state,
tagsSource,
proto.args.map((t) => SpecialVisualizations.undoEncoding(t)),
feature,
undefined
)
2023-03-29 17:21:20 +02:00
?.SetStyle(proto.style)
} catch (e) {
console.error("SPECIALRENDERING FAILED for", tagsSource.data?.id, e)
2022-01-01 01:59:50 +01:00
return new FixedUiElement(
`Could not generate special rendering for ${
viz.func.funcName
}(${viz.args.join(", ")}) ${e}`
).SetStyle("alert")
}
2022-04-01 12:51:55 +02:00
})
allElements.push(linkToWeblate)
2022-09-08 21:40:48 +02:00
2022-04-01 12:51:55 +02:00
return new Combine(allElements)
})
2021-06-14 02:39:23 +02:00
)
this.SetClass("w-full")
2021-02-05 16:32:37 +01:00
}
}