mapcomplete/Logic/Actors/TitleHandler.ts

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

65 lines
2.5 KiB
TypeScript
Raw Normal View History

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