mapcomplete/Logic/Actors/TitleHandler.ts

50 lines
1.9 KiB
TypeScript

import { Store, UIEventSource } from "../UIEventSource"
import Locale from "../../UI/i18n/Locale"
import TagRenderingAnswer from "../../UI/Popup/TagRenderingAnswer"
import Combine from "../../UI/Base/Combine"
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
import { ElementStorage } from "../ElementStorage"
import { Utils } from "../../Utils"
export default class TitleHandler {
constructor(selectedElement: Store<any>, layout: LayoutConfig, allElements: ElementStorage) {
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.getEventSourceById(tags.id) ?? new UIEventSource<any>(tags)
const title = new TagRenderingAnswer(tagsSource, layer.title, {})
return (
new Combine([defaultTitle, " | ", title]).ConstructElement()
?.textContent ?? defaultTitle
)
}
}
return defaultTitle
},
[Locale.language]
)
currentTitle.addCallbackAndRunD((title) => {
if (Utils.runningFromConsole) {
return
}
try {
document.title = title
} catch (e) {
console.error(e)
}
})
}
}