From 2c9732f54791e9e223210721196ed35705fd0c8a Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 8 Feb 2013 11:04:05 -0500 Subject: [PATCH] Fix localstorage history clearing --- js/id/graph/history.js | 15 ++++++++------- js/id/ui.js | 11 ++++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/js/id/graph/history.js b/js/id/graph/history.js index 5f648b434..caefc45d2 100644 --- a/js/id/graph/history.js +++ b/js/id/graph/history.js @@ -175,7 +175,7 @@ iD.History = function(context) { if (!lock) return; context.storage(getKey('lock'), null); - if (!stack.length) { + if (stack.length <= 1) { context.storage(getKey('history'), null); context.storage(getKey('nextIDs'), null); context.storage(getKey('index'), null); @@ -183,9 +183,11 @@ iD.History = function(context) { } var json = JSON.stringify(stack.map(function(i) { - return _.extend(i, { - graph: i.graph.entities - }); + return { + annotation: i.annotation, + imagery_used: i.imagery_used, + entities: i.graph.entities + } })); context.storage(getKey('history'), json); @@ -201,8 +203,7 @@ iD.History = function(context) { }, restorableChanges: function() { - if (!this.lock()) return false; - return !!context.storage(getKey('history')); + return lock && !!context.storage(getKey('history')); }, load: function() { @@ -221,7 +222,7 @@ iD.History = function(context) { context.storage(getKey('index', null)); stack = JSON.parse(json).map(function(d, i) { - d.graph = iD.Graph(stack[0].graph).load(d.graph); + d.graph = iD.Graph(stack[0].graph).load(d.entities); return d; }); dispatch.change(); diff --git a/js/id/ui.js b/js/id/ui.js index f18e0ef0c..359a39dcb 100644 --- a/js/id/ui.js +++ b/js/id/ui.js @@ -193,11 +193,12 @@ iD.ui = function(context) { contributors.append('span') .attr('class', 'contributor-count'); + window.onbeforeunload = function() { + history.save(); + if (history.hasChanges()) return 'You have unsaved changes'; + }; + history.on('change.editor', function() { - window.onbeforeunload = history.hasChanges() ? function() { - history.save(); - return 'You have unsaved changes.'; - } : null; var undo = history.undoAnnotation(), redo = history.redoAnnotation(); @@ -264,7 +265,7 @@ iD.ui = function(context) { context.storage('sawSplash', true); } - if (history.restorableChanges()) { + if (history.lock() && history.restorableChanges()) { iD.ui.restore(context.container(), history); }