Use querystring-style hash

This commit is contained in:
Tom MacWright
2012-11-05 15:16:00 -05:00
parent 2b1df4a2d2
commit 2340b89921
2 changed files with 43 additions and 99 deletions
-48
View File
@@ -12,51 +12,3 @@ iD.Way = {
return (!w.nodes.length) || w.nodes[w.nodes.length - 1] === w.nodes[0];
}
};
/*
// JOSM: http://josm.openstreetmap.de/browser/josm/trunk/src/org/openstreetmap/josm/data/osm/Way.java#L466
isClosed: function() {
// summary: Is this a closed way (first and last nodes the same)?
if (!this.children.length) return true;
return this.children[this.children.length - 1] === this.children[0];
},
isType: function(type) {
// summary: Is this a 'way' (always true), an 'area' (closed) or a 'line' (unclosed)?
if (type === 'way') return true;
if (type === 'area') return this.isClosed();
if (type === 'line') return !(this.isClosed());
return false; // Boolean
},
updateBounds: function() {
this._bounds = d3.geo.bounds(iD.GeoJSON.mapping(this));
},
bounds: function() {
// TODO: cache
if (!this._bounds) this.updateBounds();
return this._bounds;
},
// ---------------------
// Bounding-box handling
intersects: function(extent) {
// TODO: rewrite with new id-mapping
return true;
// No-node ways are inside of nothing.
if (!this.children.length) return false;
var bounds = this.bounds();
// left
return !(
// the bottom right is to the top-left
// of the top-left
bounds[1][0] < extent[0][0] &&
bounds[1][1] < extent[0][1] ||
// The top left is to the bottom-right
// of the top-left
bounds[0][0] > extent[1][0] &&
bounds[0][1] > extent[1][1]);
}
};
*/
+43 -51
View File
@@ -1,63 +1,55 @@
iD.Hash = function() {
var hash = {},
s0, // cached location.hash
lat = 90 - 1e-8, // allowable latitude range
map;
var hash = {},
s0, // cached location.hash
lat = 90 - 1e-8, // allowable latitude range
map;
var parser = function(map, s) {
var args = s.split("/").map(Number);
if (args.length < 3 || args.some(isNaN)) return true; // replace bogus hash
else {
map.setZoom(args[0]);
map.setCenter({lat: Math.min(lat, Math.max(-lat, args[1])), lon: args[2]});
}
};
var qs = function(a,b,c,d,e){for(b=/[?&]?([^=]+)=([^&]*)/g,c={},e=decodeURIComponent;d=b.exec(a.replace(/\+/g,' '));c[e(d[1])]=e(d[2]));return c;};
var formatter = function(map) {
var center = map.getCenter(),
var parser = function(map, s) {
var q = qs(s);
var args = (q.map || '').split("/").map(Number);
if (args.length < 3 || args.some(isNaN)) {
return true; // replace bogus hash
} else {
map.setZoom(args[0]);
map.setCenter({lat: Math.min(lat, Math.max(-lat, args[1])), lon: args[2]});
}
};
var formatter = function(map) {
var center = map.getCenter(),
zoom = map.getZoom(),
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
return "#" + zoom.toFixed(2) +
"/" + center.lat.toFixed(precision) +
"/" + center.lon.toFixed(precision);
};
return '#?map=' + zoom.toFixed(2) +
'/' + center.lat.toFixed(precision) +
'/' + center.lon.toFixed(precision);
};
var move = _.throttle(function() {
var s1 = formatter(map);
if (s0 !== s1) location.replace(s0 = s1); // don't recenter the map!
}, 1000);
var move = _.throttle(function() {
var s1 = formatter(map);
if (s0 !== s1) location.replace(s0 = s1); // don't recenter the map!
}, 1000);
function hashchange() {
if (location.hash === s0) return; // ignore spurious hashchange events
if (parser(map, (s0 = location.hash).substring(1)))
move(); // replace bogus hash
}
hash.map = function(x) {
if (!arguments.length) return map;
if (map) {
map.off("move", move);
window.removeEventListener("hashchange", hashchange, false);
function hashchange() {
if (location.hash === s0) return; // ignore spurious hashchange events
if (parser(map, (s0 = location.hash).substring(1)))
move(); // replace bogus hash
}
if (map = x) {
map.on("move", move);
window.addEventListener("hashchange", hashchange, false);
location.hash ? hashchange() : move();
}
return hash;
};
hash.parser = function(x) {
if (!arguments.length) return parser;
parser = x;
return hash;
};
hash.map = function(x) {
if (!arguments.length) return map;
if (map) {
map.off("move", move);
window.removeEventListener("hashchange", hashchange, false);
}
if (map = x) {
map.on("move", move);
window.addEventListener("hashchange", hashchange, false);
location.hash ? hashchange() : move();
}
return hash;
};
hash.formatter = function(x) {
if (!arguments.length) return formatter;
formatter = x;
return hash;
};
return hash;
};