@@ -10,6 +10,7 @@ import { LayoutNode } from './LayoutNode';
1010import { ColaNode } from './ColaNode' ;
1111import { ColaGroup } from './ColaGroup' ;
1212import { ColaLink } from './ColaLink' ;
13+ import { ForceSimulation } from './ForceSimulation' ;
1314
1415export interface ColaLayoutOptions {
1516 maxTicks : number ;
@@ -52,6 +53,10 @@ class ColaLayout extends BaseLayout implements Layout {
5253 ...COLA_LAYOUT_DEFAULTS ,
5354 ...options
5455 } ;
56+ this . forceSimulation = new ForceSimulation ( {
57+ ...this . options ,
58+ onSimulationEnd : options ?. onSimulationEnd ?? this . onSimulationEnd
59+ } ) ;
5560 this . initializeLayout ( ) ;
5661 }
5762
@@ -74,7 +79,7 @@ class ColaLayout extends BaseLayout implements Layout {
7479 this . simulationRunning = false ;
7580 action ( ( ) => {
7681 if ( this . destroyed ) {
77- this . onEnd && this . onEnd ( ) ;
82+ this . handleLayoutEnd ( ) ;
7883 return ;
7984 }
8085 this . nodes . forEach ( d => {
@@ -91,24 +96,36 @@ class ColaLayout extends BaseLayout implements Layout {
9196 if ( this . restartOnEnd !== undefined ) {
9297 this . startColaLayout ( false , this . restartOnEnd ) ;
9398 delete this . restartOnEnd ;
99+ } else {
100+ this . handleLayoutEnd ( ) ;
94101 }
95102 } else if ( this . addingNodes ) {
96103 // One round of simulation to adjust for new nodes
97104 this . forceSimulation . useForceSimulation ( this . nodes , this . edges , this . getFixedNodeDistance ) ;
98105 this . forceSimulation . restart ( ) ;
106+ } else {
107+ this . handleLayoutEnd ( ) ;
99108 }
100- this . onEnd && this . onEnd ( ) ;
101109 } ) ( ) ;
102110 } ) ;
103111 }
104112
113+ protected handleLayoutEnd = ( ) => {
114+ if ( this . onEnd ) {
115+ // Only call on end once, then clear it so that it doesn't get called again on simulations
116+ this . onEnd ( ) ;
117+ this . onEnd = undefined ;
118+ }
119+ }
120+
105121 protected onSimulationEnd = ( ) => {
106122 if ( this . addingNodes ) {
107123 if ( ! this . options . layoutOnDrag ) {
108124 this . forceSimulation . stopSimulation ( ) ;
109125 }
110126 this . addingNodes = false ;
111127 }
128+ this . handleLayoutEnd ( ) ;
112129 } ;
113130
114131 destroy ( ) : void {
0 commit comments