From b69ef106917440c893f7737ad09c61ed91829ca2 Mon Sep 17 00:00:00 2001 From: geowarin Date: Sun, 24 Aug 2014 18:47:54 +0200 Subject: [PATCH] Added undoAll() and redoAll() methods --- Backbone.Undo.js | 32 +++++++++++++++++++++++--------- Tests/Backbone.Undo.Tests.js | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Backbone.Undo.js b/Backbone.Undo.js index 6180760..c102142 100644 --- a/Backbone.Undo.js +++ b/Backbone.Undo.js @@ -248,14 +248,15 @@ /** * 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" diff --git a/Tests/Backbone.Undo.Tests.js b/Tests/Backbone.Undo.Tests.js index 8587fba..2c5f718 100644 --- a/Tests/Backbone.Undo.Tests.js +++ b/Tests/Backbone.Undo.Tests.js @@ -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 */