forked from MapComplete/MapComplete
		
	Refactoring: fix background layer switch and hotkeys
This commit is contained in:
		
							parent
							
								
									c2b9a81c51
								
							
						
					
					
						commit
						23b26c4197
					
				
					 5 changed files with 44 additions and 64 deletions
				
			
		| 
						 | 
				
			
			@ -23,20 +23,24 @@ export default class BackgroundLayerResetter {
 | 
			
		|||
        availableLayers.addCallbackAndRunD((availableLayers) => {
 | 
			
		||||
            // We only check on move/on change of the availableLayers
 | 
			
		||||
            const currentBgPolygon: RasterLayerPolygon | undefined = currentBackgroundLayer.data
 | 
			
		||||
            if (currentBackgroundLayer === undefined) {
 | 
			
		||||
                return
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (availableLayers.findIndex((available) => currentBgPolygon == available) >= 0) {
 | 
			
		||||
                // Still available!
 | 
			
		||||
                return
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            console.log("Current layer properties:", currentBgPolygon)
 | 
			
		||||
            // Oops, we panned out of range for this layer!
 | 
			
		||||
            // What is the 'best' map of the same category which is available?
 | 
			
		||||
            const availableInSameCat = RasterLayerUtils.SelectBestLayerAccordingTo(
 | 
			
		||||
                availableLayers,
 | 
			
		||||
                currentBgPolygon?.properties?.category ?? "osmbasedmap"
 | 
			
		||||
                currentBgPolygon?.properties?.category
 | 
			
		||||
            )
 | 
			
		||||
            console.log("Selecting a different layer:", availableInSameCat.properties.id)
 | 
			
		||||
            currentBackgroundLayer.setData(availableInSameCat ?? AvailableRasterLayers.osmCarto)
 | 
			
		||||
            currentBackgroundLayer.setData(availableInSameCat)
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -71,7 +71,7 @@ export default class MetaTagging {
 | 
			
		|||
            return
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        console.trace("Recalculating metatags...")
 | 
			
		||||
        console.debug("Recalculating metatags...")
 | 
			
		||||
        const metatagsToApply: SimpleMetaTagger[] = []
 | 
			
		||||
        for (const metatag of SimpleMetaTaggers.metatags) {
 | 
			
		||||
            if (metatag.includesDates) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -45,6 +45,7 @@ export class AvailableRasterLayers {
 | 
			
		|||
        },
 | 
			
		||||
        geometry: BBox.global.asGeometry(),
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static layersAvailableAt(
 | 
			
		||||
        location: Store<{ lon: number; lat: number }>
 | 
			
		||||
    ): Store<RasterLayerPolygon[]> {
 | 
			
		||||
| 
						 | 
				
			
			@ -77,44 +78,35 @@ export class AvailableRasterLayers {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
export class RasterLayerUtils {
 | 
			
		||||
    /**
 | 
			
		||||
     * Selects, from the given list of available rasterLayerPolygons, a rasterLayer.
 | 
			
		||||
     * This rasterlayer will be of type 'preferredCategory' and will be of the 'best'-layer (if available).
 | 
			
		||||
     * Returns 'undefined' if no such layer is available
 | 
			
		||||
     * @param available
 | 
			
		||||
     * @param preferredCategory
 | 
			
		||||
     * @param ignoreLayer
 | 
			
		||||
     */
 | 
			
		||||
    public static SelectBestLayerAccordingTo(
 | 
			
		||||
        available: RasterLayerPolygon[],
 | 
			
		||||
        preferredCategory: string | string[]
 | 
			
		||||
        preferredCategory: string,
 | 
			
		||||
        ignoreLayer?: RasterLayerPolygon
 | 
			
		||||
    ): RasterLayerPolygon {
 | 
			
		||||
        available = [...available]
 | 
			
		||||
 | 
			
		||||
        if (preferredCategory === undefined) {
 | 
			
		||||
            return available[0]
 | 
			
		||||
        let secondBest: RasterLayerPolygon = undefined
 | 
			
		||||
        for (const rasterLayer of available) {
 | 
			
		||||
            if (rasterLayer === ignoreLayer) {
 | 
			
		||||
                continue
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        let prefered: string[]
 | 
			
		||||
        if (typeof preferredCategory === "string") {
 | 
			
		||||
            prefered = [preferredCategory]
 | 
			
		||||
        } else {
 | 
			
		||||
            prefered = preferredCategory
 | 
			
		||||
            const p = rasterLayer.properties
 | 
			
		||||
            if (p.category === preferredCategory) {
 | 
			
		||||
                if (p.best) {
 | 
			
		||||
                    return rasterLayer
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
        for (let i = prefered.length - 1; i >= 0; i--) {
 | 
			
		||||
            const category = prefered[i]
 | 
			
		||||
            //Then sort all layers of the preferred type to the top. Stability of the sorting will force a 'best' photo layer on top
 | 
			
		||||
            available.sort((ap, bp) => {
 | 
			
		||||
                const a = ap.properties
 | 
			
		||||
                const b = bp.properties
 | 
			
		||||
                if (a.category === category && b.category === category) {
 | 
			
		||||
                    return 0
 | 
			
		||||
                if (!secondBest) {
 | 
			
		||||
                    secondBest = rasterLayer
 | 
			
		||||
                }
 | 
			
		||||
                if (a.category !== category) {
 | 
			
		||||
                    return 1
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
                return -1
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
        const best = available.find((l) => l.properties.best)
 | 
			
		||||
        if (best) {
 | 
			
		||||
            return best
 | 
			
		||||
        }
 | 
			
		||||
        return available[0]
 | 
			
		||||
        return secondBest
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,7 +21,7 @@ import { QueryParameters } from "../Logic/Web/QueryParameters"
 | 
			
		|||
import UserRelatedState from "../Logic/State/UserRelatedState"
 | 
			
		||||
import LayerConfig from "./ThemeConfig/LayerConfig"
 | 
			
		||||
import GeoLocationHandler from "../Logic/Actors/GeoLocationHandler"
 | 
			
		||||
import { AvailableRasterLayers, RasterLayerPolygon } from "./RasterLayers"
 | 
			
		||||
import { AvailableRasterLayers, RasterLayerPolygon, RasterLayerUtils } from "./RasterLayers"
 | 
			
		||||
import LayoutSource from "../Logic/FeatureSource/Sources/LayoutSource"
 | 
			
		||||
import StaticFeatureSource from "../Logic/FeatureSource/Sources/StaticFeatureSource"
 | 
			
		||||
import FeaturePropertiesStore from "../Logic/FeatureSource/Actors/FeaturePropertiesStore"
 | 
			
		||||
| 
						 | 
				
			
			@ -46,6 +46,7 @@ import OsmObjectDownloader from "../Logic/Osm/OsmObjectDownloader"
 | 
			
		|||
import ShowOverlayRasterLayer from "../UI/Map/ShowOverlayRasterLayer"
 | 
			
		||||
import { Utils } from "../Utils"
 | 
			
		||||
import { EliCategory } from "./RasterLayerProperties"
 | 
			
		||||
import BackgroundLayerResetter from "../Logic/Actors/BackgroundLayerResetter"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
| 
						 | 
				
			
			@ -300,34 +301,16 @@ export default class ThemeViewState implements SpecialVisualizationState {
 | 
			
		|||
                this.mapProperties.rasterLayer.setData(AvailableRasterLayers.osmCarto)
 | 
			
		||||
            }
 | 
			
		||||
        )
 | 
			
		||||
        const self = this
 | 
			
		||||
 | 
			
		||||
        function setLayerCategory(category: EliCategory) {
 | 
			
		||||
            const available = self.availableLayers.data
 | 
			
		||||
            const matchingCategoryLayers = available.filter(
 | 
			
		||||
                (l) => l.properties.category === category
 | 
			
		||||
        const setLayerCategory = (category: EliCategory) => {
 | 
			
		||||
            const available = this.availableLayers.data
 | 
			
		||||
            const current = this.mapProperties.rasterLayer
 | 
			
		||||
            const best = RasterLayerUtils.SelectBestLayerAccordingTo(
 | 
			
		||||
                available,
 | 
			
		||||
                category,
 | 
			
		||||
                current.data
 | 
			
		||||
            )
 | 
			
		||||
            const best =
 | 
			
		||||
                matchingCategoryLayers.find((l) => l.properties.best) ?? matchingCategoryLayers[0]
 | 
			
		||||
            const rasterLayer = self.mapProperties.rasterLayer
 | 
			
		||||
            if (rasterLayer.data !== best) {
 | 
			
		||||
                rasterLayer.setData(best)
 | 
			
		||||
                return
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // The current layer is already selected...
 | 
			
		||||
            // We switch the layers again
 | 
			
		||||
 | 
			
		||||
            if (category === "osmbasedmap") {
 | 
			
		||||
                rasterLayer.setData(undefined)
 | 
			
		||||
            } else {
 | 
			
		||||
                // search the _second_ best layer
 | 
			
		||||
                const secondbest = matchingCategoryLayers.find(
 | 
			
		||||
                    (l) => l.properties.best && l !== best
 | 
			
		||||
                )
 | 
			
		||||
                const secondNotBest = matchingCategoryLayers.find((l) => l !== best)
 | 
			
		||||
                rasterLayer.setData(secondbest ?? secondNotBest)
 | 
			
		||||
            }
 | 
			
		||||
            console.log("Best layer for category", category, "is", best.properties.id)
 | 
			
		||||
            current.setData(best)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Hotkeys.RegisterHotkey(
 | 
			
		||||
| 
						 | 
				
			
			@ -454,5 +437,6 @@ export default class ThemeViewState implements SpecialVisualizationState {
 | 
			
		|||
        new ChangeToElementsActor(this.changes, this.featureProperties)
 | 
			
		||||
        new PendingChangesUploader(this.changes, this.selectedElement)
 | 
			
		||||
        new SelectedElementTagsUpdater(this)
 | 
			
		||||
        new BackgroundLayerResetter(this.mapProperties.rasterLayer, this.availableLayers)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -343,7 +343,7 @@ export class MapLibreAdaptor implements MapProperties, ExportableMap {
 | 
			
		|||
            // already the correct background layer, nothing to do
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
        await this.awaitStyleIsLoaded()
 | 
			
		||||
        // await this.awaitStyleIsLoaded()
 | 
			
		||||
 | 
			
		||||
        if (background !== this.rasterLayer?.data?.properties) {
 | 
			
		||||
            // User selected another background in the meantime... abort
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue