From: Aleksander Ɓukasz Date: Sat, 13 Jul 2013 18:53:40 +0000 (+0200) Subject: refactoring X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/151e78ccc2757396286eb0be3ec3b368ba64b369 refactoring --- diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 49fcd22..6d76bf4 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -6,7 +6,7 @@ define([ 'use strict'; -// DocumentElement represents a node from WLXML document rendered inside Canvas +// DocumentElement represents a text or an element node from WLXML document rendered inside Canvas var DocumentElement = function(htmlElement, canvas) { if(arguments.length === 0) return; @@ -14,6 +14,28 @@ var DocumentElement = function(htmlElement, canvas) { this.$element = $(htmlElement); } +var elementTypeFromParams = function(params) { + return params.text !== undefined ? DocumentTextElement : DocumentNodeElement; + +}; + +$.extend(DocumentElement, { + create: function(params, canvas) { + return elementTypeFromParams(params).create(params); + }, + + createDOM: function(params) { + return elementTypeFromParams(params).createDOM(params); + }, + + fromHTMLElement: function(htmlElement, canvas) { + if(htmlElement.nodeType === Node.ELEMENT_NODE) + return DocumentNodeElement.fromHTMLElement(htmlElement, canvas); + if(htmlElement.nodeType === Node.TEXT_NODE) + return DocumentTextElement.fromHTMLElement(htmlElement, canvas); + } +}); + $.extend(DocumentElement.prototype, { dom: function() { return this.$element; @@ -43,16 +65,27 @@ $.extend(DocumentElement.prototype, { }); +// DocumentNodeElement represents an element node from WLXML document rendered inside Canvas var DocumentNodeElement = function(htmlElement, canvas) { DocumentElement.call(this, htmlElement, canvas); }; -var DocumentTextElement = function(htmlElement, canvas) { - DocumentElement.call(this, htmlElement, canvas); -}; +$.extend(DocumentNodeElement, { + createDOM: function(params) { + var dom = $('
').attr('wlxml-tag', params.tag); + if(params.klass) + dom.attr('wlxml-class', params.klass); + return dom; + }, -DocumentNodeElement.prototype = new DocumentElement(); -DocumentTextElement.prototype = new DocumentElement(); + create: function(params, canvas) { + return this.fromHTMLElement(this.createDOM(params)[0]); + }, + + fromHTMLElement: function(htmlElement, canvas) { + return new this(htmlElement, canvas); + } +}); var manipulate = function(e, params, action) { var element; @@ -65,6 +98,8 @@ var manipulate = function(e, params, action) { return element; }; +DocumentNodeElement.prototype = new DocumentElement(); + $.extend(DocumentNodeElement.prototype, { append: function(params) { manipulate(this, params, 'append'); @@ -133,50 +168,26 @@ $.extend(DocumentNodeElement.prototype, { }); -DocumentElement.create = function(params, canvas) { - var ElementType = params.text !== undefined ? DocumentTextElement : DocumentNodeElement; - return ElementType.create(params); -}; - -DocumentElement.createDOM = function(params) { - var ElementType = params.text !== undefined ? DocumentTextElement : DocumentNodeElement; - return ElementType.createDOM(params); -}; - -DocumentElement.fromHTMLElement = function(htmlElement, canvas) { - if(htmlElement.nodeType === Node.ELEMENT_NODE) - return DocumentNodeElement.fromHTMLElement(htmlElement, canvas); - if(htmlElement.nodeType === Node.TEXT_NODE) - return DocumentTextElement.fromHTMLElement(htmlElement, canvas); -} - -DocumentNodeElement.createDOM = function(params) { - var dom = $('
').attr('wlxml-tag', params.tag); - if(params.klass) - dom.attr('wlxml-class', params.klass); - return dom; -}; - -DocumentTextElement.createDOM = function(params) { - return $(document.createTextNode(params.text)); -}; - -DocumentNodeElement.create = function(params, canvas) { - return this.fromHTMLElement(this.createDOM(params)[0]); +// DocumentNodeElement represents a text node from WLXML document rendered inside Canvas +var DocumentTextElement = function(htmlElement, canvas) { + DocumentElement.call(this, htmlElement, canvas); }; -DocumentTextElement.create = function(params, canvas) { - return this.fromHTMLElement(this.createDOM(params)[0]); -}; +$.extend(DocumentTextElement, { + createDOM: function(params) { + return $(document.createTextNode(params.text)); + }, -DocumentNodeElement.fromHTMLElement = function(htmlElement, canvas) { - return new this(htmlElement, canvas); -}; + create: function(params, canvas) { + return this.fromHTMLElement(this.createDOM(params)[0]); + }, -DocumentTextElement.fromHTMLElement = function(htmlElement, canvas) { - return new this(htmlElement, canvas); -}; + fromHTMLElement: function(htmlElement, canvas) { + return new this(htmlElement, canvas); + } +}); +DocumentTextElement.prototype = new DocumentElement(); $.extend(DocumentTextElement.prototype, { setText: function(text) {