X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/efe36f4f1b5df351eeb4d40a54c3900cf9a7079b..ae032450627538fabf7df31fbda6cf7bd097b94f:/src/editor/modules/documentCanvas/canvas/documentElement.js?ds=inline diff --git a/src/editor/modules/documentCanvas/canvas/documentElement.js b/src/editor/modules/documentCanvas/canvas/documentElement.js index 0627bc1..60e79de 100644 --- a/src/editor/modules/documentCanvas/canvas/documentElement.js +++ b/src/editor/modules/documentCanvas/canvas/documentElement.js @@ -1,43 +1,42 @@ define([ 'libs/jquery', 'libs/underscore', -'modules/documentCanvas/classAttributes', 'modules/documentCanvas/canvas/utils', 'modules/documentCanvas/canvas/widgets', 'modules/documentCanvas/canvas/wlxmlManagers' -], function($, _, classAttributes, utils, widgets, wlxmlManagers) { +], function($, _, utils, widgets, wlxmlManagers) { 'use strict'; +/* global Node:false, document:false */ // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas var DocumentElement = function(htmlElement, canvas) { - if(arguments.length === 0) + if(arguments.length === 0) { return; + } this.canvas = canvas; this._setupDOMHandler(htmlElement); -} +}; -var elementTypeFromParams = function(params) { - return params.text !== undefined ? DocumentTextElement : DocumentNodeElement; +var elementTypeFromWlxmlNode = function(wlxmlNode) { + return wlxmlNode.nodeType === Node.TEXT_NODE ? DocumentTextElement : DocumentNodeElement; }; $.extend(DocumentElement, { - create: function(params, canvas) { - return elementTypeFromParams(params).create(params); - }, - - createDOM: function(params) { - return elementTypeFromParams(params).createDOM(params); + create: function(node, canvas) { + return elementTypeFromWlxmlNode(node).create(node, canvas); }, fromHTMLElement: function(htmlElement, canvas) { var $element = $(htmlElement); - if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined) + if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined) { return DocumentNodeElement.fromHTMLElement(htmlElement, canvas); - if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined)) + } + if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined)) { return DocumentTextElement.fromHTMLElement(htmlElement, canvas); + } return undefined; } }); @@ -52,10 +51,19 @@ $.extend(DocumentElement.prototype, { dom: function() { return this.$element; }, + data: function() { + var dom = this.dom(), + args = Array.prototype.slice.call(arguments, 0); + if(args.length === 2 && args[1] === undefined) { + return dom.removeData(args[0]); + } + return dom.data.apply(dom, arguments); + }, parent: function() { var parents = this.$element.parents('[document-node-element]'); - if(parents.length) + if(parents.length) { return DocumentElement.fromHTMLElement(parents[0], this.canvas); + } return null; }, @@ -73,13 +81,6 @@ $.extend(DocumentElement.prototype, { 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); - this.dom().replaceWith(wrapper.dom()); - wrapper.append(this); - return wrapper; - }, - markAsCurrent: function() { this.canvas.markAsCurrent(this); }, @@ -87,15 +88,17 @@ $.extend(DocumentElement.prototype, { getVerticallyFirstTextElement: function() { var toret; this.children().some(function(child) { - if(!child.isVisible()) + if(!child.isVisible()) { return false; // continue + } if(child instanceof DocumentTextElement) { toret = child; return true; // break } else { toret = child.getVerticallyFirstTextElement(); - if(toret) + if(toret) { return true; // break + } } }); return toret; @@ -127,8 +130,9 @@ $.extend(DocumentElement.prototype, { exec: function(method) { var manager = this.data('_wlxmlManager'); - if(manager[method]) + if(manager[method]) { return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1)); + } } }); @@ -139,7 +143,15 @@ var DocumentNodeElement = function(htmlElement, canvas) { }; $.extend(DocumentNodeElement, { - createDOM: function(params, canvas) { + create: function(wlxmlNode, canvas) { + return this.fromHTMLElement(this.createDOM(wlxmlNode, canvas)[0], canvas); + }, + + fromHTMLElement: function(htmlElement, canvas) { + return new this(htmlElement, canvas); + }, + + createDOM: function(wlxmlNode, canvas) { var dom = $('