Make sure we update the tilelayers switcher when setting a custom one

This commit is contained in:
Yohan Boniface 2023-12-16 09:17:02 +01:00
parent 5d2b968863
commit cbb02f9890
2 changed files with 22 additions and 12 deletions

View file

@ -1187,20 +1187,27 @@ L.U.Map.include({
/* Used in view mode to define the current tilelayer */
L.U.TileLayerControl = L.Control.IconLayers.extend({
initialize: function (map, options) {
const layers = []
map.eachTileLayer((layer) => {
this.map = map
L.Control.IconLayers.prototype.initialize.call(this, {
position: 'topleft',
manageLayers: false,
})
this.on('activelayerchange', (e) => map.selectTileLayer(e.layer))
},
setLayers: function (layers) {
if (!layers) {
layers = []
this.map.eachTileLayer((layer) => {
layers.push({
title: layer.options.name,
layer: layer,
icon: L.Util.template(layer.options.url_template, map.demoTileInfos),
icon: L.Util.template(layer.options.url_template, this.map.demoTileInfos),
})
})
}
const maxShown = 10
L.Control.IconLayers.prototype.initialize.call(this, layers.slice(0, maxShown), {
position: 'topleft',
manageLayers: false
})
this.on('activelayerchange', (e) => map.selectTileLayer(e.layer))
L.Control.IconLayers.prototype.setLayers.call(this, layers.slice(0, maxShown))
},
})
@ -1264,6 +1271,7 @@ L.U.TileLayerChooser = L.Control.extend({
'click',
function () {
this.map.selectTileLayer(tilelayer)
this.map._controls.tilelayers.setLayers()
if (options && options.callback) options.callback(tilelayer)
},
this

View file

@ -166,7 +166,7 @@ L.U.Map.include({
this.help = new L.U.Help(this)
if (this.options.hash) this.addHash()
this.initTileLayers(this.options.tilelayers)
this.initTileLayers()
// Needs tilelayer to exist for minimap
this.initControls()
// Needs locate control and hash to exist
@ -348,6 +348,7 @@ L.U.Map.include({
this.importer = new L.U.Importer(this)
this.drop = new L.U.DropControl(this)
this._controls.tilelayers = new L.U.TileLayerControl(this)
this._controls.tilelayers.setLayers()
this.renderControls()
},
@ -606,6 +607,7 @@ L.U.Map.include({
} else {
this.selectTileLayer(this.tilelayers[0])
}
if (this._controls) this._controls.tilelayers.setLayers()
},
createTileLayer: function (tilelayer) {