DocumentTextNode.after
[fnpeditor.git] / modules / documentCanvas / canvas / documentElement.js
index 637da5f..6fea275 100644 (file)
@@ -31,6 +31,30 @@ $.extend(DocumentElement.prototype, {
             toret.push(element);
         });
         return toret;
+    },
+    parent: function() {
+        return documentElementFromHTMLElement(this.$element.parent()[0]);
+    },
+
+    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;
     }
 });
 
@@ -45,12 +69,71 @@ var DocumentTextElement = function(htmlElement) {
 DocumentNodeElement.prototype = new DocumentElement();
 DocumentTextElement.prototype = new DocumentElement();
 
+var manipulate = function(e, params, action) {
+    var dom;
+    if(params instanceof DocumentElement) {
+        dom = params.dom()
+    } else {
+        dom = DocumentNodeElement.createDOM(params);
+    }
+    e.$element[action](dom);
+    return documentElementFromHTMLElement(dom);
+};
+
+$.extend(DocumentNodeElement.prototype, {
+    append: function(params) {
+        manipulate(this, params, 'append');
+    },
+    before: function(params) {
+        manipulate(this, params, 'before');
+
+    },
+    after: function(params) {
+        manipulate(this, params, 'after');
+    }
+});
+
+DocumentNodeElement.createDOM = function(params) {
+    var dom;
+    if(params.text) {
+        dom = $(document.createTextNode(params.text));
+    } else {
+        dom = $('<' + params.tag + '>');
+        if(params.klass)
+            dom.attr('class', params.klass);
+    }
+    return dom;
+};
+
+$.extend(DocumentTextElement.prototype, {
+    setText: function(text) {
+        this.$element[0].data = text;
+    },
+    getText: function() {
+        return this.$element.text();
+    },
+    after: function(params) {
+        if(params.text || params instanceof DocumentTextElement)
+            return false;
+        var dom;
+        if(params instanceof DocumentNodeElement) {
+            dom = params.dom();
+        } else {
+            dom = DocumentNodeElement.createDOM(params);
+        }
+        this.$element.wrap('<div>');
+        this.$element.parent().after(dom[0]);
+        this.$element.unwrap();
+        return documentElementFromHTMLElement(dom[0]);
+    }
+});
+
 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) {