MapComplete/Logic/Actors/TitleHandler.ts

60 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import { Store, UIEventSource } from "../UIEventSource"
import Locale from "../../UI/i18n/Locale"
import Combine from "../../UI/Base/Combine"
import { Utils } from "../../Utils"
2023-03-28 05:13:48 +02:00
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import { Feature } from "geojson"
import FeaturePropertiesStore from "../FeatureSource/Actors/FeaturePropertiesStore"
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
2023-03-29 17:21:20 +02:00
import SvelteUIElement from "../../UI/Base/SvelteUIElement"
import TagRenderingAnswer from "../../UI/Popup/TagRenderingAnswer.svelte"
2021-01-08 18:02:07 +01:00
export default class TitleHandler {
2023-03-28 05:13:48 +02:00
constructor(
selectedElement: Store<Feature>,
selectedLayer: Store<LayerConfig>,
allElements: FeaturePropertiesStore,
layout: LayoutConfig
) {
2023-03-25 02:48:24 +01:00
const currentTitle: Store<string> = selectedElement.map(
2022-09-08 21:40:48 +02:00
(selected) => {
const defaultTitle = layout?.title?.txt ?? "MapComplete"
if (selected === undefined) {
return defaultTitle
}
2021-03-21 02:03:07 +01:00
2022-09-08 21:40:48 +02:00
const tags = selected.properties
2023-03-28 05:13:48 +02:00
for (const layer of layout?.layers ?? []) {
if (layer.title === undefined) {
2022-09-08 21:40:48 +02:00
continue
2021-06-11 22:51:45 +02:00
}
if (layer.source.osmTags.matchesProperties(tags)) {
2022-09-08 21:40:48 +02:00
const tagsSource =
2023-03-28 05:13:48 +02:00
allElements.getStore(tags.id) ??
new UIEventSource<Record<string, string>>(tags)
2023-03-29 17:21:20 +02:00
const title = new SvelteUIElement(TagRenderingAnswer, { tags: tagsSource })
2022-09-08 21:40:48 +02:00
return (
new Combine([defaultTitle, " | ", title]).ConstructElement()
?.textContent ?? defaultTitle
)
2021-06-11 22:51:45 +02:00
}
}
return defaultTitle
2022-09-08 21:40:48 +02:00
},
2023-03-28 05:13:48 +02:00
[Locale.language, selectedLayer]
2021-06-11 22:51:45 +02:00
)
2022-09-08 21:40:48 +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
}
2022-09-08 21:40:48 +02:00
}