@@ -5,17 +5,20 @@ import { Cluster, hierarchy } from '@visx/hierarchy';
5
5
import { LinkVertical } from '@visx/shape' ;
6
6
import { LinearGradient } from '@visx/gradient' ;
7
7
import { StateRouteProps } from './StateRoute'
8
+ import { onHover , onHoverExit } from '../actions/actions'
9
+ import { useStoreContext } from '../store'
10
+ import Legend from './AtomsRelationshipLegend'
8
11
9
- const blue = '#acdbdf' ;
10
- const selectWhite = '#f0ece2' ;
12
+ export const blue = '#acdbdf' ;
13
+ export const selectWhite = '#f0ece2' ;
11
14
12
15
export const lightgreen = '#0BAB64' ;
13
- const green = '#3BB78F'
14
- const orange = '#FED8B1' ;
16
+ export const green = '#3BB78F'
17
+ export const orange = '#FED8B1' ;
15
18
16
- const merlinsbeard = '#f7f7f3' ;
19
+ export const merlinsbeard = '#f7f7f3' ;
17
20
export const background = '#242529' ;
18
- const root = '#d2f5e3' ;
21
+ export const root = '#d2f5e3' ;
19
22
20
23
interface clusterShape {
21
24
name ?:string ;
@@ -38,8 +41,10 @@ interface selectorsCache {
38
41
39
42
40
43
const clusterData : clusterShape = { } ;
41
- const selectorsCache :selectorsCache = { }
42
-
44
+ const selectorsCache :selectorsCache = { } ;
45
+ const bothObj = { } ;
46
+
47
+
43
48
let initialFire = false
44
49
function clusterDataPopulate ( props :StateRouteProps ) {
45
50
let atomCompObj = reorganizedCompObj ( props ) ;
@@ -57,10 +62,14 @@ function clusterDataPopulate(props:StateRouteProps) {
57
62
let outerobj :outerObjShape = { }
58
63
outerobj . name = key
59
64
selectorsCache [ key ] = true
65
+
66
+ if ( ! bothObj [ key ] ) {
67
+ bothObj [ key ] = [ ]
68
+ }
69
+
60
70
61
71
if ( props [ 0 ] . atomSelectors [ key ] . length ) {
62
72
for ( let i = 0 ; i < props [ 0 ] . atomSelectors [ key ] . length ; i ++ ) {
63
-
64
73
if ( ! outerobj . children ) outerobj . children = [ ]
65
74
let innerobj :innerObjShape = { }
66
75
innerobj . name = props [ 0 ] . atomSelectors [ key ] [ i ]
@@ -69,8 +78,15 @@ function clusterDataPopulate(props:StateRouteProps) {
69
78
//if atoms contain components
70
79
if ( atomCompObj [ props [ 0 ] . atomSelectors [ key ] [ i ] ] ) {
71
80
for ( let j = 0 ; j < atomCompObj [ props [ 0 ] . atomSelectors [ key ] [ i ] ] . length ; j ++ ) {
81
+ if ( ! bothObj [ props [ 0 ] . atomSelectors [ key ] [ i ] ] ) {
82
+ bothObj [ props [ 0 ] . atomSelectors [ key ] [ i ] ] = [ ]
83
+ }
84
+ bothObj [ props [ 0 ] . atomSelectors [ key ] [ i ] ] . push ( atomCompObj [ props [ 0 ] . atomSelectors [ key ] [ i ] ] [ 0 ] )
85
+
72
86
if ( ! innerobj . children ) innerobj . children = [ ]
73
87
innerobj . children . push ( { name :atomCompObj [ props [ 0 ] . atomSelectors [ key ] [ i ] ] } )
88
+ bothObj [ key ] . push ( atomCompObj [ props [ 0 ] . atomSelectors [ key ] [ i ] ] [ 0 ] )
89
+
74
90
}
75
91
}
76
92
outerobj . children . push ( innerobj )
@@ -81,6 +97,11 @@ function clusterDataPopulate(props:StateRouteProps) {
81
97
if ( atomCompObj [ key ] && atomCompObj [ key ] . length ) {
82
98
for ( let i = 0 ; i < atomCompObj [ key ] . length ; i ++ ) {
83
99
outerobj . children . push ( { name :atomCompObj [ key ] [ i ] } )
100
+
101
+ if ( ! bothObj [ key ] ) {
102
+ bothObj [ key ] = [ ]
103
+ }
104
+ bothObj [ key ] . push ( atomCompObj [ key ] [ i ] )
84
105
}
85
106
}
86
107
@@ -92,9 +113,11 @@ function clusterDataPopulate(props:StateRouteProps) {
92
113
let outObj :outerObjShape = { } ;
93
114
if ( ! selectorsCache [ key ] ) {
94
115
outObj . name = key
116
+ if ( ! bothObj [ key ] ) bothObj [ key ] = [ ]
95
117
for ( let i = 0 ; i < atomCompObj [ key ] . length ; i ++ ) {
96
118
if ( ! outObj . children ) outObj . children = [ ]
97
119
outObj . children . push ( { name :atomCompObj [ key ] [ i ] } )
120
+ bothObj [ key ] . push ( atomCompObj [ key ] [ i ] )
98
121
}
99
122
clusterData . children . push ( outObj )
100
123
}
@@ -120,13 +143,14 @@ function reorganizedCompObj(props) {
120
143
return reorganizedCompObj ;
121
144
}
122
145
123
- function Node ( { node } ) {
146
+ function Node ( { node, snapshots, dispatch, bothObj} ) {
147
+ // const [dispatch] = useStoreContext();
124
148
const selector = node . depth === 1 && node . height === 2
125
149
const isRoot = node . depth === 0 ;
126
150
const isParent = ! ! node . children ;
127
-
151
+
128
152
if ( isRoot ) return < RootNode node = { node } /> ;
129
- if ( selector ) return < SelectorNode node = { node } /> ;
153
+ if ( selector ) return < SelectorNode node = { node } snapshots = { snapshots } bothObj = { bothObj } dispatch = { dispatch } /> ;
130
154
131
155
return (
132
156
< Group top = { node . y } left = { node . x } >
@@ -135,9 +159,16 @@ function Node({ node }) {
135
159
r = { 12 }
136
160
fill = { isParent ? orange : blue }
137
161
stroke = { isParent ? orange : blue }
138
- // onClick={() => {
139
- // alert(`clicked: ${JSON.stringify(node.data.name)}`);
140
- // }}
162
+ onMouseLeave = { ( ) => {
163
+ for ( let i = 0 ; i < bothObj [ node . data . name ] . length ; i ++ ) {
164
+ dispatch ( onHoverExit ( snapshots [ 0 ] . recoilDomNode [ bothObj [ node . data . name ] [ i ] ] ) )
165
+ }
166
+ } }
167
+ onMouseEnter = { ( ) => {
168
+ for ( let i = 0 ; i < bothObj [ node . data . name ] . length ; i ++ ) {
169
+ dispatch ( onHover ( snapshots [ 0 ] . recoilDomNode [ bothObj [ node . data . name ] [ i ] ] ) )
170
+ }
171
+ } }
141
172
/>
142
173
) }
143
174
< text
@@ -156,6 +187,7 @@ function Node({ node }) {
156
187
}
157
188
158
189
function RootNode ( { node } ) {
190
+
159
191
const width = 40 ;
160
192
const height = 20 ;
161
193
const centerX = - width / 2 ;
@@ -189,18 +221,24 @@ function RootNode({ node }) {
189
221
) ;
190
222
}
191
223
192
- function SelectorNode ( { node } ) {
193
-
224
+ function SelectorNode ( { node, snapshots, dispatch, bothObj} ) {
194
225
return (
195
226
< Group top = { node . y } left = { node . x } >
196
227
{ node . depth !== 0 && (
197
228
< circle
198
229
r = { 12 }
199
230
fill = { selectWhite }
200
231
stroke = { selectWhite }
201
- // onClick={() => {
202
- // alert(`clicked: ${JSON.stringify(node.data.name)}`);
203
- // }}
232
+ onMouseLeave = { ( ) => {
233
+ for ( let i = 0 ; i < bothObj [ node . data . name ] . length ; i ++ ) {
234
+ dispatch ( onHoverExit ( snapshots [ 0 ] . recoilDomNode [ bothObj [ node . data . name ] [ i ] ] ) )
235
+ }
236
+ } }
237
+ onMouseEnter = { ( ) => {
238
+ for ( let i = 0 ; i < bothObj [ node . data . name ] . length ; i ++ ) {
239
+ dispatch ( onHover ( snapshots [ 0 ] . recoilDomNode [ bothObj [ node . data . name ] [ i ] ] ) )
240
+ }
241
+ } }
204
242
/>
205
243
) }
206
244
< text
@@ -218,6 +256,15 @@ function SelectorNode({ node }) {
218
256
) ;
219
257
}
220
258
259
+ function removeDup ( bothObj ) {
260
+ let filteredObj = { }
261
+ for ( let key in bothObj ) {
262
+ let array = bothObj [ key ] . filter ( ( a , b ) => bothObj [ key ] . indexOf ( a ) === b )
263
+ filteredObj [ key ] = array
264
+ }
265
+ return filteredObj
266
+ }
267
+
221
268
const defaultMargin = { top : 40 , left : 0 , right : 0 , bottom : 40 } ;
222
269
223
270
// export type DendrogramProps = {
@@ -226,13 +273,18 @@ const defaultMargin = { top: 40, left: 0, right: 0, bottom: 40 };
226
273
// margin?: { top: number; right: number; bottom: number; left: number };
227
274
// };
228
275
229
- export default function Example ( {
276
+ export default function AtomsRelationship ( {
230
277
width,
231
278
height,
232
279
margin = defaultMargin ,
233
280
snapshots,
234
281
} ) {
235
282
283
+
284
+ let filtered = removeDup ( bothObj )
285
+
286
+ const [ { tabs, currentTab } , dispatch ] = useStoreContext ( ) ;
287
+
236
288
if ( ! initialFire ) {
237
289
clusterDataPopulate ( snapshots ) ;
238
290
}
@@ -242,11 +294,16 @@ export default function Example({
242
294
const yMax = height - margin . top - margin . bottom ;
243
295
244
296
return width < 10 ? null : (
297
+ < >
298
+ < div >
299
+ < Legend
300
+ hierarchy = { hierarchy } />
301
+ </ div >
245
302
< svg width = { width } height = { height } >
303
+
246
304
< LinearGradient id = "top" from = { lightgreen } to = { green } />
247
305
248
306
< rect width = { width } height = { height } rx = { 14 } fill = { background } />
249
-
250
307
< Cluster root = { data } size = { [ xMax , yMax ] } >
251
308
{ ( cluster ) => (
252
309
< Group top = { margin . top } left = { margin . left } >
@@ -257,15 +314,20 @@ export default function Example({
257
314
stroke = { merlinsbeard }
258
315
strokeWidth = "1"
259
316
strokeOpacity = { 0.2 }
260
- fill = "none"
317
+ fill = "none"
261
318
/>
262
319
) ) }
263
320
{ cluster . descendants ( ) . map ( ( node , i ) => (
264
- < Node key = { `cluster-node-${ i } ` } node = { node } />
321
+ < Node key = { `cluster-node-${ i } ` }
322
+ node = { node }
323
+ bothObj = { filtered }
324
+ snapshots = { snapshots }
325
+ dispatch = { dispatch } />
265
326
) ) }
266
327
</ Group >
267
328
) }
268
329
</ Cluster >
269
330
</ svg >
331
+ </ >
270
332
) ;
271
- }
333
+ }
0 commit comments