Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: give precedence to feature in query string over onLoadPanel #1930

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading