X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/13e2b1378fc439299f211939abeb07c732c91566..d6174a70be823dd5772d845b83e11d0b779ab513:/modules/documentCanvas/canvas/documentElement.js?ds=inline diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index e805b35..94b8422 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -1,189 +1,347 @@ define([ 'libs/jquery-1.9.1.min', -'libs/underscore-min' -], function($, _) { +'libs/underscore-min', +'modules/documentCanvas/classAttributes' +], function($, _, classAttributes) { '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; this.canvas = canvas; - this.$element = $(htmlElement); - - this.wlxmlTag = this.$element.attr('wlxml-tag'); + this._setupDOMHandler(htmlElement); } -$.extend(DocumentElement.prototype, { - dom: function() { - return this.$element; +var elementTypeFromParams = function(params) { + return params.text !== undefined ? DocumentTextElement : DocumentNodeElement; + +}; + +$.extend(DocumentElement, { + create: function(params, canvas) { + return elementTypeFromParams(params).create(params); }, - children: function() { - var toret = []; - if(this instanceof DocumentTextElement) - return toret; + createDOM: function(params) { + return elementTypeFromParams(params).createDOM(params); + }, - var elementContent = this.$element.contents(); - var element = this; - elementContent.each(function(idx) { - var childElement = documentElementFromHTMLElement(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) { - if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '') - return true; - } - toret.push(childElement); - }); - return toret; + fromHTMLElement: function(htmlElement, canvas) { + var $element = $(htmlElement); + if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('wlxml-tag')) + return DocumentNodeElement.fromHTMLElement(htmlElement, canvas); + if($element.attr('wlxml-text') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('wlxml-text') !== undefined)) + return DocumentTextElement.fromHTMLElement(htmlElement, canvas); + return undefined; + } +}); + +$.extend(DocumentElement.prototype, { + _setupDOMHandler: function(htmlElement) { + this.$element = $(htmlElement); + }, + dom: function() { + return this.$element; }, parent: function() { - return documentElementFromHTMLElement(this.$element.parent()[0], this.canvas); + var parents = this.$element.parents('[wlxml-tag]'); + if(parents.length) + return DocumentElement.fromHTMLElement(parents[0], this.canvas); + return null; + }, + + parents: function() { + var parents = [], + parent = this.parent(); + while(parent) { + parents.push(parent); + parent = parent.parent(); + } + return parents; }, sameNode: function(other) { - return other && (typeof other === typeof this) && other.$element[0] === this.$element[0]; + return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0]; }, wrapWithNodeElement: function(wlxmlNode) { var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass}); - this.$element.replaceWith(wrapper.dom()); + this.dom().replaceWith(wrapper.dom()); wrapper.append(this); return wrapper; }, - childIndex: function(child) { - var children = this.children(), - toret = null; - children.forEach(function(c, idx) { - if(c.sameNode(child)) { - toret = idx; - return false; + detach: function() { + this.dom().detach(); + this.canvas = null; + }, + + markAsCurrent: function() { + this.canvas.markAsCurrent(this); + }, + + getVerticallyFirstTextElement: function() { + var toret; + this.children().some(function(child) { + if(!child.isVisible()) + return false; // continue + if(child instanceof DocumentTextElement) { + toret = child; + return true; // break + } else { + toret = child.getVerticallyFirstTextElement(); + if(toret) + return true; // break } }); return toret; }, - detach: function() { - this.$element.detach(); - this.canvas = null; + isVisible: function() { + return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata'; } }); +// 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 = $('