smartxml: Document.transaction now returns value from a callback
[fnpeditor.git] / src / smartxml / smartxml.js
index d085742..1e7fc21 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]};
     },
 
@@ -575,9 +582,11 @@ $.extend(Document.prototype, Backbone.Events, {
     },
 
     transaction: function(callback, context) {
+        var toret;
         this.startTransaction();
-        callback.call(context);
+        toret = callback.call(context);
         this.endTransaction();
+        return toret;
     },
 
     getNodeByPath: function(path) {
@@ -591,9 +600,15 @@ $.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});
     }