X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/428f22c06a5a72b9f036429f84eec9e1deea1c57..d6174a70be823dd5772d845b83e11d0b779ab513:/modules/documentCanvas/canvas/documentElement.js diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index aca727a..94b8422 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -1,130 +1,424 @@ define([ -'libs/jquery-1.9.1.min' -], function($) { +'libs/jquery-1.9.1.min', +'libs/underscore-min', +'modules/documentCanvas/classAttributes' +], function($, _, classAttributes) { 'use strict'; -// DocumentElement represents a node from WLXML document rendered inside Canvas -var DocumentElement = function(htmlElement) { + +// 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.$element = $(htmlElement); - this.wlxmlTag = this.$element.prop('tagName'); + this.canvas = canvas; + this._setupDOMHandler(htmlElement); +} + +var elementTypeFromParams = function(params) { + return params.text !== undefined ? DocumentTextElement : DocumentNodeElement; + }; -$.extend(DocumentElement.prototype, { - children: function() { - var toret = []; - if(this instanceof DocumentTextElement) - return toret; +$.extend(DocumentElement, { + create: function(params, canvas) { + return elementTypeFromParams(params).create(params); + }, + createDOM: function(params) { + return elementTypeFromParams(params).createDOM(params); + }, - var elementContent = this.$element.contents(); - elementContent.each(function(idx) { - var element = documentElementFromHTMLElement(this); - if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (element instanceof DocumentTextElement) && $.trim($(this).text()) === '') - return true; - if(idx > 0 && element instanceof DocumentTextElement) { - if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '') - return true; - } - toret.push(element); - }); - 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]); + 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) { - this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]); - return documentElementFromHTMLElement(this.$element.parent().get(0)); + var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass}); + 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; + }, + + isVisible: function() { + return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata'; } }); -var DocumentNodeElement = function(htmlElement) { - DocumentElement.call(this, htmlElement); -}; -var DocumentTextElement = function(htmlElement) { - DocumentElement.call(this, htmlElement); +// DocumentNodeElement represents an element node from WLXML document rendered inside Canvas +var DocumentNodeElement = function(htmlElement, canvas) { + DocumentElement.call(this, htmlElement, canvas); }; -DocumentNodeElement.prototype = new DocumentElement(); -DocumentTextElement.prototype = new DocumentElement(); +$.extend(DocumentNodeElement, { + createDOM: function(params) { + var dom = $('