Skip to content

Commit

Permalink
feat: give precedence to feature in query string over onLoadPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Jun 21, 2024
1 parent 2b2a63f commit 9734af5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 35 deletions.
74 changes: 39 additions & 35 deletions umap/static/umap/js/umap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions umap/tests/integration/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 9734af5

Please sign in to comment.