Skip to content

Commit

Permalink
fix(event): Fix firing of GRAPH_LAYOUT_END_EVENT
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-phillips-18 committed Jul 13, 2023
1 parent 5c4cad6 commit dd999c3
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/module/src/layouts/BaseLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,11 @@ export class BaseLayout implements Layout {
// Reset the force simulation
this.stopSimulation();

this.startLayout(this.graph, initialRun, addingNodes);
this.startLayout(this.graph, initialRun, addingNodes, () => {
this.graph.getController().fireEvent(GRAPH_LAYOUT_END_EVENT, { graph: this.graph });
});
} else if (restart && this.options.layoutOnDrag) {
this.updateLayout();
}
this.graph.getController().fireEvent(GRAPH_LAYOUT_END_EVENT, { graph: this.graph });
}
}
3 changes: 2 additions & 1 deletion packages/module/src/layouts/BreadthFirstLayout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Edge, Graph, Layout, Node } from '../types';
import { Edge, Graph, GRAPH_LAYOUT_END_EVENT, Layout, Node } from '../types';
import { BaseLayout } from './BaseLayout';
import { LayoutOptions } from './LayoutOptions';
import { LayoutNode } from './LayoutNode';
Expand Down Expand Up @@ -119,5 +119,6 @@ export class BreadthFirstLayout extends BaseLayout implements Layout {
x = 0;
}
}
this.graph.getController().fireEvent(GRAPH_LAYOUT_END_EVENT, { graph: this.graph });
}
}
7 changes: 5 additions & 2 deletions packages/module/src/layouts/ColaGroupsLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ class ColaGroupsLayout extends ColaLayout implements Layout {
this.startColaLayout(false, this.restartOnEnd);
this.startLayout(graph, false, this.restartOnEnd, this.onEnd);
delete this.restartOnEnd;
} else {
this.onEnd && this.onEnd();
}
} else if (this.addingNodes) {
// One round of simulation to adjust for new nodes
this.forceSimulation.useForceSimulation(this.nodes, this.edges, this.getFixedNodeDistance);
this.forceSimulation.restart();
} else {
this.onEnd && this.onEnd();
}
this.onEnd && this.onEnd();
})();
});
}
Expand Down Expand Up @@ -124,7 +127,7 @@ class ColaGroupsLayout extends ColaLayout implements Layout {
edges: LayoutLink[],
groups: LayoutGroup[]
): BaseLayout {
const layout = new ColaGroupsLayout(graph, { ...this.options, listenForChanges: false });
const layout = new ColaGroupsLayout(graph, { ...this.options, onSimulationEnd: undefined, listenForChanges: false });
layout.setupLayout(graph, nodes, edges, groups);
return layout;
}
Expand Down
11 changes: 10 additions & 1 deletion packages/module/src/layouts/ColaLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { LayoutNode } from './LayoutNode';
import { ColaNode } from './ColaNode';
import { ColaGroup } from './ColaGroup';
import { ColaLink } from './ColaLink';
import { ForceSimulation } from './ForceSimulation';

export interface ColaLayoutOptions {
maxTicks: number;
Expand Down Expand Up @@ -52,6 +53,10 @@ class ColaLayout extends BaseLayout implements Layout {
...COLA_LAYOUT_DEFAULTS,
...options
};
this.forceSimulation = new ForceSimulation({
...this.options,
onSimulationEnd: options?.onSimulationEnd ?? this.onSimulationEnd
});
this.initializeLayout();
}

Expand Down Expand Up @@ -91,13 +96,16 @@ class ColaLayout extends BaseLayout implements Layout {
if (this.restartOnEnd !== undefined) {
this.startColaLayout(false, this.restartOnEnd);
delete this.restartOnEnd;
} else {
this.onEnd && this.onEnd();
}
} else if (this.addingNodes) {
// One round of simulation to adjust for new nodes
this.forceSimulation.useForceSimulation(this.nodes, this.edges, this.getFixedNodeDistance);
this.forceSimulation.restart();
} else {
this.onEnd && this.onEnd();
}
this.onEnd && this.onEnd();
})();
});
}
Expand All @@ -109,6 +117,7 @@ class ColaLayout extends BaseLayout implements Layout {
}
this.addingNodes = false;
}
this.onEnd && this.onEnd();
};

destroy(): void {
Expand Down
3 changes: 2 additions & 1 deletion packages/module/src/layouts/ConcentricLayout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Edge, Graph, Layout, Node } from '../types';
import { Edge, Graph, GRAPH_LAYOUT_END_EVENT, Layout, Node } from '../types';
import { BaseLayout } from './BaseLayout';
import { LayoutOptions } from './LayoutOptions';
import { LayoutNode } from './LayoutNode';
Expand Down Expand Up @@ -103,5 +103,6 @@ export class ConcentricLayout extends BaseLayout implements Layout {
r += maxWH + padding;
}
}
this.graph.getController().fireEvent(GRAPH_LAYOUT_END_EVENT, { graph: this.graph });
}
}
4 changes: 3 additions & 1 deletion packages/module/src/layouts/DagreLayout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dagre from 'dagre';
import * as _ from 'lodash';
import { Edge, Graph, Layout, Node } from '../types';
import { Edge, Graph, GRAPH_LAYOUT_END_EVENT, Layout, Node } from '../types';
import { BaseLayout, LAYOUT_DEFAULTS } from './BaseLayout';
import { LayoutOptions } from './LayoutOptions';
import { LayoutLink } from './LayoutLink';
Expand Down Expand Up @@ -88,6 +88,8 @@ export class DagreLayout extends BaseLayout implements Layout {

if (this.dagreOptions.layoutOnDrag) {
this.forceSimulation.useForceSimulation(this.nodes, this.edges, this.getFixedNodeDistance);
} else {
this.graph.getController().fireEvent(GRAPH_LAYOUT_END_EVENT, { graph: this.graph });
}
}
}
3 changes: 2 additions & 1 deletion packages/module/src/layouts/ForceLayout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Graph, Layout } from '../types';
import { Graph, GRAPH_LAYOUT_END_EVENT, Layout } from '../types';
import { getGroupPadding } from '../utils/element-utils';
import { ForceSimulationNode } from './ForceSimulation';
import { BaseLayout } from '.';
Expand All @@ -12,6 +12,7 @@ export class ForceLayout extends BaseLayout implements Layout {
layoutOnDrag: true,
onSimulationEnd: () => {
this.nodes.forEach(n => n.setFixed(false));
this.graph.getController().fireEvent(GRAPH_LAYOUT_END_EVENT, { graph: this.graph });
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/module/src/layouts/GridLayout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Edge, Graph, Layout, Node } from '../types';
import { Edge, Graph, GRAPH_LAYOUT_END_EVENT, Layout, Node } from '../types';
import { BaseLayout } from './BaseLayout';
import { LayoutOptions } from './LayoutOptions';
import { LayoutNode } from './LayoutNode';
Expand Down Expand Up @@ -67,5 +67,6 @@ export class GridLayout extends BaseLayout implements Layout {
}
}
}
this.graph.getController().fireEvent(GRAPH_LAYOUT_END_EVENT, { graph: this.graph });
}
}

0 comments on commit dd999c3

Please sign in to comment.