From cd6dc510f609585737898d9c6e74c7df51c00889 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 18 Feb 2014 11:56:54 +0100 Subject: [PATCH] smartxml: Transaction object - first take Just wrapping transformation/transformations array on the undo/redo stacks --- src/smartxml/smartxml.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 1e7fc21..013ea9a 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -494,7 +494,7 @@ $.extend(Document.prototype, Backbone.Events, { if(this._transactionInProgress) { this._transactionStack.push(transformation); } else { - this.undoStack.push(transformation); + this.undoStack.push(new Transaction([transformation])); } } if(!this._undoInProgress && this._transformationLevel === 1) { @@ -510,20 +510,15 @@ $.extend(Document.prototype, Backbone.Events, { } }, undo: function() { - var transformationObject = this.undoStack.pop(), + var transaction = this.undoStack.pop(), doc = this, transformations, stopAt; - if(transformationObject) { + if(transaction) { this._undoInProgress = true; - if(_.isArray(transformationObject)) { - // We will modify this array in a minute so make sure we work on a copy. - transformations = transformationObject.slice(0); - } else { - // Lets normalize single transformation to a transaction containing one transformation. - transformations = [transformationObject]; - } + // We will modify this array in a minute so make sure we work on a copy. + transformations = transaction.transformations.slice(0); if(transformations.length > 1) { // In case of real transactions we don't want to run undo on all of transformations if we don't have to. @@ -546,20 +541,18 @@ $.extend(Document.prototype, Backbone.Events, { }); this._undoInProgress = false; - this.redoStack.push(transformationObject); + this.redoStack.push(transaction); } }, redo: function() { - var transformationObject = this.redoStack.pop(), - transformations; - if(transformationObject) { + var transaction = this.redoStack.pop(); + if(transaction) { this._transformationLevel++; - transformations = _.isArray(transformationObject) ? transformationObject : [transformationObject]; - transformations.forEach(function(t) { + transaction.transformations.forEach(function(t) { t.run({beUndoable: true}); }); this._transformationLevel--; - this.undoStack.push(transformationObject); + this.undoStack.push(transaction); } }, @@ -576,7 +569,7 @@ $.extend(Document.prototype, Backbone.Events, { } this._transactionInProgress = false; if(this._transactionStack.length) { - this.undoStack.push(this._transactionStack); + this.undoStack.push(new Transaction(this._transactionStack)); this._transactionStack = []; } }, @@ -614,6 +607,10 @@ $.extend(Document.prototype, Backbone.Events, { } }); +var Transaction = function(transformations) { + this.transformations = transformations || []; +}; + return { documentFromXML: function(xml) { -- 2.20.1