mapcomplete/Logic/Actors/TitleHandler.ts

71 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-01-08 18:02:07 +01:00
import {UIEventSource} from "../UIEventSource";
import LayoutConfig from "../../Customizations/JSON/LayoutConfig";
import Translations from "../../UI/i18n/Translations";
import Locale from "../../UI/i18n/Locale";
import {UIElement} from "../../UI/UIElement";
2021-01-25 03:12:09 +01:00
import TagRenderingAnswer from "../../UI/Popup/TagRenderingAnswer";
import {ElementStorage} from "../ElementStorage";
import Combine from "../../UI/Base/Combine";
2021-01-08 18:02:07 +01:00
2021-01-25 03:12:09 +01:00
class TitleElement extends UIElement {
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-01-25 03:12:09 +01:00
super(layoutToUse);
this._layoutToUse = layoutToUse;
this._selectedFeature = selectedFeature;
this._allElementsStorage = allElementsStorage;
this.ListenTo(Locale.language);
2021-03-21 02:03:07 +01:00
this.ListenTo(this._selectedFeature)
2021-01-25 03:12:09 +01:00
this.dumbMode = false;
}
2021-03-21 02:03:07 +01:00
2021-01-25 03:12:09 +01:00
InnerRender(): string {
2021-03-21 02:03:07 +01:00
2021-01-25 03:12:09 +01:00
const defaultTitle = Translations.WT(this._layoutToUse.data?.title)?.txt ?? "MapComplete"
const feature = this._selectedFeature.data;
2021-03-21 02:03:07 +01:00
if (feature === undefined) {
2021-01-25 03:12:09 +01:00
return defaultTitle;
}
const layout = this._layoutToUse.data;
const properties = this._selectedFeature.data.properties;
2021-03-21 02:03:07 +01:00
for (const layer of layout.layers) {
if (layer.title === undefined) {
continue;
}
2021-03-21 02:16:39 +01:00
if (layer.source.osmTags.matchesProperties(properties)) {
2021-01-25 03:12:09 +01:00
const title = new TagRenderingAnswer(
this._allElementsStorage.addOrGetElement(feature),
2021-01-25 03:12:09 +01:00
layer.title
)
2021-03-21 02:03:07 +01:00
return new Combine([defaultTitle, " | ", title]).Render();
2021-01-08 18:02:07 +01:00
}
2021-01-25 03:12:09 +01:00
}
return defaultTitle;
}
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) {
2021-01-08 18:02:07 +01:00
2021-03-21 02:03:07 +01:00
selectedFeature.addCallbackAndRun(_ => {
const title = new TitleElement(layoutToUse, selectedFeature, allElementsStorage)
const d = document.createElement('div');
2021-03-22 00:28:10 +01:00
d.innerHTML = title.InnerRender();
2021-03-21 02:03:07 +01:00
// We pass everything into a div to strip out images etc...
document.title = (d.textContent || d.innerText);
})
2021-01-08 18:02:07 +01:00
}
}