mapcomplete/Logic/Actors/TitleHandler.ts

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

61 lines
2.2 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 || selectedLayer.data === undefined) {
return defaultTitle
}
2021-03-21 02:03:07 +01:00
const tags = selected.properties
const layer = selectedLayer.data
if (layer.title === undefined) {
return defaultTitle
}
const tagsSource =
allElements.getStore(tags.id) ?? new UIEventSource<Record<string, string>>(tags)
const title = new SvelteUIElement(TagRenderingAnswer, {
tags: tagsSource,
state,
config: layer.title,
selectedElement: selectedElement.data,
layer,
})
return (
new Combine([defaultTitle, " | ", title]).ConstructElement()?.textContent ??
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
}
}