From bbccec75be436b5a32ef93a6578fc80d07fafa66 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 31 Jan 2013 18:20:57 -0500 Subject: [PATCH] Convert Hash to a behavior --- index.html | 2 +- js/id/{renderer => behavior}/hash.js | 52 ++++++++++------------- js/id/id.js | 4 +- test/index.html | 4 +- test/index_packaged.html | 2 +- test/spec/{renderer => behavior}/hash.js | 53 ++++++++---------------- 6 files changed, 47 insertions(+), 70 deletions(-) rename js/id/{renderer => behavior}/hash.js (68%) rename test/spec/{renderer => behavior}/hash.js (51%) diff --git a/index.html b/index.html index 4fe1bd621..870436dd1 100644 --- a/index.html +++ b/index.html @@ -38,7 +38,6 @@ - @@ -93,6 +92,7 @@ + diff --git a/js/id/renderer/hash.js b/js/id/behavior/hash.js similarity index 68% rename from js/id/renderer/hash.js rename to js/id/behavior/hash.js index f4204531f..65e748e28 100644 --- a/js/id/renderer/hash.js +++ b/js/id/behavior/hash.js @@ -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; diff --git a/js/id/id.js b/js/id/id.js index 69e64e5fe..7fa1d76e4 100644 --- a/js/id/id.js +++ b/js/id/id.js @@ -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); diff --git a/test/index.html b/test/index.html index 370b238fe..d03c5a6c7 100644 --- a/test/index.html +++ b/test/index.html @@ -41,7 +41,6 @@ - @@ -89,6 +88,7 @@ + @@ -149,6 +149,7 @@ + @@ -163,7 +164,6 @@ - diff --git a/test/index_packaged.html b/test/index_packaged.html index 007392f3b..60979e236 100644 --- a/test/index_packaged.html +++ b/test/index_packaged.html @@ -44,6 +44,7 @@ + @@ -58,7 +59,6 @@ - diff --git a/test/spec/renderer/hash.js b/test/spec/behavior/hash.js similarity index 51% rename from test/spec/renderer/hash.js rename to test/spec/behavior/hash.js index b0e5b567f..7616bf536 100644 --- a/test/spec/renderer/hash.js +++ b/test/spec/behavior/hash.js @@ -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"); }); });