From 67c65887b6c12e50e2cc424e28a57403f2183ee8 Mon Sep 17 00:00:00 2001 From: Martin Raifer Date: Tue, 19 Jul 2022 12:02:03 +0200 Subject: [PATCH] simplify map location restore, see #9146 --- modules/behavior/hash.js | 36 +++++++++++++++--------------------- modules/ui/init.js | 2 +- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/modules/behavior/hash.js b/modules/behavior/hash.js index eecc0e0af..8448d1ad2 100644 --- a/modules/behavior/hash.js +++ b/modules/behavior/hash.js @@ -28,7 +28,7 @@ export function behaviorHash(context) { var newParams = {}; delete oldParams.id; - var selected = context.selectedIDs().filter(function (id) { + var selected = context.selectedIDs().filter(function(id) { return context.hasEntity(id); }); if (selected.length) { @@ -53,7 +53,7 @@ export function behaviorHash(context) { var changeCount; var titleID; - var selected = context.selectedIDs().filter(function (id) { + var selected = context.selectedIDs().filter(function(id) { return context.hasEntity(id); }); if (selected.length) { @@ -110,16 +110,16 @@ export function behaviorHash(context) { // set the title we want displayed for the browser tab/window updateTitle(true /* includeChangeCount */); - //save last used map hash/location for future - const mapHash = latestHash.split('map='); - if (mapHash.length === 2) { - prefs('map-hash', mapHash[1]); + // save last used map location for future + const q = utilStringQs(latestHash); + if (q.map) { + prefs('map-location', q.map); } } } var _throttledUpdate = _throttle(updateHashIfNeeded, 500); - var _throttledUpdateTitle = _throttle(function () { + var _throttledUpdateTitle = _throttle(function() { updateTitle(true /* includeChangeCount */); }, 500); @@ -146,7 +146,7 @@ export function behaviorHash(context) { context.map().centerZoom([mapArgs[2], Math.min(_latitudeLimit, Math.max(-_latitudeLimit, mapArgs[1]))], mapArgs[0]); if (q.id && mode) { - var ids = q.id.split(',').filter(function (id) { + var ids = q.id.split(',').filter(function(id) { return context.hasEntity(id); }); if (ids.length && @@ -197,21 +197,15 @@ export function behaviorHash(context) { } if (q.map) { - behavior.hadHash = true; - } else if (prefs('map-hash')) { - //user has existing map location hash, set hash & map to location - const mapHash = prefs('map-hash'); - let currentHash = computedHash(); - - currentHash = currentHash.substring(0, currentHash.indexOf('map=')) + 'map=' + mapHash; - - window.history.replaceState(null, computedTitle(false /* includeChangeCount */), currentHash); - const mapArgs = mapHash.split('/').map(Number); + behavior.hadLocation = true; + } else if (!q.id && prefs('map-location')) { + // center map at last visited map location + const mapArgs = prefs('map-location').split('/').map(Number); context.map().centerZoom([mapArgs[2], Math.min(_latitudeLimit, Math.max(-_latitudeLimit, mapArgs[1]))], mapArgs[0]); - _cachedHash = window.location.hash; + updateHashIfNeeded(); - behavior.hadHash = true; + behavior.hadLocation = true; } hashchange(); @@ -220,7 +214,7 @@ export function behaviorHash(context) { } } - behavior.off = function () { + behavior.off = function() { _throttledUpdate.cancel(); _throttledUpdateTitle.cancel(); diff --git a/modules/ui/init.js b/modules/ui/init.js index b857719d7..f44344b26 100644 --- a/modules/ui/init.js +++ b/modules/ui/init.js @@ -354,7 +354,7 @@ export function uiInit(context) { ui.hash = behaviorHash(context); ui.hash(); - if (!ui.hash.hadHash) { + if (!ui.hash.hadLocation) { map.centerZoom([0, 0], 2); }