mapcomplete/Logic/Actors/TitleHandler.ts

48 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-01-08 18:02:07 +01:00
import {UIEventSource} from "../UIEventSource";
import Translations from "../../UI/i18n/Translations";
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: UIEventSource<any>,
layoutToUse: LayoutConfig,
allElements: ElementStorage
}) {
const currentTitle: UIEventSource<string> = state.selectedElement.map(
selected => {
const layout = state.layoutToUse
const defaultTitle = Translations.WT(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, {})
2021-09-29 01:12:38 +02:00
return new Combine([defaultTitle, " | ", title]).ConstructElement()?.innerText ?? defaultTitle;
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
}
2021-06-11 22:51:45 +02:00
document.title = title
2021-03-21 02:03:07 +01:00
})
2021-01-08 18:02:07 +01:00
}
}