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"
|
2021-07-11 15:44:17 +02:00
|
|
|
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"
|
2023-04-02 02:59:20 +02:00
|
|
|
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,
|
2022-05-27 05:49:21 +02:00
|
|
|
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[],
|
2023-04-02 02:59:20 +02:00
|
|
|
feature: Feature,
|
|
|
|
layer: LayerConfig
|
2022-08-20 12:46:33 +02:00
|
|
|
) => BaseUIElement)
|
|
|
|
> = undefined
|
|
|
|
) {
|
2021-07-11 15:44:17 +02:00
|
|
|
const extraMappings: SpecialVisualization[] = []
|
|
|
|
|
|
|
|
mapping?.forEach((value, key) => {
|
|
|
|
extraMappings.push({
|
|
|
|
funcName: key,
|
2022-05-27 05:49:21 +02:00
|
|
|
constr: typeof value === "function" ? value : () => value,
|
2021-07-11 15:44:17 +02:00
|
|
|
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(
|
2021-06-27 19:21:31 +02:00
|
|
|
Locale.language.map((language) => {
|
2022-02-01 04:14:54 +01:00
|
|
|
let txt = translation?.textFor(language)
|
2021-06-23 02:15:28 +02:00
|
|
|
if (txt === undefined) {
|
2021-06-15 01:24:04 +02:00
|
|
|
return undefined
|
2021-06-14 02:39:23 +02:00
|
|
|
}
|
2021-07-11 15:44:17 +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
|
2021-06-27 19:21:31 +02:00
|
|
|
).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)
|
2021-06-27 19:21:31 +02:00
|
|
|
}
|
|
|
|
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
|
2022-07-27 23:59:04 +02:00
|
|
|
if (viz === undefined) {
|
|
|
|
console.error(
|
|
|
|
"SPECIALRENDERING UNDEFINED for",
|
|
|
|
tagsSource.data?.id,
|
|
|
|
"THIS IS REALLY WEIRD"
|
|
|
|
)
|
|
|
|
return undefined
|
|
|
|
}
|
2021-06-27 19:21:31 +02:00
|
|
|
try {
|
2023-03-29 17:21:20 +02:00
|
|
|
const feature = state.indexedFeatures.featuresById.data.get(
|
|
|
|
tagsSource.data.id
|
|
|
|
)
|
2022-04-28 00:32:15 +02:00
|
|
|
return viz.func
|
2023-06-14 20:39:36 +02:00
|
|
|
.constr(
|
|
|
|
state,
|
|
|
|
tagsSource,
|
|
|
|
proto.args.map((t) => SpecialVisualizations.undoEncoding(t)),
|
|
|
|
feature,
|
|
|
|
undefined
|
|
|
|
)
|
2023-03-29 17:21:20 +02:00
|
|
|
?.SetStyle(proto.style)
|
2021-06-27 19:21:31 +02:00
|
|
|
} 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")
|
2021-06-27 19:21:31 +02:00
|
|
|
}
|
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-27 19:21:31 +02:00
|
|
|
})
|
2021-06-14 02:39:23 +02:00
|
|
|
)
|
2021-06-23 02:15:28 +02:00
|
|
|
|
2021-02-06 00:05:38 +01:00
|
|
|
this.SetClass("w-full")
|
2021-02-05 16:32:37 +01:00
|
|
|
}
|
|
|
|
}
|