Divec has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/377237 )

Change subject: WIP: Notification for DM node attached/detached
......................................................................

WIP: Notification for DM node attached/detached

Change-Id: I2d42f0e3345c49417d2ec8c9d1b291f10175d938
---
M src/dm/ve.dm.Document.js
M src/ve.BranchNode.js
M src/ve.Document.js
M src/ve.Node.js
4 files changed, 76 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/37/377237/1

diff --git a/src/dm/ve.dm.Document.js b/src/dm/ve.dm.Document.js
index 6087c9b..056e5c2 100644
--- a/src/dm/ve.dm.Document.js
+++ b/src/dm/ve.dm.Document.js
@@ -1697,3 +1697,17 @@
 ve.dm.Document.prototype.getDir = function () {
        return this.dir;
 };
+
+/**
+ * @inheritdoc
+ */
+ve.dm.Document.prototype.onNodeAttached = function ( node ) {
+       console.log( 'attached', node );
+};
+
+/**
+ * @inheritdoc
+ */
+ve.dm.Document.prototype.onNodeDetached = function ( node ) {
+       console.log( 'detached', node );
+};
diff --git a/src/ve.BranchNode.js b/src/ve.BranchNode.js
index 477bb4b..3ed2bd9 100644
--- a/src/ve.BranchNode.js
+++ b/src/ve.BranchNode.js
@@ -92,7 +92,7 @@
                return;
        }
        if ( oldRoot ) {
-               // Null the root, then recurse into children, then emit 
root/unroot.
+               // Null the root, then recurse into children, then emit unroot.
                // That way, at emit time, all this node's ancestors and 
descendants have
                // null root.
                this.root = null;
@@ -103,7 +103,7 @@
        }
        this.root = root;
        if ( root ) {
-               // We've set the new root, so recurse into children, then emit 
root/unroot.
+               // We've set the new root, so recurse into children, then emit 
root.
                // That way, at emit time, all this node's ancestors and 
descendants have
                // the new root.
                for ( i = 0, len = this.children.length; i < len; i++ ) {
@@ -121,14 +121,35 @@
  * @param {ve.Document} doc Document this node is a part of
  */
 ve.BranchNode.prototype.setDocument = function ( doc ) {
-       var i;
+       var i, len,
+               oldDoc = this.doc;
        if ( doc === this.doc ) {
                // Nothing to do, don't recurse into all descendants
                return;
        }
+       if ( oldDoc ) {
+               // Null the doc, then recurse into children, then notify the 
doc.
+               // That way, at notify time, all this node's ancestors and 
descendants have
+               // null doc.
+               this.doc = null;
+               for ( i = 0, len = this.children.length; i < len; i++ ) {
+                       this.children[ i ].setDocument( null );
+               }
+               if ( oldDoc.onNodeDetached ) {
+                       oldDoc.onNodeDetached( this );
+               }
+       }
        this.doc = doc;
-       for ( i = 0; i < this.children.length; i++ ) {
-               this.children[ i ].setDocument( doc );
+       if ( doc ) {
+               // We've set the new doc, so recurse into children, then notify 
the doc.
+               // That way, at notify time, all this node's ancestors and 
descendants have
+               // the new root.
+               for ( i = 0, len = this.children.length; i < len; i++ ) {
+                       this.children[ i ].setDocument( doc );
+               }
+               if ( doc.onNodeAttached ) {
+                       doc.onNodeAttached( this );
+               }
        }
 };
 
diff --git a/src/ve.Document.js b/src/ve.Document.js
index 5e909c4..1522813 100644
--- a/src/ve.Document.js
+++ b/src/ve.Document.js
@@ -563,3 +563,23 @@
        var selected = this.selectNodes( range, 'leaves' );
        return selected.length === 1 && selected[ 0 ].nodeRange.containsRange( 
range ) && selected[ 0 ].indexInNode === undefined;
 };
+
+/**
+ * Callback when a node is attached with ve.Node#setDocument
+ *
+ * The node and all its children are guaranteed to be attached
+ *
+ * @method
+ * @param {ve.Node} node The node attached
+ */
+ve.Document.prototype.onNodeAttached = null;
+
+/**
+ * Callback when a node is attached with ve.Node#setDocument
+ *
+ * The node and all its children are guaranteed to be attached
+ *
+ * @method
+ * @param {ve.Node} node The node detached
+ */
+ve.Document.prototype.onNodeDetached = null;
diff --git a/src/ve.Node.js b/src/ve.Node.js
index 55a4031..119f3e2 100644
--- a/src/ve.Node.js
+++ b/src/ve.Node.js
@@ -351,10 +351,25 @@
  * This method is overridden by nodes with children.
  *
  * @method
- * @param {ve.Document} doc Document this node is a part of
+ * @param {ve.Document|null} doc Document this node is a part of
  */
 ve.Node.prototype.setDocument = function ( doc ) {
+       var oldDoc = this.doc;
+       if ( doc === oldDoc ) {
+               return;
+       }
+       if ( oldDoc ) {
+               this.doc = null;
+               if ( oldDoc.onNodeDetached ) {
+                       oldDoc.onNodeDetached( this );
+               }
+       }
        this.doc = doc;
+       if ( doc ) {
+               if ( doc.onNodeAttached ) {
+                       doc.onNodeAttached( this );
+               }
+       }
 };
 
 /**

-- 
To view, visit https://gerrit.wikimedia.org/r/377237
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2d42f0e3345c49417d2ec8c9d1b291f10175d938
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Divec <da...@troi.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to