smartxml: Getting rid of nodeMoved event in favor of `move` flag on nodeDetached...
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 9 May 2014 10:41:04 +0000 (12:41 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 27 May 2014 14:18:08 +0000 (16:18 +0200)
Moving nodes is now expressed in terms of detaching and adding events.

This simplifies implementation and api a little bit and makes it easier
for a client code to handle "move events".

src/smartxml/core.js
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index 3fde430..007468f 100644 (file)
@@ -24,7 +24,10 @@ var INSERTION = function(implementation) {
         }
         returned = implementation.call(this, insertion.ofNode);
         if(!options.silent && returned.sameNode(insertion.ofNode)) {
         }
         returned = implementation.call(this, insertion.ofNode);
         if(!options.silent && returned.sameNode(insertion.ofNode)) {
-            this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent, nodeWasContained);
+            if(!insertion.insertsNew) {
+                this.triggerChangeEvent('nodeDetached', {node: insertion.ofNode, parent: nodeParent, move: true});
+            }
+            this.triggerChangeEvent('nodeAdded', {node: insertion.ofNode, move: !insertion.insertsNew}, nodeParent, nodeWasContained);
         }
         return returned;
     };
         }
         return returned;
     };
index f9f78e8..cfae7c7 100644 (file)
@@ -140,12 +140,9 @@ $.extend(DocumentNode.prototype, {
         var node = (metaData && metaData.node) ? metaData.node : this,
             event = new events.ChangeEvent(type, $.extend({node: node}, metaData || {}));
         if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) {
         var node = (metaData && metaData.node) ? metaData.node : this,
             event = new events.ChangeEvent(type, $.extend({node: node}, metaData || {}));
         if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) {
-            if(type === 'nodeMoved') {
-                event.meta.parent = origParent;
-            }
             this.document.trigger('change', event);
         }
             this.document.trigger('change', event);
         }
-        if((type === 'nodeAdded' || type === 'nodeMoved') && !this.document.containsNode(this) && nodeWasContained) {
+        if(type === 'nodeAdded' && !this.document.containsNode(this) && nodeWasContained) {
              event = new events.ChangeEvent('nodeDetached', {node: node, parent: origParent});
              this.document.trigger('change', event);
         }
              event = new events.ChangeEvent('nodeDetached', {node: node, parent: origParent});
              this.document.trigger('change', event);
         }
index 7fc69a3..5430278 100644 (file)
@@ -957,7 +957,7 @@ describe('smartxml', function() {
             expect(event.meta.node.sameNode(appended)).to.be.true;
         });
         
             expect(event.meta.node.sameNode(appended)).to.be.true;
         });
         
-        it('emits nodeMoved when appending aready existing node', function() {
+        it('emits nodeDetached/nodeAdded events with `move` flag when appending aready existing node', function() {
             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
                 a = node.contents()[0],
                 b = node.contents()[1],
             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
                 a = node.contents()[0],
                 b = node.contents()[1],
@@ -965,12 +965,17 @@ describe('smartxml', function() {
             node.document.on('change', spy);
             
             var appended = a.append(b),
             node.document.on('change', spy);
             
             var appended = a.append(b),
-                event = spy.args[0][0];
+                detachedEvent = spy.args[0][0],
+                addedEvent = spy.args[1][0];
+
+            expect(spy.callCount).to.equal(2);
+            expect(detachedEvent.type).to.equal('nodeDetached');
+            expect(detachedEvent.meta.node.sameNode(appended)).to.be.true;
+            expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
+            expect(addedEvent.type).to.equal('nodeAdded');
+            expect(addedEvent.meta.node.sameNode(appended)).to.be.true;
+            expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
 
 
-            expect(spy.callCount).to.equal(1);
-            expect(event.type).to.equal('nodeMoved');
-            expect(event.meta.node.sameNode(appended)).to.be.true;
-            expect(node.document.root.sameNode(event.meta.parent)).to.equal(true, 'previous parent attached to event meta');
         });
         
         it('emits nodeAdded event when prepending new node', function() {
         });
         
         it('emits nodeAdded event when prepending new node', function() {
@@ -984,18 +989,24 @@ describe('smartxml', function() {
             expect(event.meta.node.sameNode(prepended)).to.be.true;
         });
         
             expect(event.meta.node.sameNode(prepended)).to.be.true;
         });
         
-        it('emits nodeMoved when prepending aready existing node', function() {
+        it('emits nodeDetached/nodeAdded events with `move` flag when prepending aready existing node', function() {
             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
                 a = node.contents()[0],
                 b = node.contents()[1],
                 spy = sinon.spy();
             node.document.on('change', spy);
             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
                 a = node.contents()[0],
                 b = node.contents()[1],
                 spy = sinon.spy();
             node.document.on('change', spy);
-            
+
             var prepended = a.prepend(b),
             var prepended = a.prepend(b),
-                event = spy.args[0][0];
-            expect(spy.callCount).to.equal(1);
-            expect(event.type).to.equal('nodeMoved');
-            expect(event.meta.node.sameNode(prepended)).to.be.true;
+                detachedEvent = spy.args[0][0],
+                addedEvent = spy.args[1][0];
+
+            expect(spy.callCount).to.equal(2);
+            expect(detachedEvent.type).to.equal('nodeDetached');
+            expect(detachedEvent.meta.node.sameNode(prepended)).to.be.true;
+            expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
+            expect(addedEvent.type).to.equal('nodeAdded');
+            expect(addedEvent.meta.node.sameNode(prepended)).to.be.true;
+            expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
         });
         
         it('emits nodeAdded event when inserting node after another', function() {
         });
         
         it('emits nodeAdded event when inserting node after another', function() {
@@ -1009,18 +1020,23 @@ describe('smartxml', function() {
             expect(event.meta.node.sameNode(inserted)).to.be.true;
         });
         
             expect(event.meta.node.sameNode(inserted)).to.be.true;
         });
         
-        it('emits nodeMoved when inserting aready existing node after another', function() {
+        it('emits nodeDetached/nodeAdded events with `move` flag when inserting aready existing node after another', function() {
             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
                 a = node.contents()[0],
                 b = node.contents()[1],
                 spy = sinon.spy();
             node.document.on('change', spy);
             var inserted = b.after(a),
             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
                 a = node.contents()[0],
                 b = node.contents()[1],
                 spy = sinon.spy();
             node.document.on('change', spy);
             var inserted = b.after(a),
-                event = spy.args[0][0];
+                detachedEvent = spy.args[0][0],
+                addedEvent = spy.args[1][0];
 
 
-            expect(spy.callCount).to.equal(1);
-            expect(event.type).to.equal('nodeMoved');
-            expect(event.meta.node.sameNode(inserted)).to.be.true;
+            expect(spy.callCount).to.equal(2);
+            expect(detachedEvent.type).to.equal('nodeDetached');
+            expect(detachedEvent.meta.node.sameNode(inserted)).to.be.true;
+            expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
+            expect(addedEvent.type).to.equal('nodeAdded');
+            expect(addedEvent.meta.node.sameNode(inserted)).to.be.true;
+            expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
         });
 
         it('emits nodeAdded event when inserting node before another', function() {
         });
 
         it('emits nodeAdded event when inserting node before another', function() {
@@ -1034,18 +1050,23 @@ describe('smartxml', function() {
             expect(event.meta.node.sameNode(inserted)).to.be.true;
         });
         
             expect(event.meta.node.sameNode(inserted)).to.be.true;
         });
         
-        it('emits nodeAdded when inserting aready existing node before another', function() {
+        it('emits nodeDetached/nodeAdded events with `move` flag when inserting aready existing node before another', function() {
             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
                 a = node.contents()[0],
                 b = node.contents()[1],
                 spy = sinon.spy();
             node.document.on('change', spy);
             var inserted = a.before(b),
             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
                 a = node.contents()[0],
                 b = node.contents()[1],
                 spy = sinon.spy();
             node.document.on('change', spy);
             var inserted = a.before(b),
-                event = spy.args[0][0];
+                detachedEvent = spy.args[0][0],
+                addedEvent = spy.args[1][0];
 
 
-            expect(spy.callCount).to.equal(1);
-            expect(event.type).to.equal('nodeMoved');
-            expect(event.meta.node.sameNode(inserted)).to.be.true;
+            expect(spy.callCount).to.equal(2);
+            expect(detachedEvent.type).to.equal('nodeDetached');
+            expect(detachedEvent.meta.node.sameNode(inserted)).to.be.true;
+            expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
+            expect(addedEvent.type).to.equal('nodeAdded');
+            expect(addedEvent.meta.node.sameNode(inserted)).to.be.true;
+            expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
         });
 
         it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
         });
 
         it('emits nodeDetached and nodeAdded when replacing root node with another', function() {