DocumentElement.childIndex
[fnpeditor.git] / modules / documentCanvas / canvas / documentElement.js
index cd94759..d1973b8 100644 (file)
@@ -38,6 +38,23 @@ $.extend(DocumentElement.prototype, {
 
     sameNode: function(other) {
         return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
+    },
+
+    wrapWithNodeElement: function(wlxmlNode) {
+        this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]);
+        return documentElementFromHTMLElement(this.$element.parent().get(0));
+    },
+
+    childIndex: function(child) {
+        var children = this.children(),
+            toret = null;
+        children.forEach(function(c, idx) {
+            if(c.sameNode(child)) {
+                toret = idx;
+                return false;
+            }
+        });
+        return toret;
     }
 });
 
@@ -52,12 +69,32 @@ var DocumentTextElement = function(htmlElement) {
 DocumentNodeElement.prototype = new DocumentElement();
 DocumentTextElement.prototype = new DocumentElement();
 
+$.extend(DocumentNodeElement.prototype, {
+    append: function(params) {
+        var to_append = DocumentNodeElement.createDOM(params.tag, params.klass);
+        this.$element.append(to_append);
+        return documentElementFromHTMLElement(to_append);
+    },
+    after: function(params) {
+        var to_append = DocumentNodeElement.createDOM(params.tag, params.klass);
+        this.$element.after(to_append);
+        return documentElementFromHTMLElement(to_append);   
+    }
+});
+
+DocumentNodeElement.createDOM = function(tag, klass) {
+    var dom = $('<' + tag + '>');
+    if(klass)
+        dom.attr('class', klass);
+    return dom;
+};
+
 var documentElementFromHTMLElement = function(htmlElement) {
     if(htmlElement.nodeType === Node.ELEMENT_NODE)
         return new DocumentNodeElement(htmlElement);
     if(htmlElement.nodeType === Node.TEXT_NODE)
         return new DocumentTextElement(htmlElement);
-}
+};
 
 return {
     wrap: function(htmlElement) {