From b7c90165c2642dfbdb09a7b836ee4380d8df9bba Mon Sep 17 00:00:00 2001 From: Brice Schaffner Date: Fri, 12 Jul 2024 11:04:35 +0200 Subject: [PATCH 1/2] PB-811: Fix go to position if geolocation is active at startup If the geolocation was activated at startup using the goelocation query parameter the application would not re-center to the position. This would break some legacy links that use this parameter like the swisstopo "you are here" T-shirt. This was also an issue when entering the parameter in the new query format. We had several issue here, first the legacy parameter was totally ignored because URLSearchParams parsed the geolocation to an empty string which was not supported by our boolean query param. Another issue was that per default tracking was off unless you explicitely click on the geolocation button. --- src/router/legacyPermalinkManagement.routerPlugin.js | 9 +++++++-- src/router/storeSync/abstractParamConfig.class.js | 3 ++- src/store/modules/geolocation.store.js | 2 +- src/store/plugins/geolocation-management.plugin.js | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/router/legacyPermalinkManagement.routerPlugin.js b/src/router/legacyPermalinkManagement.routerPlugin.js index c91936e4a..0892fcd8d 100644 --- a/src/router/legacyPermalinkManagement.routerPlugin.js +++ b/src/router/legacyPermalinkManagement.routerPlugin.js @@ -147,18 +147,23 @@ const handleLegacyParam = ( break // if no special work to do, we just copy past legacy params to the new viewer default: + // NOTE: legacyValue is parsed using URLSearchParams which don't make any difference + // between &foo and &foo= newValue = legacyValue break } - if (newValue) { + if (newValue !== undefined) { // When receiving a query, the application will encode the URI components // We decode those so that the new query won't encode encoded character // for example, we avoid having " " becoming %2520 in the URI newQuery[key] = decodeURIComponent(newValue) log.info( - `[Legacy URL] ${param}=${legacyValue} parameter changed to ${key}=${decodeURIComponent(newValue)}` + `[Legacy URL] ${param}=${legacyValue} parameter changed to ${key}=${decodeURIComponent(newValue)}`, + newQuery ) + } else { + log.error(`[Legacy URL] ${param}=${legacyValue} parameter not processed`) } } diff --git a/src/router/storeSync/abstractParamConfig.class.js b/src/router/storeSync/abstractParamConfig.class.js index 5dc7e4f0d..bd2d6ad38 100644 --- a/src/router/storeSync/abstractParamConfig.class.js +++ b/src/router/storeSync/abstractParamConfig.class.js @@ -79,7 +79,8 @@ export default class AbstractParamConfig { // is present in the query (without a boolean value attached) return ( queryValue === null || - (typeof queryValue === 'string' && queryValue === 'true') || + queryValue === 'true' || + queryValue === '' || (typeof queryValue === 'boolean' && !!queryValue) ) } else if (queryValue === null) { diff --git a/src/store/modules/geolocation.store.js b/src/store/modules/geolocation.store.js index 44bde46c7..fa20e6197 100644 --- a/src/store/modules/geolocation.store.js +++ b/src/store/modules/geolocation.store.js @@ -19,7 +19,7 @@ const state = { * * @type Boolean */ - tracking: false, + tracking: true, /** * Device position in the current application projection [x, y] * diff --git a/src/store/plugins/geolocation-management.plugin.js b/src/store/plugins/geolocation-management.plugin.js index bfb3fcf7c..aad03cecb 100644 --- a/src/store/plugins/geolocation-management.plugin.js +++ b/src/store/plugins/geolocation-management.plugin.js @@ -120,7 +120,7 @@ const handlePositionError = (error, store, state, options = {}) => { } const activeGeolocation = (store, state, options = {}) => { - const { useInitial = false } = options + const { useInitial = true } = options if ( useInitial && store.state.geolocation.position[0] !== 0 && From da8973f62c08151e8c14308ec62909a7b8195428 Mon Sep 17 00:00:00 2001 From: Brice Schaffner Date: Fri, 12 Jul 2024 11:15:18 +0200 Subject: [PATCH 2/2] PB-811: Only change the zoom in geolocation when tracking When not tracking, e.g. you have a permalink with geolocation with a fix center and zoom, you don't want to change the zoom. --- .../abstractParamConfig.class.spec.js | 4 +-- .../plugins/geolocation-management.plugin.js | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/router/storeSync/__tests__/abstractParamConfig.class.spec.js b/src/router/storeSync/__tests__/abstractParamConfig.class.spec.js index be062506c..2537bbd46 100644 --- a/src/router/storeSync/__tests__/abstractParamConfig.class.spec.js +++ b/src/router/storeSync/__tests__/abstractParamConfig.class.spec.js @@ -80,7 +80,7 @@ describe('Test all AbstractParamConfig class functionalities', () => { }) expect(testInstance.readValueFromQuery({ test: 'true' })).to.be.true expect(testInstance.readValueFromQuery({ test: 'false' })).to.be.false - expect(testInstance.readValueFromQuery({ test: '' })).to.be.false + expect(testInstance.readValueFromQuery({ test: '' })).to.be.true // null value means the param without value, we want it to be true expect(testInstance.readValueFromQuery({ test: null })).to.be.true expect(testInstance.readValueFromQuery({})).to.be.undefined @@ -135,7 +135,7 @@ describe('Test all AbstractParamConfig class functionalities', () => { }) expect(testInstance.readValueFromQuery({ test: 'true' })).to.be.true expect(testInstance.readValueFromQuery({ test: 'false' })).to.be.false - expect(testInstance.readValueFromQuery({ test: '' })).to.be.false + expect(testInstance.readValueFromQuery({ test: '' })).to.be.true // null value means the param without value, we want it to be true expect(testInstance.readValueFromQuery({ test: null })).to.be.true expect(testInstance.readValueFromQuery({})).to.be.true diff --git a/src/store/plugins/geolocation-management.plugin.js b/src/store/plugins/geolocation-management.plugin.js index aad03cecb..ae99b96ba 100644 --- a/src/store/plugins/geolocation-management.plugin.js +++ b/src/store/plugins/geolocation-management.plugin.js @@ -55,9 +55,22 @@ const handlePositionAndDispatchToStore = (position, store) => { accuracy: position.coords.accuracy, ...dispatcher, }) - // if tracking is active, we center the view of the map on the position received + // if tracking is active, we center the view of the map on the position received and change + // to the proper zoom if (store.state.geolocation.tracking) { setCenterIfInBounds(store, positionProjected) + // set zoom level if needed + let zoomLevel = STANDARD_ZOOM_LEVEL_1_25000_MAP + if (store.state.position.projection instanceof CustomCoordinateSystem) { + zoomLevel = + store.state.position.projection.transformStandardZoomLevelToCustom(zoomLevel) + } + if (store.state.position.zoom != zoomLevel) { + store.dispatch('setZoom', { + zoom: zoomLevel, + ...dispatcher, + }) + } } } @@ -152,16 +165,6 @@ const activeGeolocation = (store, state, options = {}) => { // handle current position handlePositionAndDispatchToStore(position, store) - - // set zoom level - let zoomLevel = STANDARD_ZOOM_LEVEL_1_25000_MAP - if (state.position.projection instanceof CustomCoordinateSystem) { - zoomLevel = state.position.projection.transformStandardZoomLevelToCustom(zoomLevel) - } - store.dispatch('setZoom', { - zoom: zoomLevel, - ...dispatcher, - }) }, (error) => handlePositionError(error, store, state, { reactivate: true }), {