From 82c9ec7d395f1a11035d8bc9bb0d6c02f5980e7d Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 21 Jun 2024 21:39:17 +0200 Subject: [PATCH] feat: give precedence to feature in query string over onLoadPanel cf #1893 --- umap/static/umap/js/umap.js | 74 ++++++++++++++++-------------- umap/tests/integration/test_map.py | 29 ++++++++++++ 2 files changed, 68 insertions(+), 35 deletions(-) diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 53054cc83..6d68f9104 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -207,41 +207,7 @@ U.Map = L.Map.extend({ } this.initShortcuts() - this.onceDataLoaded(function () { - 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 - } - }) + this.onceDataLoaded(this.setViewFromQueryString) window.onbeforeunload = () => (this.editEnabled && this.isDirty) || null 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 // Missing keys inside the schema are merged with the default ones. overrideSchema: function (schema) { diff --git a/umap/tests/integration/test_map.py b/umap/tests/integration/test_map.py index 761490bde..6eb979e87 100644 --- a/umap/tests/integration/test_map.py +++ b/umap/tests/integration/test_map.py @@ -204,3 +204,32 @@ def test_zoom_control_on_load(map, live_server, page): map.save() page.goto(f"{live_server.url}{map.get_absolute_url()}") 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()