integration wip: handle newNodeRequested in a new way
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / documentElement.js
index 0627bc1..cd6b3d8 100644 (file)
@@ -18,18 +18,18 @@ var DocumentElement = function(htmlElement, canvas) {
     this._setupDOMHandler(htmlElement);
 }
 
-var elementTypeFromParams = function(params) {
-    return params.text !== undefined ? DocumentTextElement : DocumentNodeElement;
 
-};
+var elementTypeFromWlxmlNode = function(wlxmlNode) {
+    return wlxmlNode.nodeType === Node.TEXT_NODE ? DocumentTextElement : DocumentNodeElement;
+}
 
 $.extend(DocumentElement, {
-    create: function(params, canvas) {
-        return elementTypeFromParams(params).create(params);
+    create: function(node, canvas) {
+        return elementTypeFromWlxmlNode(node).create(node, canvas);
     },
 
-    createDOM: function(params) {
-        return elementTypeFromParams(params).createDOM(params);
+    createDOM: function(wlxmlNode) {
+        return elementTypeFromParams(wlxmlNode).createDOM(params);
     },
 
     fromHTMLElement: function(htmlElement, canvas) {
@@ -52,6 +52,13 @@ $.extend(DocumentElement.prototype, {
     dom: function() {
         return this.$element;
     },
+    data: function() {
+        var dom = this.dom(),
+            args = Array.prototype.slice.call(arguments, 0);
+        if(args.length === 2 && args[1] === undefined)
+            return dom.removeData(args[0]);
+        return dom.data.apply(dom, arguments);
+    },
     parent: function() {
         var parents = this.$element.parents('[document-node-element]');
         if(parents.length)
@@ -139,7 +146,15 @@ var DocumentNodeElement = function(htmlElement, canvas) {
 };
 
 $.extend(DocumentNodeElement, {
-    createDOM: function(params, canvas) {
+    create: function(wlxmlNode, canvas) {
+        return this.fromHTMLElement(this.createDOM(wlxmlNode, canvas)[0], canvas);
+    },
+
+    fromHTMLElement: function(htmlElement, canvas) {
+        return new this(htmlElement, canvas);
+    },
+
+    createDOM: function(wlxmlNode, canvas) {
         var dom = $('<div>')
                 .attr('document-node-element', ''),
             widgetsContainer = $('<div>')
@@ -154,29 +169,22 @@ $.extend(DocumentNodeElement, {
 
         var element = this.fromHTMLElement(dom[0], canvas);
 
-        element.setWlxml({tag: params.tag, klass: params.klass});
-        if(params.meta) {
-            _.keys(params.meta).forEach(function(key) {
-                element.setWlxmlMetaAttr(key, params.meta[key]);
-            });
-        }
-        element.data('other-attrs', params.others);
+        element.data('wlxmlNode', wlxmlNode);
+        wlxmlNode.setData('canvasElement', element);
 
-        if(params.rawChildren && params.rawChildren.length) {
-            container.append(params.rawChildren);
-        } else if(params.prepopulateOnEmpty) {
-            element.append(DocumentTextElement.create({text: ''}));
-        }
-        return dom;
-    },
+        element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
 
-    create: function(params, canvas) {
-        return this.fromHTMLElement(this.createDOM(params, canvas)[0], canvas);
-    },
+        _.keys(wlxmlNode.getMetaAttributes()).forEach(function(key) {
+            element.setWlxmlMetaAttr(key, params.meta[key]);
+        });
 
-    fromHTMLElement: function(htmlElement, canvas) {
-        return new this(htmlElement, canvas);
+        wlxmlNode.contents().forEach((function(node) {
+            container.append(DocumentElement.create(node).dom());
+        }).bind(this));
+
+        return dom;
     }
+
 });
 
 var manipulate = function(e, params, action) {
@@ -259,13 +267,6 @@ $.extend(DocumentNodeElement.prototype, {
             element2: parent.children()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
         };
     },
-    data: function() {
-        var dom = this.dom(),
-            args = Array.prototype.slice.call(arguments, 0);
-        if(args.length === 2 && args[1] === undefined)
-            return dom.removeData(args[0]);
-        return dom.data.apply(dom, arguments);
-    },
     toXML: function(level) {
         var node = $('<' + this.getWlxmlTag() + '>');
 
@@ -478,14 +479,17 @@ var DocumentTextElement = function(htmlElement, canvas) {
 };
 
 $.extend(DocumentTextElement, {
-    createDOM: function(params) {
-        return $('<div>')
+    createDOM: function(wlxmlTextNode) {
+        var dom = $('<div>')
             .attr('document-text-element', '')
-            .text(params.text || utils.unicode.ZWS);
+            .text(wlxmlTextNode.getText() || utils.unicode.ZWS),
+        element = this.fromHTMLElement(dom[0], this);
+        element.data('wlxmlNode', wlxmlTextNode);
+        return dom;
     },
 
-    create: function(params, canvas) {
-        return this.fromHTMLElement(this.createDOM(params)[0]);
+    create: function(wlxmlTextNode, canvas) {
+        return this.fromHTMLElement(this.createDOM(wlxmlTextNode)[0]);
     },
 
     fromHTMLElement: function(htmlElement, canvas) {