smartxml: refactor inserting methods
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 19 Nov 2013 09:09:52 +0000 (10:09 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 19 Nov 2013 09:09:52 +0000 (10:09 +0100)
src/smartxml/smartxml.js

index ffbe4c2..a357977 100644 (file)
@@ -11,6 +11,16 @@ define([
 var TEXT_NODE = Node.TEXT_NODE;
 
 
 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');
 var DocumentNode = function(nativeNode, document) {
     if(!document) {
         throw new Error('undefined document for a node');
@@ -54,19 +64,13 @@ $.extend(DocumentNode.prototype, {
         return parents;
     },
 
         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);
 
     wrapWith: function(node) {
         node = node instanceof ElementNode ? node : this.document.createElementNode(node);
@@ -185,19 +189,13 @@ $.extend(ElementNode.prototype, {
         return toret;
     },
 
         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();
 
     unwrapContent: function() {
         var parent = this.parent();