Move responsibility for dispatching change event to history

This commit is contained in:
John Firebaugh
2012-12-03 18:00:29 -05:00
parent e93c9624d8
commit 8a8d6fae32
5 changed files with 54 additions and 39 deletions
+16 -3
View File
@@ -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');
};
+1 -1
View File
@@ -118,7 +118,7 @@ var iD = function(container) {
"<a href='http://github.com/systemed/iD/issues'>report a bug</a> " +
"/ imagery <a href='http://opengeodata.org/microsoft-imagery-details'>&copy; 2012</a> Bing, GeoEye, Getmapping, Intermap, Microsoft.</p>");
map.on('update', function() {
history.on('change', function() {
var undo = history.undoAnnotation(),
redo = history.redoAnnotation();
+2 -6
View File
@@ -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');
};
-29
View File
@@ -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");
+35
View File
@@ -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;
});
});
});