From f09e399b3c7151601ca85a93bdb95b7da532c572 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Wed, 7 Feb 2024 17:53:45 +0100 Subject: [PATCH] chore: refactor setting Map options from querystring --- umap/static/umap/js/umap.js | 180 +++++++++++---------- umap/tests/integration/test_map_preview.py | 5 +- umap/tests/integration/test_querystring.py | 13 ++ 3 files changed, 105 insertions(+), 93 deletions(-) diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 7935eaeb..c7161d8e 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -69,6 +69,65 @@ L.U.Map.include({ 'tilelayers', ], + editableOptions: { + 'zoom': undefined, + 'scrollWheelZoom': Boolean, + 'scaleControl': Boolean, + 'moreControl': Boolean, + 'miniMap': Boolean, + 'displayPopupFooter': undefined, + 'onLoadPanel': String, + 'defaultView': String, + 'name': String, + 'description': String, + 'licence': undefined, + 'tilelayer': undefined, + 'overlay': undefined, + 'limitBounds': undefined, + 'color': String, + 'iconClass': String, + 'iconUrl': String, + 'smoothFactor': undefined, + 'iconOpacity': undefined, + 'opacity': undefined, + 'weight': undefined, + 'fill': undefined, + 'fillColor': undefined, + 'fillOpacity': undefined, + 'dashArray': undefined, + 'popupShape': String, + 'popupTemplate': String, + 'popupContentTemplate': String, + 'zoomTo': undefined, + 'captionBar': Boolean, + 'captionMenus': Boolean, + 'slideshow': undefined, + 'sortKey': undefined, + 'labelKey': undefined, + 'filterKey': undefined, + 'facetKey': undefined, + 'slugKey': undefined, + 'showLabel': undefined, + 'labelDirection': undefined, + 'labelInteractive': undefined, + 'outlinkTarget': undefined, + 'shortCredit': undefined, + 'longCredit': undefined, + 'permanentCredit': undefined, + 'permanentCreditBackground': undefined, + 'zoomControl': 'NullableBoolean', + 'datalayersControl': 'NullableBoolean', + 'searchControl': 'NullableBoolean', + 'locateControl': 'NullableBoolean', + 'fullscreenControl': 'NullableBoolean', + 'editinosmControl': 'NullableBoolean', + 'embedControl': 'NullableBoolean', + 'measureControl': 'NullableBoolean', + 'tilelayersControl': 'NullableBoolean', + 'starControl': 'NullableBoolean', + 'easing': undefined, + }, + initialize: function (el, geojson) { // Locale name (pt_PT, en_US…) // To be used for Django localization @@ -89,7 +148,7 @@ L.U.Map.include({ ? geojson.properties.fullscreenControl : true geojson.properties.fullscreenControl = false - L.Util.setBooleanFromQueryString(geojson.properties, 'scrollWheelZoom') + this.setOptionsFromQueryString(geojson.properties) L.Map.prototype.initialize.call(this, el, geojson.properties) @@ -109,32 +168,10 @@ L.U.Map.include({ this.demoTileInfos = this.options.demoTileInfos this.options.zoomControl = zoomControl this.options.fullscreenControl = fullscreenControl - L.Util.setBooleanFromQueryString(this.options, 'moreControl') - L.Util.setBooleanFromQueryString(this.options, 'scaleControl') - L.Util.setBooleanFromQueryString(this.options, 'miniMap') - L.Util.setFromQueryString(this.options, 'editMode') - L.Util.setBooleanFromQueryString(this.options, 'displayDataBrowserOnLoad') - L.Util.setBooleanFromQueryString(this.options, 'displayCaptionOnLoad') - L.Util.setBooleanFromQueryString(this.options, 'captionBar') - L.Util.setBooleanFromQueryString(this.options, 'captionMenus') - for (let i = 0; i < this.HIDDABLE_CONTROLS.length; i++) { - L.Util.setNullableBooleanFromQueryString( - this.options, - `${this.HIDDABLE_CONTROLS[i]}Control` - ) - } - // Specific case for datalayersControl - // which accept "expanded" value, on top of true/false/null - if (L.Util.queryString('datalayersControl') === 'expanded') { - L.Util.setFromQueryString(this.options, 'datalayersControl') - } this.datalayersOnLoad = L.Util.queryString('datalayers') - this.options.onLoadPanel = L.Util.queryString( - 'onLoadPanel', - this.options.onLoadPanel - ) - if (this.datalayersOnLoad) + if (this.datalayersOnLoad) { this.datalayersOnLoad = this.datalayersOnLoad.toString().split(',') + } if (L.Browser.ielt9) this.options.editMode = 'disabled' // TODO include ie9 @@ -313,6 +350,31 @@ L.U.Map.include({ this.on('click contextmenu.show', this.closeInplaceToolbar) }, + setOptionsFromQueryString: function (options) { + // This is not an editable option + L.Util.setFromQueryString(options, 'editMode') + // FIXME retrocompat + L.Util.setBooleanFromQueryString(options, 'displayDataBrowserOnLoad') + L.Util.setBooleanFromQueryString(options, 'displayCaptionOnLoad') + for (const [key, type] of Object.entries(this.editableOptions)) { + switch (type) { + case Boolean: + L.Util.setBooleanFromQueryString(options, key) + break + case 'NullableBoolean': + L.Util.setNullableBooleanFromQueryString(options, key) + break + case String: + L.Util.setFromQueryString(options, key) + } + } + // Specific case for datalayersControl + // which accepts "expanded" value, on top of true/false/null + if (L.Util.queryString('datalayersControl') === 'expanded') { + L.Util.setFromQueryString(options, 'datalayersControl') + } + }, + initControls: function () { this.helpMenuActions = {} this._controls = {} @@ -869,8 +931,7 @@ L.U.Map.include({ let mustReindex = false - for (let i = 0; i < this.editableOptions.length; i++) { - const option = this.editableOptions[i] + for (const option of Object.keys(this.editableOptions)) { if (typeof importedData.properties[option] !== 'undefined') { this.options[option] = importedData.properties[option] if (option === 'sortKey') mustReindex = true @@ -997,70 +1058,11 @@ L.U.Map.include({ else this.fire('saved') }, - editableOptions: [ - 'zoom', - 'scrollWheelZoom', - 'scaleControl', - 'moreControl', - 'miniMap', - 'displayPopupFooter', - 'onLoadPanel', - 'defaultView', - 'name', - 'description', - 'licence', - 'tilelayer', - 'overlay', - 'limitBounds', - 'color', - 'iconClass', - 'iconUrl', - 'smoothFactor', - 'iconOpacity', - 'opacity', - 'weight', - 'fill', - 'fillColor', - 'fillOpacity', - 'dashArray', - 'popupShape', - 'popupTemplate', - 'popupContentTemplate', - 'zoomTo', - 'captionBar', - 'captionMenus', - 'slideshow', - 'sortKey', - 'labelKey', - 'filterKey', - 'facetKey', - 'slugKey', - 'showLabel', - 'labelDirection', - 'labelInteractive', - 'outlinkTarget', - 'shortCredit', - 'longCredit', - 'permanentCredit', - 'permanentCreditBackground', - 'zoomControl', - 'datalayersControl', - 'searchControl', - 'locateControl', - 'fullscreenControl', - 'editinosmControl', - 'embedControl', - 'measureControl', - 'tilelayersControl', - 'starControl', - 'easing', - ], - exportOptions: function () { const properties = {} - for (let i = this.editableOptions.length - 1; i >= 0; i--) { - if (typeof this.options[this.editableOptions[i]] !== 'undefined') { - properties[this.editableOptions[i]] = this.options[this.editableOptions[i]] + for (const option of Object.keys(this.editableOptions)) { + if (typeof this.options[option] !== 'undefined') { + properties[option] = this.options[option] } } return properties diff --git a/umap/tests/integration/test_map_preview.py b/umap/tests/integration/test_map_preview.py index 9b4ba0d3..9176eba8 100644 --- a/umap/tests/integration/test_map_preview.py +++ b/umap/tests/integration/test_map_preview.py @@ -69,10 +69,7 @@ def test_map_preview_can_load_csv_in_querystring(page, live_server, tilelayer): def test_map_preview_can_change_styling_from_querystring(page, live_server, tilelayer): - style = {"color": "DarkRed"} - page.goto( - f"{live_server.url}/map/?data={quote(json.dumps(GEOJSON))}&style={quote(json.dumps(style))}" - ) + page.goto(f"{live_server.url}/map/?data={quote(json.dumps(GEOJSON))}&color=DarkRed") markers = page.locator(".leaflet-marker-icon .icon_container") expect(markers).to_have_count(1) expect(markers).to_have_css("background-color", "rgb(139, 0, 0)") diff --git a/umap/tests/integration/test_querystring.py b/umap/tests/integration/test_querystring.py index faab701e..5118fe6c 100644 --- a/umap/tests/integration/test_querystring.py +++ b/umap/tests/integration/test_querystring.py @@ -1,3 +1,5 @@ +import re + import pytest from playwright.sync_api import expect @@ -37,3 +39,14 @@ def test_datalayers_control(map, live_server, datalayer, page): page.goto(f"{live_server.url}{map.get_absolute_url()}?datalayersControl=expanded") expect(control).to_be_hidden() expect(box).to_be_visible() + + +def test_can_deactivate_wheel_from_query_string(map, live_server, page): + page.goto(f"{live_server.url}{map.get_absolute_url()}") + expect(page).to_have_url(re.compile(r".*#7/.+")) + page.mouse.wheel(0, 1) + expect(page).to_have_url(re.compile(r".*#6/.+")) + page.goto(f"{live_server.url}{map.get_absolute_url()}?scrollWheelZoom=false") + expect(page).to_have_url(re.compile(r".*#7/.+")) + page.mouse.wheel(0, 1) + expect(page).to_have_url(re.compile(r".*#7/.+"))