mapcomplete/Logic/Actors/TitleHandler.ts

44 lines
1.7 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";
2021-01-08 18:02:07 +01:00
export default class TitleHandler {
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)) {
const tagsSource = state.allElements.getEventSourceById(tags.id)
const title = new TagRenderingAnswer(tagsSource, layer.title)
return new Combine([defaultTitle, " | ", title]).ConstructElement().innerText;
2021-06-11 22:51:45 +02:00
}
}
return defaultTitle
}, [Locale.language]
2021-06-11 22:51:45 +02:00
)
currentTitle.addCallbackAndRunD(title => {
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
}
}