Skip to content

Commit

Permalink
Add parse and format properties to po.hash().
Browse files Browse the repository at this point in the history
These can be used to override the default location hash formatting e.g.
to add other parameters.  As `parse` is called every time the location
hash changes, it can be used to detect other parameters at the same
time.
  • Loading branch information
jasondavies committed Dec 7, 2010
1 parent 1e3ea23 commit 012d0e4
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 69 deletions.
28 changes: 24 additions & 4 deletions polymaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -1763,19 +1763,27 @@ po.hash = function() {
lat = 90 - 1e-8, // allowable latitude range
map;

function move() {
var parse = function(s) {
return s.split("/").map(Number);
};

var format = function(map) {
var center = map.center(),
zoom = map.zoom(),
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)),
s1 = "#" + zoom.toFixed(2)
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
return "#" + zoom.toFixed(2)
+ "/" + center.lat.toFixed(precision)
+ "/" + center.lon.toFixed(precision);
};

function move() {
var s1 = format(map);
if (s0 !== s1) location.replace(s0 = s1); // don't recenter the map!
}

function hashchange() {
if (location.hash === s0) return; // ignore spurious hashchange events
var args = (s0 = location.hash).substring(1).split("/").map(Number);
var args = parse((s0 = location.hash).substring(1));
if (args.length < 3 || args.some(isNaN)) move(); // replace bogus hash
else {
var size = map.size();
Expand All @@ -1799,6 +1807,18 @@ po.hash = function() {
return hash;
};

hash.parse = function(x) {
if (!arguments.length) return parse;
parse = x;
return hash;
}

hash.format = function(x) {
if (!arguments.length) return format;
format = x;
return hash;
}

return hash;
};
// Default map controls.
Expand Down
Loading

0 comments on commit 012d0e4

Please sign in to comment.