Refactoring: nodeElement renders itself with its contents
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 24 Jul 2013 16:30:44 +0000 (18:30 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 24 Jul 2013 16:30:44 +0000 (18:30 +0200)
modules/documentCanvas/canvas/canvas.js
modules/documentCanvas/canvas/documentElement.js

index 96206c3..8882c1c 100644 (file)
@@ -320,35 +320,8 @@ $.extend(Canvas.prototype, {
     },
 
     toXML: function() {
     },
 
     toXML: function() {
-        var d = function(element, parent, level) {
-            console.log(element.getText ? 'text: ' + element.getText() : 'node: ' + element.getWlxmlTag());
-            var isElementNode = element instanceof documentElement.DocumentNodeElement;
-            var parentChildren = parent.contents();
-            var dom;
-
-            var elXML = element.toXML(level);
-            if(parentChildren.length > 2 && parentChildren[0].nodeType === Node.TEXT_NODE && parentChildren[1].nodeType == Node.TEXT_NODE) {
-                $(parentChildren[0]).after(elXML);
-            } else {
-                parent.prepend(elXML);
-            }
-
-
-            if(isElementNode) {
-                elXML.each(function() {
-                    if(this.nodeType === Node.ELEMENT_NODE) {
-                        dom = $(this);
-                        return;
-                    }
-                });
-                var elementChildren = element.children();
-                for(var i = elementChildren.length - 1; i >= 0; i--) {
-                    d(elementChildren[i], dom, level + 1);
-                }
-            }
-        }
         var parent = $('<div>');
         var parent = $('<div>');
-        d(this.doc(), parent, 0);
+        this.doc().toXML(parent, 0)
         return parent.html();
     }
 });
         return parent.html();
     }
 });
index 969d85b..9178a5a 100644 (file)
@@ -148,6 +148,15 @@ var manipulate = function(e, params, action) {
 
 DocumentNodeElement.prototype = new DocumentElement();
 
 
 DocumentNodeElement.prototype = new DocumentElement();
 
+var addParts = function(parts, parent) {
+    var parentChildren = parent.contents();
+    if(parentChildren.length > 2 && parentChildren[0].nodeType === Node.TEXT_NODE && parentChildren[1].nodeType == Node.TEXT_NODE) {
+        $(parentChildren[0]).after(parts);
+    } else {
+        parent.prepend(parts);
+    }
+}
+
 $.extend(DocumentNodeElement.prototype, {
     data: function() {
         var dom = this.dom(),
 $.extend(DocumentNodeElement.prototype, {
     data: function() {
         var dom = this.dom(),
@@ -156,9 +165,9 @@ $.extend(DocumentNodeElement.prototype, {
             return dom.removeData(args[1]);
         return dom.data.apply(dom, arguments);
     },
             return dom.removeData(args[1]);
         return dom.data.apply(dom, arguments);
     },
-    toXML: function(level) {
-        var node = $('<' + this.getWlxmlTag() + '>'),
-            toret = $('<div>');
+    toXML: function(parent, level) {
+        var node = $('<' + this.getWlxmlTag() + '>');
+
         if(this.getWlxmlClass())
             node.attr('class', this.getWlxmlClass());
         var meta = this.getWlxmlMetaAttrs();
         if(this.getWlxmlClass())
             node.attr('class', this.getWlxmlClass());
         var meta = this.getWlxmlMetaAttrs();
@@ -170,26 +179,37 @@ $.extend(DocumentNodeElement.prototype, {
             node.attr(key, this.data('other-attrs')[key]);
         }, this);
 
             node.attr(key, this.data('other-attrs')[key]);
         }, this);
 
-        if(this.data('orig-before') && this.data('orig-before').length) {
-            this.data('orig-before').forEach(function(toAdd) {
-                if(toAdd)
-                    toret.prepend(document.createTextNode(toAdd));
-            });
-        } else if(level && this.getWlxmlTag() !== 'span') {
-            toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
-        }
-        if(this.data('orig-append') && this.data('orig-append').length) {
-            this.data('orig-append').forEach(function(toAdd) {
-                if(toAdd)
-                    node.prepend(toAdd);
-            });
-            //toret = toret.prepend(document.createTextNode(this.data('orig-prepend')));
-        } else if(this.getWlxmlTag() !== 'span'){
-            node.append('\n' + (new Array(level * 2 + 1)).join(' '));
+        var addFormatting = function() {
+            var toret = $('<div>');
+
+            if(this.data('orig-before') && this.data('orig-before').length) {
+                this.data('orig-before').forEach(function(toAdd) {
+                    if(toAdd)
+                        toret.prepend(document.createTextNode(toAdd));
+                });
+            } else if(level && this.getWlxmlTag() !== 'span') {
+                toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
+            }
+            if(this.data('orig-append') && this.data('orig-append').length) {
+                this.data('orig-append').forEach(function(toAdd) {
+                    if(toAdd)
+                        node.prepend(toAdd);
+                });
+            } else if(this.getWlxmlTag() !== 'span'){
+                node.append('\n' + (new Array(level * 2 + 1)).join(' '));
+            }
+            toret.append(node);
+            return toret.contents();
+        }.bind(this);
+
+        var parts = addFormatting(node);
+        
+        var children = this.children();
+        for(var i = children.length - 1; i >= 0; i--) {
+            children[i].toXML(node, level + 1);
         }
 
         }
 
-        toret.append(node);
-        return toret.contents();
+        addParts(parts, parent);
     },
     append: function(params) {
         if(params.tag !== 'span')
     },
     append: function(params) {
         if(params.tag !== 'span')
@@ -305,8 +325,8 @@ $.extend(DocumentTextElement, {
 DocumentTextElement.prototype = new DocumentElement();
 
 $.extend(DocumentTextElement.prototype, {
 DocumentTextElement.prototype = new DocumentElement();
 
 $.extend(DocumentTextElement.prototype, {
-    toXML: function() {
-        return this.getText();
+    toXML: function(parent) {
+        addParts(this.getText(), parent);
     },
     _setupDOMHandler: function(htmlElement) {
         var $element = $(htmlElement);
     },
     _setupDOMHandler: function(htmlElement) {
         var $element = $(htmlElement);