From: Aleksander Ɓukasz Date: Sat, 13 Jul 2013 18:30:35 +0000 (+0200) Subject: refactoring X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/d0db6d70e04af12eeac7275d6140b53df001aa1e refactoring --- diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index e6b8135..9506049 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -72,7 +72,7 @@ $.extend(Canvas.prototype, { doc: function() { if(this.d === null) return null; - return documentElement.wrap(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')}; + return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')}; }, wrapText: function(params) { @@ -117,7 +117,7 @@ $.extend(Canvas.prototype, { }, getDocumentElement: function(from) { if(from instanceof HTMLElement || from instanceof Text) { - return documentElement.wrap(from, this); + return documentElement.DocumentElement.fromHTMLElement(from, this); } }, getCursor: function() { diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 937e9d9..49fcd22 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -21,7 +21,7 @@ $.extend(DocumentElement.prototype, { parent: function() { var parents = this.$element.parents('[wlxml-tag]'); if(parents.length) - return documentElementFromHTMLElement(parents[0], this.canvas); + return DocumentElement.fromHTMLElement(parents[0], this.canvas); return null; }, @@ -85,7 +85,7 @@ $.extend(DocumentNodeElement.prototype, { var elementContent = this.dom().contents(); var element = this; elementContent.each(function(idx) { - var childElement = documentElementFromHTMLElement(this, element.canvas); + var childElement = DocumentElement.fromHTMLElement(this, element.canvas); if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '') return true; if(idx > 0 && childElement instanceof DocumentTextElement) { @@ -143,6 +143,13 @@ DocumentElement.createDOM = function(params) { 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) @@ -155,11 +162,19 @@ DocumentTextElement.createDOM = function(params) { }; DocumentNodeElement.create = function(params, canvas) { - return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]); + return this.fromHTMLElement(this.createDOM(params)[0]); }; DocumentTextElement.create = function(params, canvas) { - return documentElementFromHTMLElement(DocumentTextElement.createDOM(params)[0]); + return this.fromHTMLElement(this.createDOM(params)[0]); +}; + +DocumentNodeElement.fromHTMLElement = function(htmlElement, canvas) { + return new this(htmlElement, canvas); +}; + +DocumentTextElement.fromHTMLElement = function(htmlElement, canvas) { + return new this(htmlElement, canvas); }; @@ -268,19 +283,7 @@ $.extend(DocumentTextElement.prototype, { } }); -var documentElementFromHTMLElement = function(htmlElement, canvas) { - // if(!canvas) - // throw 'no canvas specified'; - if(htmlElement.nodeType === Node.ELEMENT_NODE) - return new DocumentNodeElement(htmlElement, canvas); - if(htmlElement.nodeType === Node.TEXT_NODE) - return new DocumentTextElement(htmlElement, canvas); -}; - return { - wrap: function(htmlElement, canvas) { - return documentElementFromHTMLElement(htmlElement, canvas); - }, DocumentElement: DocumentElement, DocumentNodeElement: DocumentNodeElement, DocumentTextElement: DocumentTextElement