1
- import { ChildInfo , FlowOptions } from './flow-interface' ;
2
- import { FlowChildComponent } from './flow-child.component' ;
3
-
4
- export class Connections {
1
+ import {
2
+ ChildInfo ,
3
+ DotOptions ,
4
+ FlowOptions ,
5
+ FlowPlugin ,
6
+ } from '../flow-interface' ;
7
+ import { FlowChildComponent } from '../flow-child.component' ;
8
+ import { FlowComponent } from '../flow.component' ;
9
+
10
+ export class Connections implements FlowPlugin {
5
11
// key = id of the item
6
12
// value = ids of the items that depend on it
7
13
reverseDepsMap = new Map < string , string [ ] > ( ) ;
@@ -10,12 +16,75 @@ export class Connections {
10
16
// value = index of the closest dot
11
17
closestDots = new Map < string , number > ( ) ;
12
18
13
- constructor (
14
- private list : ChildInfo [ ] ,
15
- private direction : 'horizontal' | 'vertical' = 'horizontal'
16
- ) {
17
- this . setReverseDepsMap ( list . map ( ( x ) => x . position ) ) ;
18
- // console.count('Connections');
19
+ data : FlowComponent ;
20
+ private list : ChildInfo [ ] ;
21
+ private direction : 'horizontal' | 'vertical' = 'horizontal' ;
22
+
23
+ onInit ( data : FlowComponent ) : void {
24
+ this . setData ( data ) ;
25
+ }
26
+
27
+ afterUpdate ( data : FlowComponent ) : void {
28
+ this . setData ( data ) ;
29
+
30
+ const gElement : SVGGElement = this . data . g . nativeElement ;
31
+ const childObj = this . data . getChildInfo ( ) ;
32
+ // Calculate new arrows
33
+ this . data . flow . arrows . forEach ( ( arrow ) => {
34
+ const [ from , to ] = arrow . deps ;
35
+ const fromItem = childObj [ from ] ;
36
+ const toItem = childObj [ to ] ;
37
+ if ( fromItem && toItem ) {
38
+ const [ endDotIndex , startDotIndex ] = this . getClosestDotsSimplified (
39
+ toItem ,
40
+ from
41
+ ) ;
42
+
43
+ const startDot = this . getDotByIndex (
44
+ childObj ,
45
+ fromItem . position ,
46
+ startDotIndex ,
47
+ this . data . flow . scale ,
48
+ this . data . flow . panX ,
49
+ this . data . flow . panY
50
+ ) ;
51
+ const endDot = this . getDotByIndex (
52
+ childObj ,
53
+ toItem . position ,
54
+ endDotIndex ,
55
+ this . data . flow . scale ,
56
+ this . data . flow . panX ,
57
+ this . data . flow . panY
58
+ ) ;
59
+
60
+ // we need to reverse the path because the arrow head is at the end
61
+ arrow . d = this . data . flow . arrowFn (
62
+ endDot ,
63
+ startDot ,
64
+ this . data . flow . config . ArrowSize ,
65
+ 2
66
+ ) ;
67
+ }
68
+
69
+ // Update the SVG paths
70
+ this . data . flow . arrows . forEach ( ( arrow ) => {
71
+ const pathElement = gElement . querySelector (
72
+ `#${ arrow . id } `
73
+ ) as SVGPathElement ;
74
+ if ( pathElement ) {
75
+ pathElement . setAttribute ( 'd' , arrow . d ) ;
76
+ }
77
+ } ) ;
78
+ } ) ;
79
+
80
+ this . updateDotVisibility ( this . data . oldChildObj ( ) ) ;
81
+ }
82
+
83
+ private setData ( data : FlowComponent ) {
84
+ this . data = data ;
85
+ this . list = data . list ;
86
+ this . direction = data . flow . direction ;
87
+ this . setReverseDepsMap ( this . list . map ( ( x ) => x . position ) ) ;
19
88
}
20
89
21
90
public getClosestDotsSimplified (
@@ -28,13 +97,6 @@ export class Connections {
28
97
] ;
29
98
ids . forEach ( ( x ) => this . findClosestDot ( x , item ) ) ;
30
99
// ids.forEach((x) => this.findClosestDot(x, item, childObj));
31
-
32
- // Create unique keys for each dependency and retrieve the closest dots based on these keys
33
- const closestDotIndices = ids . map ( ( x ) => {
34
- const uniqueKey = `${ item . position . id } -${ x } ` ;
35
- return this . closestDots . get ( uniqueKey ) as number ;
36
- } ) ;
37
-
38
100
// Remove duplicates
39
101
// const uniqueClosestDotIndices = Array.from(new Set(closestDotIndices));
40
102
@@ -67,12 +129,6 @@ export class Connections {
67
129
}
68
130
}
69
131
70
- private computeDistance ( dot1 : DOMRect , dot2 : DOMRect ) : number {
71
- const dx = dot1 . x - dot2 . x ;
72
- const dy = dot1 . y - dot2 . y ;
73
- return Math . sqrt ( dx * dx + dy * dy ) ;
74
- }
75
-
76
132
public _findClosestConnectionPoints (
77
133
parent : ChildInfo ,
78
134
child : ChildInfo
@@ -132,7 +188,7 @@ export class Connections {
132
188
return swapped ? [ childIndex , parentIndex ] : [ parentIndex , childIndex ] ;
133
189
}
134
190
135
- updateDotVisibility ( childObj : Record < string , FlowChildComponent > ) {
191
+ private updateDotVisibility ( childObj : Record < string , FlowChildComponent > ) {
136
192
Object . keys ( childObj ) . forEach ( ( id ) => {
137
193
const child = childObj [ id ] ;
138
194
const position = child . position ;
@@ -152,6 +208,30 @@ export class Connections {
152
208
} ) ;
153
209
}
154
210
211
+ private getDotByIndex (
212
+ childObj : Record < string , ChildInfo > ,
213
+ item : FlowOptions ,
214
+ dotIndex : number ,
215
+ scale : number ,
216
+ panX : number ,
217
+ panY : number
218
+ ) : DotOptions {
219
+ const child = childObj [ item . id ] ;
220
+ const childDots = child . dots as DOMRect [ ] ;
221
+ // Make sure the dot index is within bounds
222
+ if ( dotIndex < 0 || dotIndex >= childDots . length ) {
223
+ throw new Error ( `Invalid dot index: ${ dotIndex } ` ) ;
224
+ }
225
+
226
+ const rect = childDots [ dotIndex ] ;
227
+ const { left, top } = this . data . flow . zRect ;
228
+ // const rect = dotEl.nativeElement.getBoundingClientRect();
229
+ const x = ( rect . x + rect . width / 2 - panX - left ) / scale ;
230
+ const y = ( rect . y + rect . height / 2 - panY - top ) / scale ;
231
+
232
+ return { ...item , x, y, dotIndex } ;
233
+ }
234
+
155
235
private setReverseDepsMap ( list : FlowOptions [ ] ) {
156
236
list . forEach ( ( item ) => {
157
237
item . deps . forEach ( ( depId ) => {
0 commit comments