Merge pull request #13 from geowarin/master

Added undoAll() and redoAll() methods
This commit is contained in:
Oliver Sartun 2014-08-24 23:52:50 +02:00
commit 698a9b938a
2 changed files with 46 additions and 9 deletions

View File

@ -249,13 +249,14 @@
/**
* The main undo/redo function.
*
* @param {String} which Either "undo" or "redo"
* @param {UndoManager} manager The UndoManager-instance on which an "undo"/"redo"-Event is triggered afterwards
* @param {UndoStack} stack The UndoStack on which we perform
* @param {Boolean} magic If true, undoes / redoes all actions with the same magicFusionIndex
* @return {undefined}
*/
function managerUndoRedo (which, manager, stack, magic) {
* @param {String} which Either "undo" or "redo"
* @param {UndoManager} manager The UndoManager-instance on which an "undo"/"redo"-Event is triggered afterwards
* @param {UndoStack} stack The UndoStack on which we perform
* @param {Boolean} magic If true, undoes / redoes all actions with the same magicFusionIndex
* @param {Boolean} everything If true, undoes / redoes every actions that have been tracked
* @return {undefined}
*/
function managerUndoRedo (which, manager, stack, magic, everything) {
if (stack.isCurrentlyUndoRedoing ||
(which === "undo" && stack.pointer === -1) ||
(which === "redo" && stack.pointer === stack.length - 1)) {
@ -272,7 +273,11 @@
stack.pointer++;
action = stack.at(stack.pointer);
}
actions = magic ? stack.where({"magicFusionIndex": action.get("magicFusionIndex")}) : [action];
if (everything)
actions = _.clone(stack.models);
else
actions = magic ? stack.where({"magicFusionIndex": action.get("magicFusionIndex")}) : [action];
stack.pointer += (isUndo ? -1 : 1) * (actions.length - 1);
while (action = isUndo ? actions.pop() : actions.shift()) {
// Here we're calling the Action's undo / redo method
@ -675,6 +680,11 @@
undo: function (magic) {
managerUndoRedo("undo", this, this.stack, magic);
},
undoAll: function () {
managerUndoRedo("undo", this, this.stack, false, true);
},
/**
* Redoes a previously undone action or a set of actions.
* @param {Boolean} [magic] If true, all actions that happened basically at the same time are redone together
@ -683,6 +693,10 @@
redo: function (magic) {
managerUndoRedo("redo", this, this.stack, magic);
},
redoAll: function () {
managerUndoRedo("redo", this, this.stack, false, true);
},
/**
* Checks if there's an action in the stack that can be undone / redone
* @param {String} type Either "undo" or "redo"

View File

@ -306,6 +306,29 @@ test("Clearing all actions", function () {
deepEqual(model.toJSON(), {"t": 2}, "Clearing actions before redoing was successful");
})
test("Undoing all actions", function () {
var model = new Backbone.Model({
"t": 1
});
var UndoManager = new Backbone.UndoManager({
track: true,
register: model
});
model.set("t", 2);
model.set("t", 3);
model.set("t", 4);
UndoManager.undoAll();
deepEqual(model.toJSON(), {"t": 1}, "Calling undoAll was successful");
UndoManager.redoAll();
deepEqual(model.toJSON(), {"t": 4}, "Calling redoAll was successful");
})
/**
* Async tests for magic condensation
*/