1
- import { HierarchyPointNode , Selection } from "d3" ;
2
- import { drawLinks } from "./draw-links " ;
1
+ import { HierarchyPointNode } from "d3-hierarchy " ;
2
+ import { Selection } from "d3-selection " ;
3
3
import { initiliazeSVG } from "./initializeSVG" ;
4
- import { placeLinkEnter } from "./link-enter" ;
5
- import { placeNodeEnter } from "./node-enter" ;
6
- import { placeExit } from "./node-exit" ;
4
+ import { drawLinkEnter } from "./links/link-enter" ;
5
+ import { drawLinkExit } from "./links/link-exit" ;
6
+ import { drawLinkUpdate } from "./links/link-update" ;
7
+ import { drawNodeEnter } from "./nodes/node-enter" ;
8
+ import { drawNodeExit } from "./nodes/node-exit" ;
9
+ import { drawNodeUpdate } from "./nodes/node-update" ;
7
10
import { generateBasicTreemap , generateNestedData } from "./prepare-data" ;
8
11
import { ExtendedHierarchyPointNode , ITreeConfig } from "./typings" ;
9
12
@@ -35,11 +38,11 @@ export function create(userSettings: Partial<ITreeConfig>) {
35
38
} ;
36
39
const settings : ITreeConfig = { ...defaultSettings , ...userSettings } ;
37
40
let oldPosition : Array < { x0 ?: number ; y0 ?: number ; id ?: string } > = [ ] ;
41
+
38
42
function draw (
39
43
svg : Selection < SVGGElement , { } , HTMLElement , any > ,
40
44
computedTree : HierarchyPointNode < { } >
41
45
) {
42
- const duration = 400 ;
43
46
const nodes = computedTree . descendants ( ) as ExtendedHierarchyPointNode [ ] ;
44
47
const links = computedTree . descendants ( ) . slice ( 1 ) ;
45
48
@@ -51,18 +54,20 @@ export function create(userSettings: Partial<ITreeConfig>) {
51
54
} ) ;
52
55
}
53
56
54
- nodes . forEach ( ( el : ExtendedHierarchyPointNode ) => {
55
- el . x0 = {
56
- ...el ,
57
+ nodes . forEach ( ( currentNode : ExtendedHierarchyPointNode ) => {
58
+ currentNode . x0 = {
59
+ ...currentNode ,
57
60
x0 : undefined ,
58
61
y0 : undefined ,
59
- ...oldPosition . filter ( toto => toto . id === el . id ) [ 0 ] ,
62
+ // @ts -ignore
63
+ ...oldPosition . filter ( oldNode => oldNode . id === currentNode . id ) [ 0 ] ,
60
64
} . x0 ;
61
- el . y0 = {
62
- ...el ,
65
+ currentNode . y0 = {
66
+ ...currentNode ,
63
67
x0 : undefined ,
64
68
y0 : undefined ,
65
- ...oldPosition . filter ( toto => toto . id === el . id ) [ 0 ] ,
69
+ // @ts -ignore
70
+ ...oldPosition . filter ( oldNode => oldNode . id === currentNode . id ) [ 0 ] ,
66
71
} . y0 ;
67
72
} ) ;
68
73
@@ -71,73 +76,19 @@ export function create(userSettings: Partial<ITreeConfig>) {
71
76
return d [ settings . nodeField ] ;
72
77
} ) ;
73
78
74
- const nodeEnter = placeNodeEnter ( node , settings ) ;
75
-
76
- nodeEnter
77
- . append ( "rect" )
78
- . attr ( "class" , "node" )
79
- . attr ( "rx" , 5 )
80
- . attr ( "ry" , 5 )
81
- . attr ( "width" , settings . nodeWidth )
82
- . attr ( "height" , settings . nodeHeight )
83
- . style ( "fill" , ( { data } : { data : any } ) => settings . nodeColor ( data ) )
84
- . style ( "cursor" , "pointer" )
85
- . on ( "click" , settings . onNodeClick )
86
- . on ( "mouseenter" , settings . onNodeMouseEnter )
87
- . on ( "mouseleave" , settings . onNodeMouseLeave ) ;
88
-
89
- nodeEnter
90
- . append ( "foreignObject" )
91
- . attr ( "width" , settings . nodeWidth )
92
- . attr ( "height" , settings . nodeHeight )
93
- . style ( "pointer-events" , "none" )
94
- . html ( ( { data } : { data : any } ) => settings . nodeTemplate ( data ) ) ; // settings.nodeTemplate(data));
95
-
96
- // @ts -ignore
97
- const nodeUpdate = nodeEnter . merge ( node ) ;
98
-
99
- nodeUpdate
100
- . transition ( )
101
- . duration ( duration )
102
- . attr ( "transform" , ( d : any ) => {
103
- return settings . horizontalLayout
104
- ? "translate(" + d . y + "," + d . x + ")"
105
- : "translate(" + d . x + "," + d . y + ")" ;
106
- } ) ;
107
-
108
- const nodeExit = placeExit ( node , settings ) ;
109
- nodeExit . select ( "rect" ) . style ( "fill-opacity" , 1e-6 ) ;
110
- nodeExit . select ( "circle" ) . attr ( "r" , 1e-6 ) ;
111
- nodeExit . select ( "text" ) . style ( "fill-opacity" , 1e-6 ) ;
79
+ const nodeEnter = drawNodeEnter ( node , settings ) ;
80
+ drawNodeUpdate ( nodeEnter , node , settings ) ;
81
+ drawNodeExit ( node , settings ) ;
112
82
113
83
// ****************** links section ***************************
114
84
115
85
const link = svg . selectAll ( "path.link" ) . data ( links , ( d : any ) => {
116
86
return d . id ;
117
87
} ) ;
118
88
119
- const linkEnter = placeLinkEnter ( link , computedTree , settings ) ;
120
-
121
- // @ts -ignore
122
- const linkUpdate = linkEnter . merge ( link ) ;
123
-
124
- linkUpdate
125
- . transition ( )
126
- . duration ( duration )
127
- . attr ( "d" , ( d : any ) => {
128
- return drawLinks ( d , d . parent , settings ) ;
129
- } ) ;
130
-
131
- const linkExit = link
132
- . exit ( )
133
- . transition ( )
134
- . duration ( duration )
135
- . attr ( "d" , ( d : any ) => {
136
- const o = { x : d . ancestors ( ) [ 1 ] . x0 , y : d . ancestors ( ) [ 1 ] . y0 } ;
137
- return drawLinks ( o , o , settings ) ;
138
- } )
139
- . remove ( ) ;
140
- linkExit . select ( ".link" ) . attr ( "stroke-opacity" , 1e-6 ) ;
89
+ const linkEnter = drawLinkEnter ( link , computedTree , settings ) ;
90
+ drawLinkUpdate ( linkEnter , link , settings ) ;
91
+ drawLinkExit ( link , settings ) ;
141
92
142
93
nodes . forEach ( ( d : any , index : any ) => {
143
94
oldPosition [ index ] = { x0 : d . x , y0 : d . y , id : d . id } ;
@@ -163,8 +114,8 @@ export function create(userSettings: Partial<ITreeConfig>) {
163
114
oldPosition = [ ] ;
164
115
}
165
116
166
- const obj = { refresh, clean } ;
117
+ const treeObject = { refresh, clean } ;
167
118
168
119
const svg = initiliazeSVG ( settings ) ;
169
- return obj ;
120
+ return treeObject ;
170
121
}
0 commit comments