smartxml: DocumentNode.getIndex function for syntactic sugar
[fnpeditor.git] / src / smartxml / smartxml.js
index 28a88ee..b4036d3 100644 (file)
@@ -40,15 +40,17 @@ $.extend(DocumentNode.prototype, {
     },
 
     after: function(node) {
-        node = node instanceof ElementNode ? node : this.document.createElementNode(node);
-        this._$.after(node.nativeNode);
-        return node;
+        var insertion = this.getNodeInsertion(node);
+        this._$.after(insertion.ofNode.nativeNode);
+        this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode});
+        return insertion.ofNode;
     },
 
     before: function(node) {
-        node = node instanceof ElementNode ? node : this.document.createElementNode(node);
-        this._$.before(node.nativeNode);
-        return node;
+        var insertion = this.getNodeInsertion(node);
+        this._$.before(insertion.ofNode.nativeNode);
+        this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode});
+        return insertion.ofNode;
     },
 
     wrapWith: function(node) {
@@ -65,6 +67,22 @@ $.extend(DocumentNode.prototype, {
         var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {}));
         this.document.trigger('change', event);
     },
+    
+    getNodeInsertion: function(node) {
+        var insertion = {};
+        if(node instanceof DocumentNode) {
+            insertion.ofNode = node;
+            insertion.insertsNew = !this.document.containsNode(node);
+        } else {
+          insertion.ofNode = this.document.createElementNode(node);
+          insertion.insertsNew = true;
+        }
+        return insertion;
+    },
+
+    getIndex: function() {
+        return this.parent().indexOf(this);
+    }
 });
 
 var ElementNode = function(nativeNode, document) {
@@ -153,13 +171,17 @@ $.extend(ElementNode.prototype, {
     },
 
     append: function(node) {
-        node = node instanceof DocumentNode ? node : this.document.createElementNode(node);
-        this._$.append(node.nativeNode);
+        var insertion = this.getNodeInsertion(node);
+        this._$.append(insertion.ofNode.nativeNode);
+        this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode});
+        return insertion.ofNode;
     },
 
     prepend: function(node) {
-        node = node instanceof DocumentNode ? node : this.document.createElementNode(node);
-        this._$.prepend(node.nativeNode);
+        var insertion = this.getNodeInsertion(node);
+        this._$.prepend(insertion.ofNode.nativeNode);
+        this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode});
+        return insertion.ofNode;
     },
 
     unwrapContent: function() {
@@ -257,7 +279,7 @@ $.extend(TextNode.prototype, {
                 textNodeIdx: this.parent().indexOf(this),
                 offsetStart: Math.min(desc.start, desc.end),
                 offsetEnd: Math.max(desc.start, desc.end),
-                _with: {tag: desc.tagName, attrs: desc.attrs}
+                _with: {tagName: desc.tagName, attrs: desc.attrs}
             });
         } else {
             return DocumentNode.prototype.wrapWith.call(this, desc);
@@ -316,6 +338,10 @@ $.extend(Document.prototype, Backbone.Events, {
         return this.root.toXML();
     },
 
+    containsNode: function(node) {
+        return node._$.parents().index(this.root._$) !== -1;
+    },
+
     wrapNodes: function(params) {
         if(!(params.element1.parent().sameNode(params.element2.parent()))) {
             throw new Error('Wrapping non-sibling nodes not supported.');
@@ -374,7 +400,7 @@ $.extend(Document.prototype, Backbone.Events, {
             throw new Error('Wrapping text in non-sibling text nodes not supported.');
         }
         
-        var wrapperElement = this.createElementNode({tagName: params._with.tag, attrs: params._with.attrs});
+        var wrapperElement = this.createElementNode({tagName: params._with.tagName, attrs: params._with.attrs});
         textNode1.after(wrapperElement);
         textNode1.detach();