mapcomplete/Logic/Actors/TitleHandler.ts

67 lines
2.4 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 {ElementStorage} from "../ElementStorage";
import Combine from "../../UI/Base/Combine";
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig";
2021-01-08 18:02:07 +01:00
2021-06-11 22:51:45 +02:00
class TitleElement extends UIEventSource<string> {
2021-01-25 03:12:09 +01:00
private readonly _layoutToUse: UIEventSource<LayoutConfig>;
private readonly _selectedFeature: UIEventSource<any>;
private readonly _allElementsStorage: ElementStorage;
2021-03-21 02:03:07 +01:00
2021-01-25 03:12:09 +01:00
constructor(layoutToUse: UIEventSource<LayoutConfig>,
selectedFeature: UIEventSource<any>,
2021-03-21 02:03:07 +01:00
allElementsStorage: ElementStorage) {
2021-06-11 22:51:45 +02:00
super("MapComplete");
2021-01-25 03:12:09 +01:00
this._layoutToUse = layoutToUse;
this._selectedFeature = selectedFeature;
this._allElementsStorage = allElementsStorage;
2021-06-11 22:51:45 +02:00
this.syncWith(
this._selectedFeature.map(
selected => {
const defaultTitle = Translations.WT(this._layoutToUse.data?.title)?.txt ?? "MapComplete"
2021-03-21 02:03:07 +01:00
if (selected === undefined) {
2021-06-11 22:51:45 +02:00
return defaultTitle
}
2021-01-25 03:12:09 +01:00
2021-06-11 22:51:45 +02:00
const layout = layoutToUse.data;
const tags = selected.properties;
2021-01-25 03:12:09 +01:00
2021-06-11 22:51:45 +02:00
for (const layer of layout.layers) {
if (layer.title === undefined) {
continue;
}
if (layer.source.osmTags.matchesProperties(tags)) {
2021-06-12 02:58:32 +02:00
const tagsSource = allElementsStorage.getEventSourceById(tags.id)
const title = new TagRenderingAnswer(tagsSource, layer.title)
2021-06-11 22:51:45 +02:00
return new Combine([defaultTitle, " | ", title]).ConstructElement().innerText;
}
}
2021-01-25 03:12:09 +01:00
2021-06-11 22:51:45 +02:00
return defaultTitle
}
2021-06-11 22:51:45 +02:00
, [Locale.language, layoutToUse]
)
)
2021-01-25 03:12:09 +01:00
}
2021-03-21 02:03:07 +01:00
2021-01-25 03:12:09 +01:00
}
export default class TitleHandler {
constructor(layoutToUse: UIEventSource<LayoutConfig>,
selectedFeature: UIEventSource<any>,
2021-03-21 02:03:07 +01:00
allElementsStorage: ElementStorage) {
new TitleElement(layoutToUse, selectedFeature, allElementsStorage).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
}
}