editor: canvas - first take on hierarchical canvas element registers
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 3 Jul 2014 14:29:10 +0000 (16:29 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 14 Aug 2014 14:26:13 +0000 (16:26 +0200)
src/editor/modules/documentCanvas/canvas/canvas.js
src/editor/modules/documentCanvas/canvas/container.js
src/editor/modules/documentCanvas/canvas/documentElement.js
src/editor/modules/documentCanvas/canvas/elementsRegister.js
src/editor/modules/documentCanvas/canvas/wlxmlListener.js

index d3376e3..3dac88f 100644 (file)
@@ -66,7 +66,7 @@ $.extend(TextHandler.prototype, {
 var Canvas = function(wlxmlDocument, elements, metadata, sandbox) {
     this.metadata = metadata || {};
     this.sandbox = sandbox;
 var Canvas = function(wlxmlDocument, elements, metadata, sandbox) {
     this.metadata = metadata || {};
     this.sandbox = sandbox;
-    this.elementsRegister = new ElementsRegister(documentElement.DocumentNodeElement, nullElement);
+    this.elementsRegister = this.createElementsRegister();
 
     elements = [
         {tag: 'section', klass: null, prototype: genericElement},
 
     elements = [
         {tag: 'section', klass: null, prototype: genericElement},
@@ -111,14 +111,28 @@ $.extend(Canvas.prototype, Backbone.Events, {
         this.reloadRoot();
     },
 
         this.reloadRoot();
     },
 
-    createElement: function(wlxmlNode) {
+    createElement: function(wlxmlNode, register, useRoot) {
         var Factory;
         var Factory;
+        register = register || this.elementsRegister;
         if(wlxmlNode.nodeType === Node.TEXT_NODE) {
             Factory = documentElement.DocumentTextElement;
         } else {
         if(wlxmlNode.nodeType === Node.TEXT_NODE) {
             Factory = documentElement.DocumentTextElement;
         } else {
+            Factory = register.getElement({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
+        }
+        if(!Factory && useRoot) {
             Factory = this.elementsRegister.getElement({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
             Factory = this.elementsRegister.getElement({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
+            if(!Factory) {
+                Factory = documentElement.DocumentNodeElement;
+            }
+        }
+
+        if(Factory) {
+            return new Factory(wlxmlNode, this);
         }
         }
-        return new Factory(wlxmlNode, this);
+    },
+
+    createElementsRegister: function() {
+        return new ElementsRegister(documentElement.DocumentNodeElement, nullElement);
     },
 
     getDocumentElement: function(htmlElement) {
     },
 
     getDocumentElement: function(htmlElement) {
index 6436526..069c78a 100644 (file)
@@ -14,7 +14,7 @@ var Container = function(nodes, params, element) {
     this.element = element;
 
     nodes.forEach(function(node) {
     this.element = element;
 
     nodes.forEach(function(node) {
-        var el = this.element.canvas.createElement(node);
+        var el = this.element.createElement(node);
         if(el.dom) {
             this.dom.append(el.dom);
         }
         if(el.dom) {
             this.dom.append(el.dom);
         }
@@ -102,7 +102,7 @@ _.extend(Container.prototype, {
         if(param instanceof documentElement.DocumentElement) {
             element = param;
         } else {
         if(param instanceof documentElement.DocumentElement) {
             element = param;
         } else {
-            element = this.element.canvas.createElement(param);//
+            element = this.element.createElement(param);//
         }
         if(element.dom) {
             this.dom.prepend(element.dom);
         }
         if(element.dom) {
             this.dom.prepend(element.dom);
index adeaafe..c0dd361 100644 (file)
@@ -95,6 +95,7 @@ $.extend(DocumentElement.prototype, {
 var DocumentNodeElement = function(wlxmlNode, canvas) {
     DocumentElement.call(this, wlxmlNode, canvas);
     this.containers = [];
 var DocumentNodeElement = function(wlxmlNode, canvas) {
     DocumentElement.call(this, wlxmlNode, canvas);
     this.containers = [];
+    this.elementsRegister = canvas.createElementsRegister();
     this.contextMenuActions = [];
     this.init(this.dom);
 };
     this.contextMenuActions = [];
     this.init(this.dom);
 };
@@ -105,7 +106,7 @@ var manipulate = function(e, params, action) {
     if(params instanceof DocumentElement) {
         element = params;
     } else {
     if(params instanceof DocumentElement) {
         element = params;
     } else {
-        element = e.canvas.createElement(params);
+        element = e.createElement(params);
     }
     if(element.dom) {
         e.dom[action](element.dom);
     }
     if(element.dom) {
         e.dom[action](element.dom);
@@ -147,6 +148,10 @@ $.extend(DocumentNodeElement.prototype, {
             this.containers.splice(idx, 1);
         }
     },
             this.containers.splice(idx, 1);
         }
     },
+    createElement: function(wlxmlNode) {
+        var parent = this.wlxmlNode.parent() ? utils.getElementForNode(this.wlxmlNode.parent()) : null;
+        return this.canvas.createElement(wlxmlNode, this.elementsRegister, !parent) || parent.createElement(wlxmlNode);
+    },
     addToContextMenu: function(actionFqName) {
         this.contextMenuActions.push(this.canvas.createAction(actionFqName));
     },
     addToContextMenu: function(actionFqName) {
         this.contextMenuActions.push(this.canvas.createAction(actionFqName));
     },
@@ -297,7 +302,7 @@ $.extend(DocumentTextElement.prototype, {
         if(params instanceof DocumentNodeElement) {
             element = params;
         } else {
         if(params instanceof DocumentNodeElement) {
             element = params;
         } else {
-            element = this.canvas.createElement(params);
+            element = this.parent().createElement(params);
         }
         if(element.dom) {
             this.dom.wrap('<div>');
         }
         if(element.dom) {
             this.dom.wrap('<div>');
@@ -315,7 +320,7 @@ $.extend(DocumentTextElement.prototype, {
         if(params instanceof DocumentNodeElement) {
             element = params;
         } else {
         if(params instanceof DocumentNodeElement) {
             element = params;
         } else {
-            element = this.canvas.createElement(params);
+            element = this.createElement(params);
         }
         if(element.dom) {
             this.dom.wrap('<div>');
         }
         if(element.dom) {
             this.dom.wrap('<div>');
index 9a44514..7b2cf99 100644 (file)
@@ -38,9 +38,6 @@ _.extend(ElementsRegister.prototype, {
                 }
             }.bind(this));
         }
                 }
             }.bind(this));
         }
-        if(!Factory) {
-            Factory = this.BaseType;
-        }
         return Factory;
     }
 });
         return Factory;
     }
 });
index d107612..b1a9582 100644 (file)
@@ -49,7 +49,7 @@ var handlers = {
                 if(event.meta.node.isRoot()) {
                     this.canvas.reloadRoot();
                 } else {
                 if(event.meta.node.isRoot()) {
                     this.canvas.reloadRoot();
                 } else {
-                    newElement = this.canvas.createElement(event.meta.node);
+                    newElement = element.parent().createElement(event.meta.node);
                     element.dom.replaceWith(newElement.dom);
                 }
             }
                     element.dom.replaceWith(newElement.dom);
                 }
             }