smartxml: Transaction object - first take
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 18 Feb 2014 10:56:54 +0000 (11:56 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 18 Feb 2014 10:56:54 +0000 (11:56 +0100)
Just wrapping transformation/transformations array on the undo/redo stacks

src/smartxml/smartxml.js

index 1e7fc21..013ea9a 100644 (file)
@@ -494,7 +494,7 @@ $.extend(Document.prototype, Backbone.Events, {
                         if(this._transactionInProgress) {
                             this._transactionStack.push(transformation);
                         } else {
                         if(this._transactionInProgress) {
                             this._transactionStack.push(transformation);
                         } else {
-                            this.undoStack.push(transformation);
+                            this.undoStack.push(new Transaction([transformation]));
                         }
                     }
                     if(!this._undoInProgress && this._transformationLevel === 1) {
                         }
                     }
                     if(!this._undoInProgress && this._transformationLevel === 1) {
@@ -510,20 +510,15 @@ $.extend(Document.prototype, Backbone.Events, {
         }
     },
     undo: function() {
         }
     },
     undo: function() {
-        var transformationObject = this.undoStack.pop(),
+        var transaction = this.undoStack.pop(),
             doc = this,
             transformations, stopAt;
 
             doc = this,
             transformations, stopAt;
 
-        if(transformationObject) {
+        if(transaction) {
             this._undoInProgress = true;
 
             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.
 
             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._undoInProgress = false;
-            this.redoStack.push(transformationObject);
+            this.redoStack.push(transaction);
         }
     },
     redo: function() {
         }
     },
     redo: function() {
-        var transformationObject = this.redoStack.pop(),
-            transformations;
-        if(transformationObject) {
+        var transaction = this.redoStack.pop();
+        if(transaction) {
             this._transformationLevel++;
             this._transformationLevel++;
-            transformations = _.isArray(transformationObject) ? transformationObject : [transformationObject];
-            transformations.forEach(function(t) {
+            transaction.transformations.forEach(function(t) {
                 t.run({beUndoable: true});
             });
             this._transformationLevel--;
                 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._transactionInProgress = false;
         if(this._transactionStack.length) {
-            this.undoStack.push(this._transactionStack);
+            this.undoStack.push(new Transaction(this._transactionStack));
             this._transactionStack = [];
         }
     },
             this._transactionStack = [];
         }
     },
@@ -614,6 +607,10 @@ $.extend(Document.prototype, Backbone.Events, {
     }
 });
 
     }
 });
 
+var Transaction = function(transformations) {
+    this.transformations = transformations || [];
+};
+
 
 return {
     documentFromXML: function(xml) {
 
 return {
     documentFromXML: function(xml) {