diff --git a/js/id/connection.js b/js/id/connection.js index 399c48d86..794232682 100644 --- a/js/id/connection.js +++ b/js/id/connection.js @@ -1,4 +1,4 @@ -iD.Connection = function() { +iD.Connection = function(context) { var event = d3.dispatch('auth', 'load'), url = 'http://www.openstreetmap.org', @@ -7,7 +7,7 @@ iD.Connection = function() { keys, inflight = {}, loadedTiles = {}, - oauth = iD.OAuth().url(url); + oauth = iD.OAuth(context).url(url); function changesetUrl(changesetId) { return url + '/browse/changeset/' + changesetId; diff --git a/js/id/id.js b/js/id/id.js index ef026a097..0b2bbd617 100644 --- a/js/id/id.js +++ b/js/id/id.js @@ -1,34 +1,42 @@ window.iD = function () { var context = {}, history = iD.History(), - connection = iD.Connection(), + storage = localStorage || {}, dispatch = d3.dispatch('enter', 'exit'), mode, container, ui = iD.ui(context), map = iD.Map(context); + context.storage = function(k, v) { + if (arguments.length === 1) return storage[k]; + else storage[k] = v; + }; + + // the connection requires .storage() to be available on calling. + var connection = iD.Connection(context); + /* Straight accessors. Avoid using these if you can. */ - context.ui = function () { return ui; }; - context.connection = function () { return connection; }; - context.history = function () { return history; }; - context.map = function () { return map; }; + context.ui = function() { return ui; }; + context.connection = function() { return connection; }; + context.history = function() { return history; }; + context.map = function() { return map; }; /* History */ - context.graph = history.graph; + context.graph = history.graph; context.perform = history.perform; context.replace = history.replace; - context.pop = history.pop; - context.undo = history.undo; - context.redo = history.undo; + context.pop = history.pop; + context.undo = history.undo; + context.redo = history.undo; context.changes = history.changes; /* Graph */ - context.entity = function (id) { + context.entity = function(id) { return history.graph().entity(id); }; - context.geometry = function (id) { + context.geometry = function(id) { return context.entity(id).geometry(history.graph()); }; @@ -49,22 +57,22 @@ window.iD = function () { }; /* Behaviors */ - context.install = function (behavior) { + context.install = function(behavior) { context.surface().call(behavior); }; - context.uninstall = function (behavior) { + context.uninstall = function(behavior) { context.surface().call(behavior.off); }; /* Map */ - context.background = function () { return map.background; }; - context.surface = function () { return map.surface; }; + context.background = function() { return map.background; }; + context.surface = function() { return map.surface; }; context.projection = map.projection; - context.tail = map.tail; - context.redraw = map.redraw; + context.tail = map.tail; + context.redraw = map.redraw; - context.container = function (_) { + context.container = function(_) { if (!arguments.length) return container; container = _; return context; diff --git a/js/id/oauth.js b/js/id/oauth.js index a09f2affc..4aa1d1876 100644 --- a/js/id/oauth.js +++ b/js/id/oauth.js @@ -1,4 +1,4 @@ -iD.OAuth = function() { +iD.OAuth = function(context) { var baseurl = 'http://www.openstreetmap.org', o = {}, keys, @@ -6,10 +6,6 @@ iD.OAuth = function() { function keyclean(x) { return x.replace(/\W/g, ''); } - if (token('oauth_token')) { - o.oauth_token = token('oauth_token'); - } - function timenonce(o) { o.oauth_timestamp = ohauth.timestamp(); o.oauth_nonce = ohauth.nonce(); @@ -18,10 +14,11 @@ iD.OAuth = function() { // token getter/setter, namespaced to the current `apibase` value. function token(k, x) { - if (arguments.length == 2) { - localStorage[keyclean(baseurl) + k] = x; - } - return localStorage[keyclean(baseurl) + k]; + return context.storage(keyclean(baseurl) + k, x); + } + + if (token('oauth_token')) { + o.oauth_token = token('oauth_token'); } oauth.authenticated = function() { diff --git a/js/id/ui.js b/js/id/ui.js index 5714e05ca..c28ca1282 100644 --- a/js/id/ui.js +++ b/js/id/ui.js @@ -247,9 +247,9 @@ iD.ui = function (context) { context.enter(iD.modes.Browse(context)); - if (!localStorage.sawSplash) { + if (!context.storage('sawSplash')) { iD.ui.splash(); - localStorage.sawSplash = true; + context.storage('sawSplash', true); } }; }; diff --git a/js/id/ui/commit.js b/js/id/ui/commit.js index 6c1133b35..09bb99404 100644 --- a/js/id/ui/commit.js +++ b/js/id/ui/commit.js @@ -38,7 +38,7 @@ iD.ui.commit = function(context) { comment_section.append('textarea') .attr('class', 'changeset-comment') .attr('placeholder', 'Brief Description of your contributions') - .property('value', localStorage.comment || '') + .property('value', context.storage('comment') || '') .node().select(); var commit_info = diff --git a/test/spec/connection.js b/test/spec/connection.js index 652c1419b..cdc439a46 100644 --- a/test/spec/connection.js +++ b/test/spec/connection.js @@ -2,7 +2,8 @@ describe('iD.Connection', function () { var c; beforeEach(function () { - c = new iD.Connection(); + context = iD(); + c = new iD.Connection(context); }); it('is instantiated', function () { diff --git a/test/spec/oauth.js b/test/spec/oauth.js index 8fd57c18c..b7ae1074f 100644 --- a/test/spec/oauth.js +++ b/test/spec/oauth.js @@ -2,7 +2,8 @@ describe('iD.OAuth', function() { var o; beforeEach(function() { - o = iD.OAuth(); + context = iD(); + o = iD.OAuth(context); }); describe('#logout', function() {