mapcomplete/Logic/Actors/TitleHandler.ts

59 lines
2.2 KiB
TypeScript

import { Store, UIEventSource } from "../UIEventSource"
import Locale from "../../UI/i18n/Locale"
import Combine from "../../UI/Base/Combine"
import { Utils } from "../../Utils"
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import { Feature } from "geojson"
import FeaturePropertiesStore from "../FeatureSource/Actors/FeaturePropertiesStore"
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
import SvelteUIElement from "../../UI/Base/SvelteUIElement"
import TagRenderingAnswer from "../../UI/Popup/TagRenderingAnswer.svelte"
export default class TitleHandler {
constructor(
selectedElement: Store<Feature>,
selectedLayer: Store<LayerConfig>,
allElements: FeaturePropertiesStore,
layout: LayoutConfig
) {
const currentTitle: Store<string> = selectedElement.map(
(selected) => {
const defaultTitle = layout?.title?.txt ?? "MapComplete"
if (selected === undefined) {
return defaultTitle
}
const tags = selected.properties
for (const layer of layout?.layers ?? []) {
if (layer.title === undefined) {
continue
}
if (layer.source.osmTags.matchesProperties(tags)) {
const tagsSource =
allElements.getStore(tags.id) ??
new UIEventSource<Record<string, string>>(tags)
const title = new SvelteUIElement(TagRenderingAnswer, { tags: tagsSource })
return (
new Combine([defaultTitle, " | ", title]).ConstructElement()
?.textContent ?? defaultTitle
)
}
}
return defaultTitle
},
[Locale.language, selectedLayer]
)
currentTitle.addCallbackAndRunD((title) => {
if (Utils.runningFromConsole) {
return
}
try {
document.title = title
} catch (e) {
console.error(e)
}
})
}
}