Split defaultDatalayer in defaultView/EditDatalayer

In view mode, we don't want the fallback to create a new datalayer.
This commit is contained in:
Yohan Boniface 2023-10-27 11:42:00 +02:00
parent 9eea9f942d
commit 29ab98ede3
3 changed files with 29 additions and 5 deletions

View file

@ -1382,7 +1382,7 @@ L.U.Search = L.PhotonSearch.extend({
}) })
L.DomEvent.on(edit, 'mousedown', (e) => { L.DomEvent.on(edit, 'mousedown', (e) => {
L.DomEvent.stop(e) L.DomEvent.stop(e)
const datalayer = self.map.defaultDataLayer() const datalayer = self.map.defaultEditDataLayer()
const layer = datalayer.geojsonToFeatures(feature) const layer = datalayer.geojsonToFeatures(feature)
layer.isDirty = true layer.isDirty = true
layer.edit() layer.edit()
@ -1636,7 +1636,7 @@ L.U.Editable = L.Editable.extend({
connectCreatedToMap: function (layer) { connectCreatedToMap: function (layer) {
// Overrided from Leaflet.Editable // Overrided from Leaflet.Editable
const datalayer = this.map.defaultDataLayer() const datalayer = this.map.defaultEditDataLayer()
datalayer.addLayer(layer) datalayer.addLayer(layer)
layer.isDirty = true layer.isDirty = true
return layer return layer

View file

@ -694,8 +694,8 @@ L.U.Map.include({
this._setDefaultCenter() this._setDefaultCenter()
return return
} }
const datalayer = this.defaultDataLayer(), const datalayer = this.defaultViewDataLayer(),
feature = datalayer.getFeatureByIndex(-1) feature = datalayer ? datalayer.getFeatureByIndex(-1) : null
if (feature) feature.zoomTo() if (feature) feature.zoomTo()
else this._setDefaultCenter() else this._setDefaultCenter()
}) })
@ -1225,10 +1225,19 @@ L.U.Map.include({
} }
}, },
defaultViewDataLayer: function () {
let datalayer, fallback
datalayer = this.findDataLayer((datalayer) => {
fallback = datalayer
if (datalayer.isVisible()) return true
})
return datalayer || fallback
},
// TODO: allow to control the default datalayer // TODO: allow to control the default datalayer
// (edit and viewing) // (edit and viewing)
// cf https://github.com/umap-project/umap/issues/585 // cf https://github.com/umap-project/umap/issues/585
defaultDataLayer: function () { defaultEditDataLayer: function () {
let datalayer, fallback let datalayer, fallback
datalayer = this.lastUsedDataLayer datalayer = this.lastUsedDataLayer
if ( if (

View file

@ -1,4 +1,5 @@
import json import json
import re
from pathlib import Path from pathlib import Path
import pytest import pytest
@ -11,6 +12,20 @@ from ..base import DataLayerFactory
pytestmark = pytest.mark.django_db 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( def test_remote_layer_should_not_be_used_as_datalayer_for_created_features(
map, live_server, datalayer, page map, live_server, datalayer, page
): ):