Convert Hash to a behavior

This commit is contained in:
John Firebaugh
2013-01-31 18:20:57 -05:00
parent 9cb0879818
commit bbccec75be
6 changed files with 47 additions and 70 deletions

View File

@@ -38,7 +38,6 @@
<script src='js/id/renderer/background.js'></script>
<script src='js/id/renderer/background_source.js'></script>
<script src='js/id/renderer/map.js'></script>
<script src='js/id/renderer/hash.js'></script>
<script src="js/id/svg.js"></script>
<script src="js/id/svg/areas.js"></script>
@@ -93,6 +92,7 @@
<script src='js/id/behavior/drag_node.js'></script>
<script src='js/id/behavior/draw.js'></script>
<script src='js/id/behavior/draw_way.js'></script>
<script src='js/id/behavior/hash.js'></script>
<script src='js/id/behavior/hover.js'></script>
<script src='js/id/behavior/select.js'></script>

View File

@@ -1,9 +1,6 @@
iD.Hash = function() {
var hash = { hadHash: false },
s0 = null, // cached location.hash
lat = 90 - 1e-8, // allowable latitude range
controller,
map;
iD.behavior.Hash = function(controller, map) {
var s0 = null, // cached location.hash
lat = 90 - 1e-8; // allowable latitude range
var parser = function(map, s) {
var q = iD.util.stringQs(s);
@@ -61,32 +58,29 @@ iD.Hash = function() {
map.on('drawn.hash', null);
}
hash.controller = function(_) {
if (!arguments.length) return controller;
controller = _;
return hash;
};
function hash() {
map.on('move.hash', move);
hash.map = function(x) {
if (!arguments.length) return map;
if (map) {
map.on("move.hash", null);
window.removeEventListener("hashchange", hashchange, false);
}
map = x;
if (x) {
map.on("move.hash", move);
window.addEventListener("hashchange", hashchange, false);
if (location.hash) {
var q = iD.util.stringQs(location.hash.substring(1));
if (q.id) {
willselect(q.id);
}
hashchange();
hash.hadHash = true;
d3.select(window)
.on('hashchange.hash', hashchange);
if (location.hash) {
var q = iD.util.stringQs(location.hash.substring(1));
if (q.id) {
willselect(q.id);
}
hashchange();
hash.hadHash = true;
}
return hash;
}
hash.off = function() {
map.on('move.hash', null);
d3.select(window)
.on('hashchange.hash', null);
location.hash = "";
};
return hash;

View File

@@ -234,7 +234,9 @@ window.iD = function(container) {
d3.select(document)
.call(keybinding);
var hash = iD.Hash().controller(controller).map(map);
var hash = iD.behavior.Hash(controller, map);
hash();
if (!hash.hadHash) {
map.centerZoom([-77.02271, 38.90085], 20);

View File

@@ -41,7 +41,6 @@
<script src='../js/id/renderer/background.js'></script>
<script src='../js/id/renderer/background_source.js'></script>
<script src='../js/id/renderer/map.js'></script>
<script src='../js/id/renderer/hash.js'></script>
<script src="../js/id/svg.js"></script>
<script src="../js/id/svg/areas.js"></script>
@@ -89,6 +88,7 @@
<script src='../js/id/behavior/drag_node.js'></script>
<script src='../js/id/behavior/draw.js'></script>
<script src='../js/id/behavior/draw_way.js'></script>
<script src='../js/id/behavior/hash.js'></script>
<script src='../js/id/behavior/hover.js'></script>
<script src='../js/id/behavior/select.js'></script>
@@ -149,6 +149,7 @@
<script src="spec/actions/split_way.js"></script>
<script src='spec/actions/unjoin_node.js'></script>
<script src="spec/behavior/hash.js"></script>
<script src="spec/behavior/hover.js"></script>
<script src="spec/geo/extent.js"></script>
@@ -163,7 +164,6 @@
<script src="spec/modes/add_point.js"></script>
<script src="spec/renderer/background.js"></script>
<script src="spec/renderer/hash.js"></script>
<script src="spec/renderer/map.js"></script>
<script src="spec/svg.js"></script>

View File

@@ -44,6 +44,7 @@
<script src="spec/actions/split_way.js"></script>
<script src='spec/actions/unjoin_node.js'></script>
<script src="spec/behavior/hash.js"></script>
<script src="spec/behavior/hover.js"></script>
<script src="spec/geo/extent.js"></script>
@@ -58,7 +59,6 @@
<script src="spec/modes/add_point.js"></script>
<script src="spec/renderer/background.js"></script>
<script src="spec/renderer/hash.js"></script>
<script src="spec/renderer/map.js"></script>
<script src="spec/svg.js"></script>

View File

@@ -1,60 +1,41 @@
describe("iD.Hash", function () {
describe("iD.behavior.Hash", function () {
var hash, map, controller;
beforeEach(function () {
hash = iD.Hash();
map = {
on: function () { return map; },
zoom: function () { return arguments.length ? map : 0; },
center: function () { return arguments.length ? map : [0, 0]; },
centerZoom: function () { return arguments.length ? map : [0, 0]; }
};
controller = {
on: function () { return controller; }
};
hash = iD.behavior.Hash(controller, map);
});
afterEach(function () {
hash.map(null);
location.hash = "";
hash.off();
});
describe("#map()", function () {
it("gets and sets map", function () {
expect(hash.controller(controller).map(map)).to.equal(hash);
expect(hash.map()).to.equal(map);
});
it("sets hadHash if location.hash is present", function () {
location.hash = "map=20.00/38.87952/-77.02405";
hash();
expect(hash.hadHash).to.be.true;
});
it("sets hadHash if location.hash is present", function () {
location.hash = "map=20.00/38.87952/-77.02405";
hash.map(map);
expect(hash.hadHash).to.be.true;
});
it("centerZooms map to requested level", function () {
location.hash = "map=20.00/38.87952/-77.02405";
sinon.spy(map, 'centerZoom');
hash.map(map);
expect(map.centerZoom).to.have.been.calledWith([-77.02405,38.87952], 20.0);
});
it("binds the map's move event", function () {
sinon.spy(map, 'on');
hash.map(map);
expect(map.on).to.have.been.calledWith('move.hash', sinon.match.instanceOf(Function));
});
it("unbinds the map's move event", function () {
sinon.spy(map, 'on');
hash.map(map);
hash.map(null);
expect(map.on).to.have.been.calledWith('move.hash', null);
});
it("centerZooms map to requested level", function () {
location.hash = "map=20.00/38.87952/-77.02405";
sinon.spy(map, 'centerZoom');
hash();
expect(map.centerZoom).to.have.been.calledWith([-77.02405,38.87952], 20.0);
});
describe("on window hashchange events", function () {
beforeEach(function () {
hash.map(map);
hash();
});
function onhashchange(fn) {
@@ -77,7 +58,7 @@ describe("iD.Hash", function () {
sinon.stub(map, 'on')
.withArgs("move.hash", sinon.match.instanceOf(Function))
.yields();
hash.map(map);
hash();
expect(location.hash).to.equal("#map=0.00/0/0");
});
});