X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/5281c07b13ea63c96602a232e61332fb41ca0779..02ea7eed98a6826504c4472a9309010277026a03:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 28a88ee..b469068 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -40,15 +40,17 @@ $.extend(DocumentNode.prototype, { }, after: function(node) { - node = node instanceof ElementNode ? node : this.document.createElementNode(node); - this._$.after(node.nativeNode); - return node; + var insertion = this.getNodeInsertion(node); + this._$.after(insertion.ofNode.nativeNode); + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); + return insertion.ofNode; }, before: function(node) { - node = node instanceof ElementNode ? node : this.document.createElementNode(node); - this._$.before(node.nativeNode); - return node; + var insertion = this.getNodeInsertion(node); + this._$.before(insertion.ofNode.nativeNode); + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); + return insertion.ofNode; }, wrapWith: function(node) { @@ -65,6 +67,18 @@ $.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; + } }); var ElementNode = function(nativeNode, document) { @@ -153,13 +167,17 @@ $.extend(ElementNode.prototype, { }, append: function(node) { - node = node instanceof DocumentNode ? node : this.document.createElementNode(node); - this._$.append(node.nativeNode); + var insertion = this.getNodeInsertion(node); + this._$.append(insertion.ofNode.nativeNode); + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); + return insertion.ofNode; }, prepend: function(node) { - node = node instanceof DocumentNode ? node : this.document.createElementNode(node); - this._$.prepend(node.nativeNode); + var insertion = this.getNodeInsertion(node); + this._$.prepend(insertion.ofNode.nativeNode); + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); + return insertion.ofNode; }, unwrapContent: function() { @@ -257,7 +275,7 @@ $.extend(TextNode.prototype, { textNodeIdx: this.parent().indexOf(this), offsetStart: Math.min(desc.start, desc.end), offsetEnd: Math.max(desc.start, desc.end), - _with: {tag: desc.tagName, attrs: desc.attrs} + _with: {tagName: desc.tagName, attrs: desc.attrs} }); } else { return DocumentNode.prototype.wrapWith.call(this, desc); @@ -316,6 +334,10 @@ $.extend(Document.prototype, Backbone.Events, { return this.root.toXML(); }, + containsNode: function(node) { + return node._$.parents().index(this.root._$) !== -1; + }, + wrapNodes: function(params) { if(!(params.element1.parent().sameNode(params.element2.parent()))) { throw new Error('Wrapping non-sibling nodes not supported.'); @@ -374,7 +396,7 @@ $.extend(Document.prototype, Backbone.Events, { throw new Error('Wrapping text in non-sibling text nodes not supported.'); } - var wrapperElement = this.createElementNode({tagName: params._with.tag, attrs: params._with.attrs}); + var wrapperElement = this.createElementNode({tagName: params._with.tagName, attrs: params._with.attrs}); textNode1.after(wrapperElement); textNode1.detach();