MapComplete/src/Logic/Actors/TitleHandler.ts

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

54 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-09-27 03:19:31 +02:00
import { Store } from "../UIEventSource"
2021-01-08 18:02:07 +01:00
import Locale from "../../UI/i18n/Locale"
import { Utils } from "../../Utils"
2023-03-28 05:13:48 +02:00
import { Feature } from "geojson"
2023-03-31 03:28:11 +02:00
import { SpecialVisualizationState } from "../../UI/SpecialVisualization"
2021-01-08 18:02:07 +01:00
export default class TitleHandler {
2023-03-28 05:13:48 +02:00
constructor(
selectedElement: Store<Feature>,
2023-03-31 03:28:11 +02:00
state: SpecialVisualizationState
2023-03-28 05:13:48 +02:00
) {
2023-03-25 02:48:24 +01:00
const currentTitle: Store<string> = selectedElement.map(
(selected) => {
2024-09-27 03:19:31 +02:00
const lng = Locale.language.data
const defaultTitle = state.layout?.title?.textFor(lng) ?? "MapComplete"
if (selected === undefined) {
return defaultTitle
}
2024-09-27 03:19:31 +02:00
const layer = state.layout.getMatchingLayer(selected.properties)
if (layer === undefined) {
return defaultTitle
}
2021-03-21 02:03:07 +01:00
const tags = selected.properties
if (layer.title === undefined) {
return defaultTitle
}
2024-09-27 03:19:31 +02:00
const toRender = Utils.NoNull(layer?.title?.GetRenderValues(tags))
const titleUnsubbed = toRender[0]?.then?.textFor(lng)
if (titleUnsubbed === undefined) {
return defaultTitle
}
const title = Utils.SubstituteKeys(titleUnsubbed, tags)
const el = document.createElement("span")
el.innerHTML = title
return el.innerText + " | " + defaultTitle
},
[Locale.language]
2021-06-11 22:51:45 +02:00
)
currentTitle.addCallbackAndRunD((title) => {
2021-11-07 16:34:51 +01:00
if (Utils.runningFromConsole) {
return
}
2022-10-27 01:50:41 +02:00
try {
document.title = title
} catch (e) {
2022-09-20 16:03:28 +02:00
console.error(e)
}
2021-03-21 02:03:07 +01:00
})
2021-01-08 18:02:07 +01:00
}
}