From 52903c509d6cd63806b68c850c64559e87c6dbcb Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Mon, 30 Jun 2014 00:33:19 -0400 Subject: [PATCH] `iD.behavior.Hash` handles selected id parameter (this is better than having `iD.modes.Select` mess with `location.hash`) --- js/id/behavior/hash.js | 42 +++++++++++++++++++++++++++++++----------- js/id/modes/select.js | 23 ----------------------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/js/id/behavior/hash.js b/js/id/behavior/hash.js index fb6309f6c..44a38b5aa 100644 --- a/js/id/behavior/hash.js +++ b/js/id/behavior/hash.js @@ -14,15 +14,29 @@ iD.behavior.Hash = function(context) { }; var formatter = function(map) { - var center = map.center(), + var mode = context.mode(), + center = map.center(), zoom = map.zoom(), - precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)); - var q = iD.util.stringQs(location.hash.substring(1)); - return '#' + iD.util.qsString(_.assign(q, { - map: zoom.toFixed(2) + - '/' + center[0].toFixed(precision) + - '/' + center[1].toFixed(precision) - }), true); + precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)), + q = iD.util.stringQs(location.hash.substring(1)), + newParams = {}; + + if (mode && mode.id === 'browse') { + delete q.id; + } else { + var selected = context.selectedIDs().filter(function(id) { + return !context.entity(id).isNew(); + }); + if (selected.length) { + newParams.id = selected.join(','); + } + } + + newParams.map = zoom.toFixed(2) + + '/' + center[0].toFixed(precision) + + '/' + center[1].toFixed(precision); + + return '#' + iD.util.qsString(_.assign(q, newParams), true); }; function update() { @@ -30,7 +44,7 @@ iD.behavior.Hash = function(context) { if (s0 !== s1) location.replace(s0 = s1); // don't recenter the map! } - var move = _.throttle(update, 500); + var throttledUpdate = _.throttle(update, 500); function hashchange() { if (location.hash === s0) return; // ignore spurious hashchange events @@ -41,14 +55,17 @@ iD.behavior.Hash = function(context) { function hash() { context.map() - .on('move.hash', move); + .on('move.hash', throttledUpdate); + + context + .on('enter.hash', throttledUpdate); d3.select(window) .on('hashchange.hash', hashchange); if (location.hash) { var q = iD.util.stringQs(location.hash.substring(1)); - if (q.id) context.loadEntity(q.id, !q.map); + if (q.id) context.loadEntity(q.id.split(',')[0], !q.map); hashchange(); if (q.map) hash.hadHash = true; } @@ -58,6 +75,9 @@ iD.behavior.Hash = function(context) { context.map() .on('move.hash', null); + context + .on('enter.hash', null); + d3.select(window) .on('hashchange.hash', null); diff --git a/js/id/modes/select.js b/js/id/modes/select.js index a284ba75f..5acee7a84 100644 --- a/js/id/modes/select.js +++ b/js/id/modes/select.js @@ -93,17 +93,6 @@ iD.modes.Select = function(context, selectedIDs) { }); }); - var notNew = selectedIDs.filter(function(id) { - return !context.entity(id).isNew(); - }); - - if (notNew.length) { - var q = iD.util.stringQs(location.hash.substring(1)); - location.replace('#' + iD.util.qsString(_.assign(q, { - id: notNew.join(',') - }), true)); - } - context.ui().sidebar .select(singular() ? singular().id : null, newFeature); @@ -187,18 +176,6 @@ iD.modes.Select = function(context, selectedIDs) { context.uninstall(behavior); }); - var q = _.omit(iD.util.stringQs(location.hash.substring(1)), 'id'), - center = context.map().center(), - zoom = context.map().zoom(), - precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)), - newhash = '#' + iD.util.qsString(_.assign(q, { - map: zoom.toFixed(2) + - '/' + center[0].toFixed(precision) + - '/' + center[1].toFixed(precision) - }), true); - - location.replace(newhash); - keybinding.off(); context.history()