@@ -51,6 +51,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
51
51
renderCustomNodeElement : undefined ,
52
52
enableLegacyTransitions : false ,
53
53
hasInteractiveNodes : false ,
54
+ dimensions : undefined ,
54
55
} ;
55
56
56
57
state : TreeState = {
@@ -151,7 +152,12 @@ class Tree extends React.Component<TreeProps, TreeState> {
151
152
. scaleExtent ( zoomable ? [ scaleExtent . min , scaleExtent . max ] : [ zoom , zoom ] )
152
153
// TODO: break this out into a separate zoom handler fn, rather than inlining it.
153
154
. filter ( ( ) => {
154
- if ( hasInteractiveNodes ) return event . target . classList . contains ( this . svgInstanceRef ) || event . target . classList . contains ( this . gInstanceRef ) || event . shiftKey ;
155
+ if ( hasInteractiveNodes )
156
+ return (
157
+ event . target . classList . contains ( this . svgInstanceRef ) ||
158
+ event . target . classList . contains ( this . gInstanceRef ) ||
159
+ event . shiftKey
160
+ ) ;
155
161
return true ;
156
162
} )
157
163
. on ( 'zoom' , ( ) => {
@@ -377,6 +383,43 @@ class Tree extends React.Component<TreeProps, TreeState> {
377
383
}
378
384
} ;
379
385
386
+ /**
387
+ * Takes a hierarchy point node and centers the node on the screen
388
+ * if the dimensions parameter is passed to the tree.
389
+ *
390
+ * This code is adapted from Rob Schmuecker's centerNode method.
391
+ * Link: http://www.robschmuecker.com/d3-js-drag-and-drop-zoomable-tree/
392
+ *
393
+ * @param hierarchyPointNode
394
+ */
395
+ centerNode = ( hierarchyPointNode : HierarchyPointNode < TreeNodeDatum > ) => {
396
+ // if the dimensions are given
397
+ if ( this . props . dimensions ) {
398
+ const g = select ( `.${ this . gInstanceRef } ` ) ;
399
+ const svg = select ( `.${ this . svgInstanceRef } ` ) ;
400
+ const scale = this . state . d3 . scale ;
401
+
402
+ let x , y ;
403
+ // if the orientation is horizontal, calculate the variables inverted (x->y, y->x)
404
+ if ( this . props . orientation === 'horizontal' ) {
405
+ y = - hierarchyPointNode . x * scale + this . props . dimensions . width / 2 ;
406
+ x = - hierarchyPointNode . y * scale + this . props . dimensions . height / 2 ;
407
+ } else {
408
+ // else, calculate the variables normally (x->x, y->y)
409
+ x = - hierarchyPointNode . x * scale + this . props . dimensions . width / 2 ;
410
+ y = - hierarchyPointNode . y * scale + this . props . dimensions . height / 2 ;
411
+ }
412
+ //@ts -ignore
413
+ g . transition ( )
414
+ . duration ( 800 )
415
+ . attr ( 'transform' , 'translate(' + x + ',' + y + ')scale(' + scale + ')' ) ;
416
+ // Sets the viewport to the new center so that it does not jump back to original
417
+ // coordinates when dragged/zoomed
418
+ //@ts -ignore
419
+ svg . call ( d3zoom ( ) . transform , zoomIdentity . translate ( x , y ) . scale ( this . props . zoom ) ) ;
420
+ }
421
+ } ;
422
+
380
423
/**
381
424
* Generates tree elements (`nodes` and `links`) by
382
425
* grabbing the rootNode from `this.state.data[0]`.
@@ -525,6 +568,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
525
568
onNodeMouseOver = { this . handleOnNodeMouseOverCb }
526
569
onNodeMouseOut = { this . handleOnNodeMouseOutCb }
527
570
subscriptions = { subscriptions }
571
+ centerNode = { this . centerNode }
528
572
/>
529
573
) ;
530
574
} ) }
0 commit comments