mapcomplete/Logic/Actors/TitleHandler.ts

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

57 lines
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 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"
2021-01-08 18:02:07 +01:00
export default class TitleHandler {
2021-11-07 16:34:51 +01:00
constructor(state: {
selectedElement: Store<any>
layoutToUse: LayoutConfig
allElements: ElementStorage
}) {
const currentTitle: Store<string> = state.selectedElement.map(
(selected) => {
const layout = state.layoutToUse
const defaultTitle = layout?.title?.txt ?? "MapComplete"
if (selected === undefined) {
return defaultTitle
}
2021-03-21 02:03:07 +01:00
const tags = selected.properties
for (const layer of 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 =
state.allElements.getEventSourceById(tags.id) ??
new UIEventSource<any>(tags)
2021-12-21 18:35:31 +01:00
const title = new TagRenderingAnswer(tagsSource, layer.title, {})
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
},
[Locale.language]
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
}
}