X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/35abf5d5c78b517329e313b1158f8b980940acd6..428f22c06a5a72b9f036429f84eec9e1deea1c57:/modules/documentCanvas/canvas/documentElement.js diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index cd94759..aca727a 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -38,6 +38,23 @@ $.extend(DocumentElement.prototype, { sameNode: function(other) { return other && (typeof other === typeof this) && other.$element[0] === this.$element[0]; + }, + + wrapWithNodeElement: function(wlxmlNode) { + this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]); + return documentElementFromHTMLElement(this.$element.parent().get(0)); + }, + + childIndex: function(child) { + var children = this.children(), + toret = null; + children.forEach(function(c, idx) { + if(c.sameNode(child)) { + toret = idx; + return false; + } + }); + return toret; } }); @@ -52,12 +69,57 @@ var DocumentTextElement = function(htmlElement) { DocumentNodeElement.prototype = new DocumentElement(); DocumentTextElement.prototype = new DocumentElement(); +var manipulate = function(e, params, action) { + var dom; + if(params instanceof DocumentElement) { + dom = params.dom() + } else { + dom = DocumentNodeElement.createDOM(params); + } + e.$element[action](dom); + return documentElementFromHTMLElement(dom); +}; + +$.extend(DocumentNodeElement.prototype, { + append: function(params) { + manipulate(this, params, 'append'); + }, + before: function(params) { + manipulate(this, params, 'before'); + + }, + after: function(params) { + manipulate(this, params, 'after'); + } +}); + +DocumentNodeElement.createDOM = function(params) { + var dom; + if(params.text) { + dom = $(document.createTextNode(params.text)); + } else { + dom = $('<' + params.tag + '>'); + if(params.klass) + dom.attr('class', params.klass); + } + return dom; +}; + +$.extend(DocumentTextElement.prototype, { + setText: function(text) { + this.$element[0].data = text; + }, + getText: function() { + return this.$element.text(); + } +}); + var documentElementFromHTMLElement = function(htmlElement) { if(htmlElement.nodeType === Node.ELEMENT_NODE) return new DocumentNodeElement(htmlElement); if(htmlElement.nodeType === Node.TEXT_NODE) return new DocumentTextElement(htmlElement); -} +}; return { wrap: function(htmlElement) {