X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/8b83f8a0f1562b8365ccc7735a6301eba613d28c..b8fd04203458b16750c8494f5a00851421d251ce:/src/editor/modules/documentCanvas/canvas/documentElement.js?ds=sidebyside diff --git a/src/editor/modules/documentCanvas/canvas/documentElement.js b/src/editor/modules/documentCanvas/canvas/documentElement.js index 7f448ba..f4505b0 100644 --- a/src/editor/modules/documentCanvas/canvas/documentElement.js +++ b/src/editor/modules/documentCanvas/canvas/documentElement.js @@ -10,58 +10,28 @@ define([ // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas -var DocumentElement = function(htmlElement, canvas) { +var DocumentElement = function(wlxmlNode, canvas) { if(arguments.length === 0) { return; } + this.wlxmlNode = wlxmlNode; this.canvas = canvas; - this._setupDOMHandler(htmlElement); -}; - -var elementTypeFromWlxmlNode = function(wlxmlNode) { - return wlxmlNode.nodeType === Node.TEXT_NODE ? DocumentTextElement : DocumentNodeElement; + this.$element = this.createDOM(); + this.$element.data('canvas-element', this); }; -$.extend(DocumentElement, { - 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) { - return DocumentNodeElement.fromHTMLElement(htmlElement, canvas); - } - 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; - } -}); - $.extend(DocumentElement.prototype, { - _setupDOMHandler: function(htmlElement) { - this.$element = $(htmlElement); - }, bound: function() { return $.contains(document.documentElement, this.dom()[0]); }, 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) { - return DocumentElement.fromHTMLElement(parents[0], this.canvas); + return this.canvas.getDocumentElement(parents[0]); } return null; }, @@ -128,29 +98,37 @@ $.extend(DocumentElement.prototype, { }, exec: function(method) { - var manager = this.data('_wlxmlManager'); - if(manager[method]) { - return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1)); + if(this.manager && this.manager[method]) { + return this.manager[method].apply(this.manager, Array.prototype.slice.call(arguments, 1)); } } }); // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas -var DocumentNodeElement = function(htmlElement, canvas) { - DocumentElement.call(this, htmlElement, canvas); +var DocumentNodeElement = function(wlxmlNode, canvas) { + DocumentElement.call(this, wlxmlNode, canvas); + wlxmlNode.setData('canvasElement', this); }; -$.extend(DocumentNodeElement, { - create: function(wlxmlNode, canvas) { - return this.fromHTMLElement(this.createDOM(wlxmlNode, canvas)[0], canvas); - }, - fromHTMLElement: function(htmlElement, canvas) { - return new this(htmlElement, canvas); - }, +var manipulate = function(e, params, action) { + var element; + if(params instanceof DocumentElement) { + element = params; + } else { + element = e.canvas.createElement(params); + } + var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom(); + target[action](element.dom()); + return element; +}; - createDOM: function(wlxmlNode, canvas) { +DocumentNodeElement.prototype = new DocumentElement(); + + +$.extend(DocumentNodeElement.prototype, { + createDOM: function() { var dom = $('