From c5c65ccdfd6c421424a3153afcf3ae64d5e73198 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 7 Dec 2012 12:29:17 -0500 Subject: [PATCH] Include difference in history change events --- js/id/graph/history.js | 26 ++++++++++++++++++-------- test/spec/graph/history.js | 8 ++++---- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/js/id/graph/history.js b/js/id/graph/history.js index 683b0af16..21c1d9326 100644 --- a/js/id/graph/history.js +++ b/js/id/graph/history.js @@ -19,10 +19,8 @@ iD.History = function() { return {graph: graph, annotation: annotation}; } - function maybeChange() { - if (stack[index].annotation) { - dispatch.change(); - } + function change(previous) { + dispatch.change(history.graph().difference(previous)); } var history = { @@ -37,32 +35,44 @@ iD.History = function() { }, perform: function () { + var previous = stack[index].graph; + stack = stack.slice(0, index + 1); stack.push(perform(arguments)); index++; - dispatch.change(); + + change(previous); }, replace: function () { + var previous = stack[index].graph; + // assert(index == stack.length - 1) stack[index] = perform(arguments); - dispatch.change(); + + change(previous); }, undo: function () { + var previous = stack[index].graph; + while (index > 0) { index--; if (stack[index].annotation) break; } - dispatch.change(); + + change(previous); }, redo: function () { + var previous = stack[index].graph; + while (index < stack.length - 1) { index++; if (stack[index].annotation) break; } - dispatch.change(); + + change(previous); }, undoAnnotation: function () { diff --git a/test/spec/graph/history.js b/test/spec/graph/history.js index be7237292..15fe10964 100644 --- a/test/spec/graph/history.js +++ b/test/spec/graph/history.js @@ -28,7 +28,7 @@ describe("iD.History", function () { it("emits a change event", function () { history.on('change', spy); history.perform(action); - expect(spy).to.have.been.called; + expect(spy).to.have.been.calledWith([]); }); it("performs multiple actions", function () { @@ -57,7 +57,7 @@ describe("iD.History", function () { it("emits a change event", function () { history.on('change', spy); history.replace(action); - expect(spy).to.have.been.called; + expect(spy).to.have.been.calledWith([]); }); it("performs multiple actions", function () { @@ -87,7 +87,7 @@ describe("iD.History", function () { history.perform(action); history.on('change', spy); history.undo(); - expect(spy).to.have.been.called; + expect(spy).to.have.been.calledWith([]); }); }); @@ -97,7 +97,7 @@ describe("iD.History", function () { history.undo(); history.on('change', spy); history.redo(); - expect(spy).to.have.been.called; + expect(spy).to.have.been.calledWith([]); }); });