1
1
import React , { PropTypes } from 'react' ;
2
2
import { TransitionGroup } from 'react-transition-group' ;
3
- import { layout } from 'd3' ;
3
+ import { layout , select , behavior , event } from 'd3' ;
4
4
import clone from 'clone' ;
5
5
import uuid from 'uuid' ;
6
6
@@ -15,7 +15,6 @@ export default class Tree extends React.Component {
15
15
this . state = {
16
16
initialRender : true ,
17
17
data : this . assignInternalProperties ( clone ( props . data ) ) ,
18
- zoom : undefined ,
19
18
} ;
20
19
this . findNodesById = this . findNodesById . bind ( this ) ;
21
20
this . collapseNode = this . collapseNode . bind ( this ) ;
@@ -24,6 +23,7 @@ export default class Tree extends React.Component {
24
23
25
24
26
25
componentDidMount ( ) {
26
+ this . bindZoomListener ( ) ;
27
27
// TODO find better way of setting initialDepth, re-render here is suboptimal
28
28
this . setState ( { initialRender : false } ) ; // eslint-disable-line
29
29
}
@@ -62,20 +62,24 @@ export default class Tree extends React.Component {
62
62
*
63
63
* @return {void }
64
64
*/
65
- // bindZoomListener() {
66
- // const { zoomable, scaleExtent } = this.props;
67
- // const svg = select('.svg');
68
- //
69
- // if (zoomable) {
70
- // this.setState({ zoom: 'scale(1)' });
71
- // svg.call(behavior.zoom()
72
- // .scaleExtent([scaleExtent.min, scaleExtent.max])
73
- // .on('zoom', () => {
74
- // this.setState({ zoom: `scale(${event.scale})` });
75
- // })
76
- // );
77
- // }
78
- // }
65
+ bindZoomListener ( ) {
66
+ const { zoomable, scaleExtent, translate } = this . props ;
67
+ const svg = select ( '.rd3t-svg' ) ;
68
+ const g = select ( '.rd3t-g' ) ;
69
+
70
+ if ( zoomable ) {
71
+ svg . call ( behavior . zoom ( )
72
+ . scaleExtent ( [ scaleExtent . min , scaleExtent . max ] )
73
+ . on ( 'zoom' , ( ) => {
74
+ g . attr ( 'transform' ,
75
+ `translate(${ event . translate } ) scale(${ event . scale } )`
76
+ ) ;
77
+ } )
78
+ // Offset so that first pan and zoom does not jump back to [0,0] coords
79
+ . translate ( [ translate . x , translate . y ] )
80
+ ) ;
81
+ }
82
+ }
79
83
80
84
81
85
/**
@@ -223,6 +227,7 @@ export default class Tree extends React.Component {
223
227
< svg className = "rd3t-svg" width = "100%" height = "100%" >
224
228
< TransitionGroup
225
229
component = "g"
230
+ className = "rd3t-g"
226
231
transform = { `translate(${ translate . x } ,${ translate . y } )` }
227
232
>
228
233
{ nodes . map ( ( nodeData ) =>
@@ -263,8 +268,8 @@ Tree.defaultProps = {
263
268
depthFactor : undefined ,
264
269
collapsible : true ,
265
270
initialDepth : undefined ,
266
- // zoomable: true,
267
- // scaleExtent: { min: 0.1, max: 1 },
271
+ zoomable : true ,
272
+ scaleExtent : { min : 0.1 , max : 1 } ,
268
273
} ;
269
274
270
275
Tree . propTypes = {
@@ -285,9 +290,9 @@ Tree.propTypes = {
285
290
depthFactor : PropTypes . number ,
286
291
collapsible : PropTypes . bool ,
287
292
initialDepth : PropTypes . number ,
288
- // zoomable: PropTypes.bool,
289
- // scaleExtent: PropTypes.shape({
290
- // min: PropTypes.number,
291
- // max: PropTypes.number,
292
- // }),
293
+ zoomable : PropTypes . bool ,
294
+ scaleExtent : PropTypes . shape ( {
295
+ min : PropTypes . number ,
296
+ max : PropTypes . number ,
297
+ } ) ,
293
298
} ;
0 commit comments