Fallback for browsers that do not support localStorage. Fixes #591

This commit is contained in:
Tom MacWright
2013-02-01 13:12:46 -05:00
parent f1b6f5b14a
commit 031c8d655e
7 changed files with 41 additions and 34 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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() {

View File

@@ -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);
}
};
};

View File

@@ -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 =

View File

@@ -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 () {

View File

@@ -2,7 +2,8 @@ describe('iD.OAuth', function() {
var o;
beforeEach(function() {
o = iD.OAuth();
context = iD();
o = iD.OAuth(context);
});
describe('#logout', function() {