1
- import {
2
- FlowOptions ,
3
- ChildInfo ,
4
- FlowDirection ,
5
- FlowPlugin ,
6
- } from '../flow-interface' ;
1
+ import { FlowOptions , ChildInfo , FlowDirection } from '../flow-interface' ;
7
2
import { FlowComponent } from '../flow.component' ;
8
-
9
- export class ArrangementsOld {
10
- constructor (
11
- private list : ChildInfo [ ] ,
12
- private direction : 'horizontal' | 'vertical' = 'horizontal' ,
13
- public horizontalPadding = 100 ,
14
- public verticalPadding = 20 ,
15
- public groupPadding = 20
16
- ) { }
17
-
18
- public autoArrange ( ) : Map < string , FlowOptions > {
19
- const newItems = new Map < string , FlowOptions > ( ) ;
20
- let currentX = 0 ;
21
- let currentY = 0 ;
22
-
23
- // Handle both horizontal and vertical directions
24
- const baseNodes = this . list . filter (
25
- ( node ) => node . position . deps . length === 0
26
- ) ;
27
-
28
- for ( const baseNode of baseNodes ) {
29
- if ( this . direction === 'horizontal' ) {
30
- this . positionDependents ( baseNode , 0 , currentY , newItems ) ;
31
- currentY += baseNode . elRect . height + this . verticalPadding ;
32
- } else {
33
- // Vertical arrangement
34
- this . positionDependents ( baseNode , 0 , currentX , newItems ) ;
35
- currentX += baseNode . elRect . width + this . verticalPadding ;
36
- }
37
- }
38
-
39
- return newItems ;
40
- }
41
-
42
- private positionDependents (
43
- baseNode : ChildInfo ,
44
- baseX : number ,
45
- baseY : number ,
46
- newItems : Map < string , FlowOptions > ,
47
- config : { first : boolean ; gp : number ; maxDepLength : number } = {
48
- first : true ,
49
- gp : - this . groupPadding * 2 ,
50
- maxDepLength : 0 ,
51
- }
52
- ) : { consumedSpace : number ; dep : boolean } {
53
- const dependents = this . list . filter ( ( child ) =>
54
- child . position . deps . includes ( baseNode . position . id )
55
- ) ;
56
-
57
- const isV = this . direction === 'vertical' ;
58
-
59
- let startY = baseY ;
60
- const { width : w , height : h } = baseNode . elRect ;
61
- let newX = baseX + ( isV ? h : w ) + this . horizontalPadding ;
62
- const height = isV ? w : h ;
63
-
64
- const childC : { first : boolean ; gp : number ; maxDepLength : number } = {
65
- first : true ,
66
- gp : 0 ,
67
- maxDepLength : 0 ,
68
- } ;
69
- for ( let i = 0 ; i < dependents . length ; i ++ ) {
70
- const depLast = i === dependents . length - 1 ;
71
- childC . first = i === 0 ;
72
- const dependent = dependents [ i ] ;
73
- const { consumedSpace, dep } = this . positionDependents (
74
- dependent ,
75
- newX ,
76
- startY ,
77
- newItems ,
78
- childC
79
- ) ;
80
-
81
- startY = 0 ;
82
-
83
- if ( childC . maxDepLength > 1 && dep && ! depLast ) {
84
- startY += this . groupPadding ;
85
- config . gp += this . groupPadding ;
86
- }
87
- startY += consumedSpace + ( ! depLast ? this . verticalPadding : 0 ) ;
88
- }
89
-
90
- // baseY += childC.gp;
91
- config . maxDepLength = Math . max ( config . maxDepLength , childC . maxDepLength ) ;
92
-
93
- let y = 0 ;
94
- if ( dependents . length > 1 ) {
95
- // find the first and last dependent and there y position
96
- const firstDepId = dependents [ 0 ] . position . id ;
97
- const lastDepId = dependents [ dependents . length - 1 ] . position . id ;
98
- const firstDep = newItems . get ( firstDepId ) ! ;
99
- const lastDep = newItems . get ( lastDepId ) ! ;
100
- // find the center of the first and last dependent
101
- y = ( isV ? firstDep . x + lastDep . x : firstDep . y + lastDep . y ) / 2 ;
102
- } else {
103
- y = baseY + ( dependents . length ? ( startY - baseY ) / 2 - height / 2 : 0 ) ;
104
-
105
- // TODO: This is not working as expected
106
- // If there are more than one dependency, We need to center the node based on the parents
107
- if ( baseNode . position . deps . length > 1 ) {
108
- const len = baseNode . position . deps . length / 2 ;
109
- const halfVerticalPadding = ( this . verticalPadding * len ) / 2 ;
110
- y -= baseNode . elRect . height * len - halfVerticalPadding ;
111
- }
112
- }
113
- newItems . set ( baseNode . position . id , {
114
- ...baseNode . position ,
115
- x : isV ? y : baseX ,
116
- y : isV ? baseX : y ,
117
- } ) ;
118
- // add groupPadding if there are more than one dependency
119
- const groupPad =
120
- dependents . length > 1 ? this . groupPadding - this . verticalPadding : 0 ;
121
- const consumedSpace = startY + ( dependents . length ? 0 : height ) + groupPad ;
122
- return { consumedSpace, dep : dependents . length > 0 } ;
123
- }
124
- }
3
+ import { FlowPlugin } from './plugin' ;
125
4
126
5
const ROOT_DATA = new Map < string , ArrangeNode > ( ) ;
127
6
const ROOT_DEPS = new Map < string , string [ ] > ( ) ;
@@ -137,8 +16,6 @@ export class Arrangements implements FlowPlugin {
137
16
public verticalPadding = 20 ;
138
17
public groupPadding = 20 ;
139
18
140
- constructor ( ) { }
141
-
142
19
onInit ( data : FlowComponent ) : void {
143
20
this . data = data ;
144
21
}
@@ -213,7 +90,7 @@ export class ArrangeNode {
213
90
214
91
// we need to recursively call this method to get all the dependents of the node
215
92
// and then we need to position them
216
- arrange ( x = 0 , y = 0 , direction : FlowDirection ) : Coordinates {
93
+ arrange ( x : number , y : number , direction : FlowDirection ) : Coordinates {
217
94
const dependents = ROOT_DEPS . get ( this . position . id ) || [ ] ;
218
95
let startX = x ;
219
96
let startY = y ;
@@ -225,7 +102,8 @@ export class ArrangeNode {
225
102
} else {
226
103
startY += this . elRect . height + HORIZONTAL_PADDING ;
227
104
}
228
- let first , last : Coordinates ;
105
+ let first : Coordinates = { x : 0 , y : 0 } ;
106
+ let last : Coordinates = { x : 0 , y : 0 } ;
229
107
for ( let i = 0 ; i < len ; i ++ ) {
230
108
const dep = dependents [ i ] ;
231
109
const dependent = ROOT_DATA . get ( dep ) ! ;
@@ -242,10 +120,10 @@ export class ArrangeNode {
242
120
}
243
121
if ( direction === 'horizontal' ) {
244
122
startY -= VERTICAL_PADDING + this . elRect . height ;
245
- y = first ! . y + ( last ! . y - first ! . y ) / 2 ;
123
+ y = first . y + ( last . y - first . y ) / 2 ;
246
124
} else {
247
125
startX -= VERTICAL_PADDING + this . elRect . width ;
248
- x = first ! . x + ( last ! . x - first ! . x ) / 2 ;
126
+ x = first . x + ( last . x - first . x ) / 2 ;
249
127
}
250
128
}
251
129
this . position . x = x ;
0 commit comments