@@ -2,6 +2,7 @@ const TreeLayout = (function() {
22
33 // depth of recursion
44 let maxDepth ;
5+ const rh = 50 ; // row height
56
67 // recursively build hierarchy from a root word or link
78 function addNode ( node , depth , source ) {
@@ -57,12 +58,45 @@ const TreeLayout = (function() {
5758 } ;
5859
5960 class TreeLayout {
60- constructor ( el ) {
61+ constructor ( el , mainSVG , openInModal ) {
6162 // container element
62- this . svg = d3 . select ( el ) ;
63+ this . isInModal = openInModal ;
64+ this . svg = openInModal ? d3 . select ( el ) : d3 . select ( document . body ) . append ( 'svg' )
65+ . attr ( 'id' , 'tree-svg' ) ;
6366 this . draggable = this . svg . append ( 'g' ) ;
6467 this . g = this . draggable . append ( 'g' ) ;
6568
69+ let self = this ;
70+ this . svg . append ( 'text' )
71+ . text ( openInModal ? 'Show in main window' : 'Pop into modal' )
72+ . attr ( 'id' , 'tree-popout' )
73+ . attr ( 'x' , 15 )
74+ . attr ( 'y' , 25 )
75+ . on ( 'click' , function ( ) {
76+ let node = document . getElementById ( 'tree-svg' ) ;
77+ let parent = node . parentNode ;
78+ if ( parent === document . body ) {
79+ d3 . select ( this ) . text ( 'Show in main window' ) ;
80+ d3 . select ( el ) . node ( ) . appendChild ( node ) ;
81+ self . isInModal = true ;
82+ }
83+ else {
84+ d3 . select ( this ) . text ( 'Pop into modal' ) ;
85+ document . body . appendChild ( node ) ;
86+ self . isInModal = false ;
87+ }
88+ mainSVG . fire ( 'build-tree' ) ;
89+ } ) ;
90+ this . svg . append ( 'text' )
91+ . text ( 'Close' )
92+ . attr ( 'id' , 'tree-close' )
93+ . attr ( 'x' , this . svg . node ( ) . getBoundingClientRect ( ) . width - 15 )
94+ . attr ( 'text-anchor' , 'end' )
95+ . attr ( 'y' , 25 )
96+ . on ( 'click' , ( ) => {
97+ this . svg . classed ( 'hidden' , true ) ;
98+ } ) ;
99+
66100 // add zoom/pan events
67101 this . svg . call ( d3 . zoom ( )
68102 . scaleExtent ( [ 1 / 2 , 4 ] )
@@ -74,10 +108,12 @@ const TreeLayout = (function() {
74108 // selected words to generate graph around
75109 this . word = null ;
76110 this . maxDepth = 20 ; // default value for max dist from root
111+ this . maxWidth = 0 ;
112+ this . layers = [ ] ;
77113 }
78114 resize ( ) {
79- // let bounds = this.svg.node().getBoundingClientRect();
80- // this.g.attr('transform', ` translate(${ bounds.width / 2}, 30)`);// ${ bounds.height / 2})` );
115+ let bounds = this . svg . node ( ) . getBoundingClientRect ( ) ;
116+ this . g . attr ( 'transform' , ' translate(' + [ bounds . width / 2 - this . maxWidth / 2 , bounds . height / 2 - ( this . layers . length - 1 ) * rh / 2 ] + ')' ) ;
81117 }
82118 clear ( ) {
83119 this . word = null ;
@@ -89,8 +125,6 @@ const TreeLayout = (function() {
89125 * Word or Link "root" nodes
90126 */
91127 graph ( selected ) {
92- this . resize ( ) ;
93-
94128 maxDepth = this . maxDepth ;
95129
96130 let data = [ ] ;
@@ -220,6 +254,8 @@ const TreeLayout = (function() {
220254 } ) ;
221255 } // end for
222256
257+ this . maxWidth = maxWidth ;
258+ this . layers = layers ;
223259
224260 let nodeSVG = this . g . selectAll ( '.node' )
225261 . data ( nodes , d => d . node ) ;
@@ -230,8 +266,6 @@ const TreeLayout = (function() {
230266 let edgeSVG = this . g . selectAll ( '.edge' )
231267 . data ( links , d => d . source . node ) ;
232268
233- //layout constants
234- const rh = 50 ; // row height
235269 nodeSVG . exit ( ) . remove ( ) ;
236270 nodeSVG . enter ( ) . append ( 'text' )
237271 . attr ( 'class' , 'node' )
@@ -243,8 +277,7 @@ const TreeLayout = (function() {
243277 . attr ( 'transform' , d => 'translate(' + [ d . offset , d . depth * rh ] + ')' ) ;
244278
245279 // resize
246- let bounds = this . svg . node ( ) . getBoundingClientRect ( ) ;
247- this . g . attr ( 'transform' , 'translate(' + [ bounds . width / 2 - maxWidth / 2 , bounds . height / 2 - layers . length * rh / 2 ] + ')' ) ;
280+ this . resize ( ) ;
248281
249282 edgeSVG . exit ( ) . remove ( ) ;
250283 edgeSVG . enter ( ) . append ( 'path' )
0 commit comments