feat: give precedence to feature in query string over onLoadPanel

cf #1893
This commit is contained in:
Yohan Boniface 2024-06-21 21:39:17 +02:00
parent 2b2a63fe29
commit 82c9ec7d39
2 changed files with 68 additions and 35 deletions

View file

@ -207,41 +207,7 @@ U.Map = L.Map.extend({
} }
this.initShortcuts() this.initShortcuts()
this.onceDataLoaded(function () { this.onceDataLoaded(this.setViewFromQueryString)
const slug = L.Util.queryString('feature')
if (slug && this.features_index[slug]) this.features_index[slug].view()
if (this.options.noControl) return
this.initCaptionBar()
if (L.Util.queryString('share')) {
this.share.open()
} else if (this.options.onLoadPanel === 'databrowser') {
this.panel.setDefaultMode('expanded')
this.openBrowser('data')
} else if (this.options.onLoadPanel === 'datalayers') {
this.panel.setDefaultMode('condensed')
this.openBrowser('layers')
} else if (this.options.onLoadPanel === 'datafilters') {
this.panel.setDefaultMode('expanded')
this.openBrowser('filters')
} else if (this.options.onLoadPanel === 'caption') {
this.panel.setDefaultMode('condensed')
this.openCaption()
}
if (L.Util.queryString('edit')) {
if (this.hasEditMode()) this.enableEdit()
// Sometimes users share the ?edit link by mistake, let's remove
// this search parameter from URL to prevent this
const url = new URL(window.location)
url.searchParams.delete('edit')
history.pushState({}, '', url)
}
if (L.Util.queryString('download')) {
const download_url = this.urls.get('map_download', {
map_id: this.options.umap_id,
})
window.location = download_url
}
})
window.onbeforeunload = () => (this.editEnabled && this.isDirty) || null window.onbeforeunload = () => (this.editEnabled && this.isDirty) || null
this.backup() this.backup()
@ -335,6 +301,44 @@ U.Map = L.Map.extend({
} }
}, },
setViewFromQueryString: function () {
if (this.options.noControl) return
this.initCaptionBar()
if (L.Util.queryString('share')) {
this.share.open()
} else if (this.options.onLoadPanel === 'databrowser') {
this.panel.setDefaultMode('expanded')
this.openBrowser('data')
} else if (this.options.onLoadPanel === 'datalayers') {
this.panel.setDefaultMode('condensed')
this.openBrowser('layers')
} else if (this.options.onLoadPanel === 'datafilters') {
this.panel.setDefaultMode('expanded')
this.openBrowser('filters')
} else if (this.options.onLoadPanel === 'caption') {
this.panel.setDefaultMode('condensed')
this.openCaption()
}
// Comes after default panels, so if it opens in a panel it will
// take precedence.
const slug = L.Util.queryString('feature')
if (slug && this.features_index[slug]) this.features_index[slug].view()
if (L.Util.queryString('edit')) {
if (this.hasEditMode()) this.enableEdit()
// Sometimes users share the ?edit link by mistake, let's remove
// this search parameter from URL to prevent this
const url = new URL(window.location)
url.searchParams.delete('edit')
history.pushState({}, '', url)
}
if (L.Util.queryString('download')) {
const download_url = this.urls.get('map_download', {
map_id: this.options.umap_id,
})
window.location = download_url
}
},
// Merge the given schema with the default one // Merge the given schema with the default one
// Missing keys inside the schema are merged with the default ones. // Missing keys inside the schema are merged with the default ones.
overrideSchema: function (schema) { overrideSchema: function (schema) {

View file

@ -204,3 +204,32 @@ def test_zoom_control_on_load(map, live_server, page):
map.save() map.save()
page.goto(f"{live_server.url}{map.get_absolute_url()}") page.goto(f"{live_server.url}{map.get_absolute_url()}")
expect(page.locator(".leaflet-control-zoom")).to_be_hidden() expect(page.locator(".leaflet-control-zoom")).to_be_hidden()
def test_feature_in_query_string_has_precedence_over_onloadpanel(
map, live_server, page
):
map.settings["properties"]["onLoadPanel"] = "caption"
map.name = "This is my map"
map.save()
data = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"name": "FooBar"},
"geometry": {
"type": "Point",
"coordinates": [2.12, 49.57],
},
}
],
"_umap_options": {"popupShape": "Panel"},
}
DataLayerFactory(map=map, data=data)
page.goto(f"{live_server.url}{map.get_absolute_url()}?feature=FooBar")
expect(page.get_by_role("heading", name="FooBar")).to_be_visible()
expect(page.get_by_role("heading", name="This is my map")).to_be_hidden()
page.goto(f"{live_server.url}{map.get_absolute_url()}")
expect(page.get_by_role("heading", name="FooBar")).to_be_hidden()
expect(page.get_by_role("heading", name="This is my map")).to_be_visible()