Skip to content

Commit

Permalink
simplify map location restore, see openstreetmap#9146
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrasd committed Jul 19, 2022
1 parent 981628b commit 67c6588
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
36 changes: 15 additions & 21 deletions modules/behavior/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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 &&
Expand Down Expand Up @@ -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();
Expand All @@ -220,7 +214,7 @@ export function behaviorHash(context) {
}
}

behavior.off = function () {
behavior.off = function() {
_throttledUpdate.cancel();
_throttledUpdateTitle.cancel();

Expand Down
2 changes: 1 addition & 1 deletion modules/ui/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 67c6588

Please sign in to comment.