X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/6f88a272f930c30868c43b724368b4bcf6f0a3a7..9ebf1ada325abec0fe83789b04f6a8af576818a4:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index d8b6bf0..a357977 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -11,6 +11,16 @@ define([ var TEXT_NODE = Node.TEXT_NODE; +var INSERTION = function(implementation) { + var toret = function(node) { + var insertion = this.getNodeInsertion(node); + implementation.call(this, insertion.ofNode.nativeNode); + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); + return insertion.ofNode; + }; + return toret; +}; + var DocumentNode = function(nativeNode, document) { if(!document) { throw new Error('undefined document for a node'); @@ -26,27 +36,41 @@ $.extend(DocumentNode.prototype, { this._$ = $(nativeNode); }, - detach: function() { this._$.detach(); }, + detach: function() { + var parent = this.parent(); + this._$.detach(); + this.triggerChangeEvent('nodeDetached', {parent: parent}); + return this; + }, sameNode: function(otherNode) { return otherNode && this.nativeNode === otherNode.nativeNode; }, parent: function() { - return this.nativeNode.parentNode ? this.document.createElementNode(this.nativeNode.parentNode) : null; + var parentNode = this.nativeNode.parentNode; + if(parentNode && parentNode.nodeType === Node.ELEMENT_NODE) { + return this.document.createElementNode(parentNode); + } + return null; }, - after: function(node) { - node = node instanceof ElementNode ? node : this.document.createElementNode(node); - this._$.after(node.nativeNode); - return node; + parents: function() { + var parent = this.parent(), + parents = parent ? parent.parents() : []; + if(parent) { + parents.unshift(parent); + } + return parents; }, - before: function(node) { - node = node instanceof ElementNode ? node : this.document.createElementNode(node); - this._$.before(node.nativeNode); - return node; - }, + after: INSERTION(function(nativeNode) { + return this._$.after(nativeNode); + }), + + before: INSERTION(function(nativeNode) { + return this._$.before(nativeNode); + }), wrapWith: function(node) { node = node instanceof ElementNode ? node : this.document.createElementNode(node); @@ -62,6 +86,22 @@ $.extend(DocumentNode.prototype, { var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {})); this.document.trigger('change', event); }, + + getNodeInsertion: function(node) { + var insertion = {}; + if(node instanceof DocumentNode) { + insertion.ofNode = node; + insertion.insertsNew = !this.document.containsNode(node); + } else { + insertion.ofNode = this.document.createElementNode(node); + insertion.insertsNew = true; + } + return insertion; + }, + + getIndex: function() { + return this.parent().indexOf(this); + } }); var ElementNode = function(nativeNode, document) { @@ -149,10 +189,13 @@ $.extend(ElementNode.prototype, { return toret; }, - append: function(node) { - node = node instanceof DocumentNode ? node : this.document.createElementNode(node); - this._$.append(node.nativeNode); - }, + append: INSERTION(function(nativeNode) { + this._$.append(nativeNode); + }), + + prepend: INSERTION(function(nativeNode) { + this._$.prepend(nativeNode); + }), unwrapContent: function() { var parent = this.parent(); @@ -200,6 +243,10 @@ $.extend(ElementNode.prototype, { }; }, + wrapText: function(params) { + return this.document._wrapText(_.extend({inside: this}, params)); + }, + toXML: function() { var wrapper = $('