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

View file

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