smartxml: Transaction object - first take
[fnpeditor.git] / src / smartxml / smartxml.js
index d87ec54..013ea9a 100644 (file)
@@ -66,7 +66,7 @@ $.extend(DocumentNode.prototype, {
             }
         });
 
-        if(idx !== 'undefined') {
+        if(idx !== undefined) {
             nodePath = nodePath.slice(0, idx);
         }
         toret = nodePath.map(function(node) {return node.getIndex(); });
@@ -123,6 +123,9 @@ $.extend(DocumentNode.prototype, {
         var node = (metaData && metaData.node) ? metaData.node : this,
             event = new events.ChangeEvent(type, $.extend({node: node}, metaData || {}));
         if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) {
+            if(type === 'nodeMoved') {
+                event.meta.parent = origParent;
+            }
             this.document.trigger('change', event);
         }
         if((type === 'nodeAdded' || type === 'nodeMoved') && !this.document.containsNode(this) && nodeWasContained) {
@@ -370,13 +373,17 @@ $.extend(Document.prototype, Backbone.Events, {
             return noSiblingParents;
         }
 
-        var i;
-        for(i = 0; i < Math.min(parents1.length, parents2.length); i++) {
+        var stop = Math.min(parents1.length, parents2.length),
+            i;
+        for(i = 0; i < stop; i++) {
             if(parents1[i].sameNode(parents2[i])) {
                 continue;
             }
             break;
         }
+        if(i === stop) {
+            i--;
+        }
         return {node1: parents1[i], node2: parents2[i]};
     },
 
@@ -487,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) {
@@ -503,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.
@@ -539,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);
         }
     },
 
@@ -569,11 +569,19 @@ $.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 = [];
         }
     },
 
+    transaction: function(callback, context) {
+        var toret;
+        this.startTransaction();
+        toret = callback.call(context);
+        this.endTransaction();
+        return toret;
+    },
+
     getNodeByPath: function(path) {
         var toret = this.root;
         path.forEach(function(idx) {
@@ -585,14 +593,24 @@ $.extend(Document.prototype, Backbone.Events, {
     _defineDocumentProperties: function($document) {
         var doc = this;
         Object.defineProperty(doc, 'root', {get: function() {
+            if(!$document) {
+                return null;
+            }
             return doc.createDocumentNode($document[0]);
         }, configurable: true});
         Object.defineProperty(doc, 'dom', {get: function() {
+            if(!$document) {
+                return null;
+            }
             return $document[0];
         }, configurable: true});
     }
 });
 
+var Transaction = function(transformations) {
+    this.transformations = transformations || [];
+};
+
 
 return {
     documentFromXML: function(xml) {