From cbb02f9890439bb2cf7ea805551c83c399a601cb Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Sat, 16 Dec 2023 09:17:02 +0100 Subject: [PATCH] Make sure we update the tilelayers switcher when setting a custom one --- umap/static/umap/js/umap.controls.js | 30 ++++++++++++++++++---------- umap/static/umap/js/umap.js | 4 +++- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index bd753209..523d6316 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -1187,21 +1187,28 @@ 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) => { - 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), { + this.map = map + L.Control.IconLayers.prototype.initialize.call(this, { position: 'topleft', - manageLayers: false + 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, 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 */ @@ -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 diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 39addb50..b055dbf2 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -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) {