From: Aleksander Ɓukasz Date: Tue, 19 Nov 2013 09:09:52 +0000 (+0100) Subject: smartxml: refactor inserting methods X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/9ebf1ada325abec0fe83789b04f6a8af576818a4 smartxml: refactor inserting methods --- diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index ffbe4c2..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'); @@ -54,19 +64,13 @@ $.extend(DocumentNode.prototype, { return parents; }, - after: function(node) { - var insertion = this.getNodeInsertion(node); - this._$.after(insertion.ofNode.nativeNode); - this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); - return insertion.ofNode; - }, + after: INSERTION(function(nativeNode) { + return this._$.after(nativeNode); + }), - before: function(node) { - var insertion = this.getNodeInsertion(node); - this._$.before(insertion.ofNode.nativeNode); - this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); - return insertion.ofNode; - }, + before: INSERTION(function(nativeNode) { + return this._$.before(nativeNode); + }), wrapWith: function(node) { node = node instanceof ElementNode ? node : this.document.createElementNode(node); @@ -185,19 +189,13 @@ $.extend(ElementNode.prototype, { return toret; }, - append: function(node) { - var insertion = this.getNodeInsertion(node); - this._$.append(insertion.ofNode.nativeNode); - this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); - return insertion.ofNode; - }, + append: INSERTION(function(nativeNode) { + this._$.append(nativeNode); + }), - prepend: function(node) { - var insertion = this.getNodeInsertion(node); - this._$.prepend(insertion.ofNode.nativeNode); - this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); - return insertion.ofNode; - }, + prepend: INSERTION(function(nativeNode) { + this._$.prepend(nativeNode); + }), unwrapContent: function() { var parent = this.parent();