From c833dec45110d8cb85cdf63813a17b1c27516552 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 3 Jul 2014 16:29:10 +0200 Subject: [PATCH 1/1] editor: canvas - first take on hierarchical canvas element registers --- .../modules/documentCanvas/canvas/canvas.js | 20 ++++++++++++++++--- .../documentCanvas/canvas/container.js | 4 ++-- .../documentCanvas/canvas/documentElement.js | 11 +++++++--- .../documentCanvas/canvas/elementsRegister.js | 3 --- .../documentCanvas/canvas/wlxmlListener.js | 2 +- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index d3376e3..3dac88f 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -66,7 +66,7 @@ $.extend(TextHandler.prototype, { 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}, @@ -111,14 +111,28 @@ $.extend(Canvas.prototype, Backbone.Events, { this.reloadRoot(); }, - createElement: function(wlxmlNode) { + createElement: function(wlxmlNode, register, useRoot) { var Factory; + register = register || this.elementsRegister; 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()}); + 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) { diff --git a/src/editor/modules/documentCanvas/canvas/container.js b/src/editor/modules/documentCanvas/canvas/container.js index 6436526..069c78a 100644 --- a/src/editor/modules/documentCanvas/canvas/container.js +++ b/src/editor/modules/documentCanvas/canvas/container.js @@ -14,7 +14,7 @@ var Container = function(nodes, params, element) { 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); } @@ -102,7 +102,7 @@ _.extend(Container.prototype, { 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); diff --git a/src/editor/modules/documentCanvas/canvas/documentElement.js b/src/editor/modules/documentCanvas/canvas/documentElement.js index adeaafe..c0dd361 100644 --- a/src/editor/modules/documentCanvas/canvas/documentElement.js +++ b/src/editor/modules/documentCanvas/canvas/documentElement.js @@ -95,6 +95,7 @@ $.extend(DocumentElement.prototype, { var DocumentNodeElement = function(wlxmlNode, canvas) { DocumentElement.call(this, wlxmlNode, canvas); this.containers = []; + this.elementsRegister = canvas.createElementsRegister(); this.contextMenuActions = []; this.init(this.dom); }; @@ -105,7 +106,7 @@ var manipulate = function(e, params, action) { if(params instanceof DocumentElement) { element = params; } else { - element = e.canvas.createElement(params); + element = e.createElement(params); } if(element.dom) { e.dom[action](element.dom); @@ -147,6 +148,10 @@ $.extend(DocumentNodeElement.prototype, { 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)); }, @@ -297,7 +302,7 @@ $.extend(DocumentTextElement.prototype, { if(params instanceof DocumentNodeElement) { element = params; } else { - element = this.canvas.createElement(params); + element = this.parent().createElement(params); } if(element.dom) { this.dom.wrap('
'); @@ -315,7 +320,7 @@ $.extend(DocumentTextElement.prototype, { if(params instanceof DocumentNodeElement) { element = params; } else { - element = this.canvas.createElement(params); + element = this.createElement(params); } if(element.dom) { this.dom.wrap('
'); diff --git a/src/editor/modules/documentCanvas/canvas/elementsRegister.js b/src/editor/modules/documentCanvas/canvas/elementsRegister.js index 9a44514..7b2cf99 100644 --- a/src/editor/modules/documentCanvas/canvas/elementsRegister.js +++ b/src/editor/modules/documentCanvas/canvas/elementsRegister.js @@ -38,9 +38,6 @@ _.extend(ElementsRegister.prototype, { } }.bind(this)); } - if(!Factory) { - Factory = this.BaseType; - } return Factory; } }); diff --git a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js index d107612..b1a9582 100644 --- a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js +++ b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js @@ -49,7 +49,7 @@ var handlers = { 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); } } -- 2.20.1