diff --git a/js/id/graph/history.js b/js/id/graph/history.js index 501b0a037..41feba27c 100644 --- a/js/id/graph/history.js +++ b/js/id/graph/history.js @@ -1,8 +1,15 @@ iD.History = function() { var stack = [iD.Graph()], - index = 0; + index = 0, + dispatch = d3.dispatch('change'); - return { + function maybeChange() { + if (stack[index].annotation) { + dispatch.change(); + } + } + + var history = { graph: function () { return stack[index]; }, @@ -17,11 +24,13 @@ iD.History = function() { stack = stack.slice(0, index + 1); stack.push(action(this.graph())); index++; + maybeChange(); }, replace: function (action) { // assert(index == stack.length - 1) stack[index] = action(this.graph()); + maybeChange(); }, undo: function () { @@ -29,6 +38,7 @@ iD.History = function() { index--; if (stack[index].annotation) break; } + dispatch.change(); }, redo: function () { @@ -36,6 +46,7 @@ iD.History = function() { index++; if (stack[index].annotation) break; } + dispatch.change(); }, undoAnnotation: function () { @@ -79,5 +90,7 @@ iD.History = function() { 'delete': this['delete']() }; } - } + }; + + return d3.rebind(history, dispatch, 'on'); }; diff --git a/js/id/id.js b/js/id/id.js index 148968e6d..c053504c4 100644 --- a/js/id/id.js +++ b/js/id/id.js @@ -118,7 +118,7 @@ var iD = function(container) { "report a bug " + "/ imagery © 2012 Bing, GeoEye, Getmapping, Intermap, Microsoft.

"); - map.on('update', function() { + history.on('change', function() { var undo = history.undoAnnotation(), redo = history.redoAnnotation(); diff --git a/js/id/renderer/map.js b/js/id/renderer/map.js index 7a720838d..611b2db3c 100644 --- a/js/id/renderer/map.js +++ b/js/id/renderer/map.js @@ -1,7 +1,7 @@ iD.Map = function() { var connection, history, dimensions = [], - dispatch = d3.dispatch('move', 'update'), + dispatch = d3.dispatch('move'), inspector = iD.Inspector(), selection = null, translateStart, @@ -45,7 +45,6 @@ iD.Map = function() { .on('dragend', function () { if (dragging) { dragging = false; - map.update(); redraw(); } }), @@ -517,21 +516,18 @@ iD.Map = function() { map.perform = function(action) { history.perform(action); - map.update(); redraw(); return map; }; map.undo = function() { history.undo(); - map.update(); redraw(); return map; }; map.redo = function() { history.redo(); - map.update(); redraw(); return map; }; @@ -626,5 +622,5 @@ iD.Map = function() { map.selectEntity = selectEntity; map.dblclickEnable = dblclickEnable; - return d3.rebind(map, dispatch, 'on', 'move', 'update'); + return d3.rebind(map, dispatch, 'on', 'move'); }; diff --git a/test/spec/Map.js b/test/spec/Map.js index 82ff270d5..ec8d9484d 100644 --- a/test/spec/Map.js +++ b/test/spec/Map.js @@ -61,35 +61,6 @@ describe('Map', function() { }); }); - describe("update", function () { - var spy; - - beforeEach(function () { - spy = sinon.spy(); - map.history({ - perform: function () {}, - undo: function () {}, - redo: function () {} - }); - map.on('update', spy); - }); - - it("is emitted when performing an action", function () { - map.perform(iD.actions.noop); - expect(spy).to.have.been.called; - }); - - it("is emitted when undoing an action", function () { - map.undo(); - expect(spy).to.have.been.called; - }); - - it("is emitted when redoing an action", function () { - map.redo(); - expect(spy).to.have.been.called; - }); - }); - describe("surface", function() { it("is an SVG element", function() { expect(map.surface.node().tagName).to.equal("svg"); diff --git a/test/spec/history.js b/test/spec/history.js index ad2a2585b..821e15390 100644 --- a/test/spec/history.js +++ b/test/spec/history.js @@ -38,4 +38,39 @@ describe("History", function () { expect(history.redoAnnotation()).to.equal("action"); }); }); + + describe("change", function () { + var spy; + + beforeEach(function () { + spy = sinon.spy(); + }); + + it("is not emitted when performing a noop", function () { + history.on('change', spy); + history.perform(iD.actions.noop); + expect(spy).not.to.have.been.called; + }); + + it("is emitted when performing an action", function () { + history.on('change', spy); + history.perform(action); + expect(spy).to.have.been.called; + }); + + it("is emitted when undoing an action", function () { + history.perform(action); + history.on('change', spy); + history.undo(); + expect(spy).to.have.been.called; + }); + + it("is emitted when redoing an action", function () { + history.perform(action); + history.undo(); + history.on('change', spy); + history.redo(); + expect(spy).to.have.been.called; + }); + }); });