diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index c88de1b0..ba459f48 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -1382,7 +1382,7 @@ L.U.Search = L.PhotonSearch.extend({ }) L.DomEvent.on(edit, 'mousedown', (e) => { L.DomEvent.stop(e) - const datalayer = self.map.defaultDataLayer() + const datalayer = self.map.defaultEditDataLayer() const layer = datalayer.geojsonToFeatures(feature) layer.isDirty = true layer.edit() @@ -1636,7 +1636,7 @@ L.U.Editable = L.Editable.extend({ connectCreatedToMap: function (layer) { // Overrided from Leaflet.Editable - const datalayer = this.map.defaultDataLayer() + const datalayer = this.map.defaultEditDataLayer() datalayer.addLayer(layer) layer.isDirty = true return layer diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index bcb244dc..85f17ed4 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -694,10 +694,17 @@ L.U.Map.include({ this._setDefaultCenter() return } - const datalayer = this.defaultDataLayer(), - feature = datalayer.getFeatureByIndex(-1) - if (feature) feature.zoomTo() - else this._setDefaultCenter() + const datalayer = this.firstVisibleDatalayer() + let feature + if (datalayer) { + const feature = datalayer.getFeatureByIndex(-1) + if (feature) { + feature.zoomTo() + return + } + } + // Fallback, no datalayer or no feature found + this._setDefaultCenter() }) } else { this._setDefaultCenter() @@ -1225,10 +1232,16 @@ L.U.Map.include({ } }, + firstVisibleDatalayer: function () { + return this.findDataLayer((datalayer) => { + if (datalayer.isVisible()) return true + }) + }, + // TODO: allow to control the default datalayer // (edit and viewing) // cf https://github.com/umap-project/umap/issues/585 - defaultDataLayer: function () { + defaultEditDataLayer: function () { let datalayer, fallback datalayer = this.lastUsedDataLayer if ( diff --git a/umap/tests/integration/test_map.py b/umap/tests/integration/test_map.py index a40e55d6..ec4500a2 100644 --- a/umap/tests/integration/test_map.py +++ b/umap/tests/integration/test_map.py @@ -1,4 +1,5 @@ import json +import re from pathlib import Path import pytest @@ -11,6 +12,20 @@ from ..base import DataLayerFactory pytestmark = pytest.mark.django_db +def test_default_view_latest_without_datalayer_should_use_default_center( + map, live_server, datalayer, page +): + datalayer.settings["displayOnLoad"] = False + datalayer.save() + map.settings["defaultView"] = "latest" + map.save() + page.goto(f"{live_server.url}{map.get_absolute_url()}") + # Hash is defined, so map is initialized + expect(page).to_have_url(re.compile(".*#7/.*")) + layers = page.locator(".umap-browse-datalayers li") + expect(layers).to_have_count(1) + + def test_remote_layer_should_not_be_used_as_datalayer_for_created_features( map, live_server, datalayer, page ):