smartxml: emitting nodeDetached event
[fnpeditor.git] / src / smartxml / smartxml.js
index 28a88ee..18664c7 100644 (file)
@@ -27,7 +27,9 @@ $.extend(DocumentNode.prototype, {
     },
 
     detach: function() {
+        var parent = this.parent();
         this._$.detach();
+        this.triggerChangeEvent('nodeDetached', {parent: parent});
         return this;
     },
 
@@ -40,15 +42,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 +69,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 +173,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 +281,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);
@@ -297,7 +321,13 @@ $.extend(Document.prototype, Backbone.Events, {
                 from = node[0];
             }
         }
-        return new this.ElementNodeFactory(from, this);
+        var Factory;
+        if(from.nodeType === Node.TEXT_NODE) {
+            Factory = this.TextNodeFactory;
+        } else if(from.nodeType === Node.ELEMENT_NODE) {
+            Factory = this.ElementNodeFactory;
+        }
+        return new Factory(from, this);
     },
 
     createTextNode: function(nativeNode) {
@@ -316,6 +346,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 +408,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();