From e65f61d2962eba8301afa51e27f0e085e8db2ff7 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 5 Nov 2024 19:08:10 +0100 Subject: [PATCH] Fix: show favourites in loaded layers if favourites are enabled --- src/Models/FilteredLayer.ts | 28 +++++++++++++++++----------- src/Models/GlobalFilter.ts | 4 ++++ src/Models/ThemeViewState.ts | 32 +++++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/Models/FilteredLayer.ts b/src/Models/FilteredLayer.ts index 420935051..545842963 100644 --- a/src/Models/FilteredLayer.ts +++ b/src/Models/FilteredLayer.ts @@ -36,7 +36,7 @@ export default class FilteredLayer { constructor( layer: LayerConfig, appliedFilters?: ReadonlyMap>, - isDisplayed?: UIEventSource + isDisplayed?: UIEventSource, ) { this.layerDef = layer this.isDisplayed = isDisplayed ?? new UIEventSource(true) @@ -82,25 +82,25 @@ export default class FilteredLayer { layer: LayerConfig, context: string, osmConnection: OsmConnection, - enabledByDefault?: Store + enabledByDefault?: Store, ) { let isDisplayed: UIEventSource if (layer.syncSelection === "local") { isDisplayed = LocalStorageSource.getParsed( context + "-layer-" + layer.id + "-enabled", - layer.shownByDefault + layer.shownByDefault, ) } else if (layer.syncSelection === "theme-only") { isDisplayed = FilteredLayer.getPref( osmConnection, context + "-layer-" + layer.id + "-enabled", - layer + layer, ) } else if (layer.syncSelection === "global") { isDisplayed = FilteredLayer.getPref( osmConnection, "layer-" + layer.id + "-enabled", - layer + layer, ) } else { let isShown = layer.shownByDefault @@ -110,7 +110,7 @@ export default class FilteredLayer { isDisplayed = QueryParameters.GetBooleanQueryParameter( FilteredLayer.queryParameterKey(layer), isShown, - "Whether or not layer " + layer.id + " is shown" + "Whether or not layer " + layer.id + " is shown", ) } @@ -145,7 +145,7 @@ export default class FilteredLayer { */ private static fieldsToTags( option: FilterConfigOption, - fieldstate: string | Record + fieldstate: string | Record, ): TagsFilter | undefined { let properties: Record if (typeof fieldstate === "string") { @@ -181,7 +181,7 @@ export default class FilteredLayer { private static getPref( osmConnection: OsmConnection, key: string, - layer: LayerConfig + layer: LayerConfig, ): UIEventSource { return osmConnection.GetPreference(key, layer.shownByDefault + "").sync( (v) => { @@ -196,7 +196,7 @@ export default class FilteredLayer { return undefined } return "" + b - } + }, ) } @@ -216,8 +216,14 @@ export default class FilteredLayer { } for (const globalFilter of globalFilters ?? []) { const neededTags = globalFilter.osmTags - if (neededTags !== undefined && !neededTags.matchesProperties(properties)) { - return false + if (neededTags !== undefined) { + const doesMatch = neededTags.matchesProperties(properties) + if (globalFilter.forceShowOnMatch) { + return doesMatch || this.isDisplayed.data + } + if (!doesMatch) { + return false + } } } { diff --git a/src/Models/GlobalFilter.ts b/src/Models/GlobalFilter.ts index ce837acca..280172316 100644 --- a/src/Models/GlobalFilter.ts +++ b/src/Models/GlobalFilter.ts @@ -4,6 +4,10 @@ import { TagsFilter } from "../Logic/Tags/TagsFilter" export interface GlobalFilter { osmTags: TagsFilter + /** + * If set, this object will be shown instead of hidden, even if the layer is not displayed + */ + forceShowOnMatch?: boolean, state: number | string | undefined id: string onNewPoint: { diff --git a/src/Models/ThemeViewState.ts b/src/Models/ThemeViewState.ts index e91dffcf6..5ec6203ce 100644 --- a/src/Models/ThemeViewState.ts +++ b/src/Models/ThemeViewState.ts @@ -76,6 +76,7 @@ import { GeocodeResult, GeocodingUtils } from "../Logic/Search/GeocodingProvider import SearchState from "../Logic/State/SearchState" import { ShowDataLayerOptions } from "../UI/Map/ShowDataLayerOptions" import { PanoramaxUploader } from "../Logic/ImageProviders/Panoramax" +import { Tag } from "../Logic/Tags/Tag" /** * @@ -443,9 +444,16 @@ export default class ThemeViewState implements SpecialVisualizationState { const filteringFeatureSource = new Map() this.perLayer.forEach((fs, layerName) => { const doShowLayer = this.mapProperties.zoom.map( - (z) => - (fs.layer.isDisplayed?.data ?? true) && z >= (fs.layer.layerDef?.minzoom ?? 0), - [fs.layer.isDisplayed] + (z) => { + if ((fs.layer.isDisplayed?.data ?? true) && z >= (fs.layer.layerDef?.minzoom ?? 0)){ + return true + } + if(this.layerState.globalFilters.data.some(f => f.forceShowOnMatch)){ + return true + } + return false + }, + [fs.layer.isDisplayed, this.layerState.globalFilters] ) if (!doShowLayer.data && this.featureSwitches.featureSwitchFilter.data === false) { @@ -984,6 +992,24 @@ export default class ThemeViewState implements SpecialVisualizationState { this.userRelatedState.showScale.addCallbackAndRun((showScale) => { this.mapProperties.showScale.set(showScale) }) + + + this.layerState.filteredLayers.get("favourite").isDisplayed.addCallbackAndRunD(favouritesShown => { + const oldGlobal = this.layerState.globalFilters.data + const key = "show-favourite" + if(favouritesShown){ + this.layerState.globalFilters.set([...oldGlobal, { + forceShowOnMatch: true, + id:key, + osmTags: new Tag("_favourite","yes"), + state: 0, + onNewPoint: undefined + }]) + }else{ + this.layerState.globalFilters.set(oldGlobal.filter(gl => gl.id !== key)) + } + }) + new ThemeViewStateHashActor(this) new MetaTagging(this) new TitleHandler(this.selectedElement, this)