@@ -8,7 +8,6 @@ import {ServerMapDiagram} from './server-map-diagram.class';
8
8
import { ServerMapData } from './server-map-data.class' ;
9
9
import { IServerMapOption } from './server-map-factory' ;
10
10
import { ServerMapTemplate } from './server-map-template' ;
11
- import { isEmpty } from 'app/core/utils/util' ;
12
11
13
12
const enum GraphStyle {
14
13
NODE_WIDTH = 100 ,
@@ -19,8 +18,9 @@ const enum GraphStyle {
19
18
}
20
19
21
20
export class ServerMapDiagramWithCytoscapejs extends ServerMapDiagram {
21
+ private updateLayoutManually : boolean ;
22
22
private cy : any ;
23
- private addedNodes : any ;
23
+ private addedNodes : any = [ ] ;
24
24
protected computedStyle = getComputedStyle ( document . body ) ;
25
25
protected serverMapColor = {
26
26
text : this . computedStyle . getPropertyValue ( '--text-primary' ) ,
@@ -141,78 +141,34 @@ export class ServerMapDiagramWithCytoscapejs extends ServerMapDiagram {
141
141
} ) ;
142
142
143
143
this . cy . on ( 'layoutstop' , ( ) => {
144
- // TODO: Add experimental flag
145
- // this.cy.nodes().unlock();
146
- // // Check overlay and adjust the position
147
- // const rootNodes = this.cy.nodes().roots().toArray();
148
- // const leafNodes = this.cy.nodes().leaves().toArray();
149
-
150
- // this.addedNodes.forEach((addedNode: any) => {
151
- // const {y} = addedNode.position();
152
- // const {h, y1} = addedNode.boundingBox();
153
- // const labelHeight = h - (y - y1) * 2;
154
- // const isRootNode = rootNodes.some((node: any) => node.id() === addedNode.id());
155
- // const isLeafNode = leafNodes.some((node: any) => node.id() === addedNode.id());
156
-
157
- // if (isRootNode || isLeafNode) {
158
- // const sameTypeNodes = isRootNode ? rootNodes.filter((node: any) => node.id() !== addedNode.id())
159
- // : leafNodes.filter((node: any) => node.id() !== addedNode.id());
160
- // const newX = sameTypeNodes.reduce((acc: number, node: any) => {
161
- // const {x} = node.position();
162
-
163
- // return acc + x;
164
- // }, 0) / sameTypeNodes.length;
165
-
166
- // const overlayableNodes = this.cy.nodes().toArray().filter((node: any) => {
167
- // return this.isXPosOverlaid(addedNode, node);
168
- // });
169
-
170
- // let newY1;
171
-
172
- // if (Math.random() >= 0.5) {
173
- // // Add at the top
174
- // const topY = Math.min(...overlayableNodes.map((node: any) => node.boundingBox().y1));
175
- // const newY2 = topY - GraphStyle.NODE_GAP;
176
-
177
- // newY1 = newY2 - h;
178
- // } else {
179
- // // Add at the bottom
180
- // const bottomY = Math.max(...overlayableNodes.map((node: any) => node.boundingBox().y2));
181
-
182
- // newY1 = bottomY + GraphStyle.NODE_GAP;
183
- // }
184
-
185
- // const newY = (h - labelHeight) / 2 + newY1;
186
-
187
- // addedNode.position({
188
- // x: newX,
189
- // y: newY
190
- // });
191
- // } else {
192
- // const isOverlaid = this.cy.nodes().toArray().some((node: any) => addedNode.id() !== node.id() && this.areTheyOverlaid(addedNode, node));
144
+ if ( ! this . updateLayoutManually ) {
145
+ this . cy . nodes ( ) . unlock ( ) ;
146
+ // Check overlay and adjust the position
147
+ this . addedNodes . forEach ( ( addedNode : any ) => {
148
+ const isOverlaid = this . cy . nodes ( ) . some ( ( node : any ) => addedNode . id ( ) !== node . id ( ) && this . areTheyOverlaid ( addedNode , node ) ) ;
193
149
194
- // if (!isOverlaid) {
195
- // return;
196
- // }
150
+ if ( ! isOverlaid ) {
151
+ return ;
152
+ }
197
153
198
- // const {x, y} = addedNode.position();
199
- // const {h, y1} = addedNode.boundingBox();
200
- // const labelHeight = h - (y - y1) * 2;
154
+ const { x, y} = addedNode . position ( ) ;
155
+ const { h, y1} = addedNode . boundingBox ( ) ;
156
+ const labelHeight = h - ( y - y1 ) * 2 ;
201
157
202
- // const nodesAtSameX = this.cy.nodes().filter((node: any) => {
203
- // return x === node.position().x;
204
- // });
205
- // const topY = Math.min(...nodesAtSameX.map((node: any) => node.boundingBox().y1));
206
- // const newY2 = topY - GraphStyle.NODE_GAP;
207
- // const newY1 = newY2 - h;
208
- // const newY = (h - labelHeight) / 2 + newY1;
158
+ const nodesAtSameX = this . cy . nodes ( ) . filter ( ( node : any ) => {
159
+ return x === node . position ( ) . x ;
160
+ } ) ;
161
+ const topY = Math . min ( ...nodesAtSameX . map ( ( node : any ) => node . boundingBox ( ) . y1 ) ) ;
162
+ const newY2 = topY - GraphStyle . NODE_GAP ;
163
+ const newY1 = newY2 - h ;
164
+ const newY = ( h - labelHeight ) / 2 + newY1 ;
209
165
210
- // addedNode.position({
211
- // x,
212
- // y: newY
213
- // });
214
- // }
215
- // });
166
+ addedNode . position ( {
167
+ x,
168
+ y : newY
169
+ } ) ;
170
+ } ) ;
171
+ }
216
172
} ) ;
217
173
218
174
this . cy . on ( 'select' , 'node' , ( { target} : any ) => {
@@ -396,19 +352,15 @@ export class ServerMapDiagramWithCytoscapejs extends ServerMapDiagram {
396
352
tap ( ( elements : { [ key : string ] : any } ) => {
397
353
this . addedNodes = this . cy . add ( elements ) . nodes ( ) ;
398
354
} ) ,
399
- // filter(() => !isEmpty(this.addedNodes))
400
355
filter ( ( ) => ! this . addedNodes . empty ( ) )
401
356
) . subscribe ( ( elements : { [ key : string ] : any } ) => {
402
- console . log ( 'node 추가!' ) ;
403
- console . log ( this . addedNodes . map ( ( node : any ) => node . id ( ) ) ) ;
404
- // TODO: Add experimental option
405
- // this.cy.nodes().lock();
406
- // this.initLayout();
407
- // this.adjustStyle(elements);
408
-
409
- // this.updateLayout();
410
- // this.updateLayoutWithRank();
411
- this . updateLayoutWithoutRank ( ) ;
357
+ if ( this . updateLayoutManually ) {
358
+ this . updateLayout ( ) ;
359
+ } else {
360
+ this . cy . nodes ( ) . lock ( ) ;
361
+ this . initLayout ( ) ;
362
+ }
363
+
412
364
this . adjustStyle ( elements ) ;
413
365
} ) ;
414
366
} else {
@@ -451,7 +403,7 @@ export class ServerMapDiagramWithCytoscapejs extends ServerMapDiagram {
451
403
this . baseApplicationKey = baseApplicationKey ;
452
404
}
453
405
454
- private updateLayoutWithoutRank ( ) : void {
406
+ private updateLayout ( ) : void {
455
407
const centerNode = this . cy . getElementById ( this . baseApplicationKey ) ;
456
408
const { x : centerNodeX , y : centerNodeY } = centerNode . position ( ) ;
457
409
const { y1 : centerNodeY1 , y2 : centerNodeY2 } = centerNode . boundingBox ( ) ;
@@ -710,6 +662,10 @@ export class ServerMapDiagramWithCytoscapejs extends ServerMapDiagram {
710
662
} ) . run ( ) ;
711
663
}
712
664
665
+ setUpdateLayoutOption ( updateLayoutManually : boolean ) : void {
666
+ this . updateLayoutManually = updateLayoutManually ;
667
+ }
668
+
713
669
redraw ( ) : void {
714
670
this . setMapData ( this . serverMapData , this . baseApplicationKey , this . shouldRefresh ) ;
715
671
}
0 commit comments