mapcomplete/Logic/Actors/BackgroundLayerResetter.ts

50 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import { UIEventSource } from "../UIEventSource"
import BaseLayer from "../../Models/BaseLayer"
import AvailableBaseLayers from "./AvailableBaseLayers"
import Loc from "../../Models/Loc"
import { Utils } from "../../Utils"
2021-01-02 21:03:40 +01:00
/**
* Sets the current background layer to a layer that is actually available
*/
export default class BackgroundLayerResetter {
2022-09-08 21:40:48 +02:00
constructor(
currentBackgroundLayer: UIEventSource<BaseLayer>,
location: UIEventSource<Loc>,
availableLayers: UIEventSource<BaseLayer[]>,
defaultLayerId: string = undefined
) {
2021-11-07 16:34:51 +01:00
if (Utils.runningFromConsole) {
return
}
2021-11-07 16:34:51 +01:00
2022-09-08 21:40:48 +02:00
defaultLayerId = defaultLayerId ?? AvailableBaseLayers.osmCarto.id
2021-01-02 21:03:40 +01:00
// Change the baselayer back to OSM if we go out of the current range of the layer
2022-09-08 21:40:48 +02:00
availableLayers.addCallbackAndRun((availableLayers) => {
let defaultLayer = undefined
const currentLayer = currentBackgroundLayer.data.id
2021-01-02 21:03:40 +01:00
for (const availableLayer of availableLayers) {
if (availableLayer.id === currentLayer) {
if (availableLayer.max_zoom < location.data.zoom) {
2022-09-08 21:40:48 +02:00
break
2021-01-02 21:03:40 +01:00
}
if (availableLayer.min_zoom > location.data.zoom) {
2022-09-08 21:40:48 +02:00
break
2021-01-02 21:03:40 +01:00
}
if (availableLayer.id === defaultLayerId) {
2022-09-08 21:40:48 +02:00
defaultLayer = availableLayer
2021-01-02 21:03:40 +01:00
}
2022-09-08 21:40:48 +02:00
return // All good - the current layer still works!
2021-01-02 21:03:40 +01:00
}
}
// Oops, we panned out of range for this layer!
2022-09-08 21:40:48 +02:00
console.log(
"AvailableBaseLayers-actor: detected that the current bounds aren't sufficient anymore - reverting to OSM standard"
)
currentBackgroundLayer.setData(defaultLayer ?? AvailableBaseLayers.osmCarto)
})
2021-01-02 21:03:40 +01:00
}
2022-09-08 21:40:48 +02:00
}