Skip to content

Commit 7f0eb64

Browse files
committed
move validateGraph logic to a separate function
1 parent dcac8e3 commit 7f0eb64

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

packages/joint-layout-directed-graph/DirectedGraph.mjs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ export const DirectedGraph = {
105105
}
106106
},
107107

108+
// Validate that the graph does not have an arrangement of cells that Dagre considers invalid:
109+
// - child connected to a container
110+
// - container connected to a child
111+
// - container connected to a container
112+
// Throw an understandable error if one of the above situations is the case.
113+
validateGraph: function(graph) {
114+
115+
graph.getLinks().forEach((link) => {
116+
const source = link.getSourceElement();
117+
const target = link.getTargetElement();
118+
// is container = is element && has at least one embedded element
119+
const isSourceContainer = source && source.getEmbeddedCells().some((cell) => cell.isElement());
120+
const isTargetContainer = target && target.getEmbeddedCells().some((cell) => cell.isElement());
121+
if ((isSourceContainer && target) || (source && isTargetContainer)) {
122+
// see https://github.com/clientIO/joint/issues/455
123+
throw new Error('DirectedGraph: It is not possible to connect a child to a container.');
124+
}
125+
});
126+
},
127+
108128
layout: function(graphOrCells, opt) {
109129

110130
var graph;
@@ -129,23 +149,8 @@ export const DirectedGraph = {
129149
disableOptimalOrderHeuristic: false
130150
});
131151

132-
// Check that we are not trying to connect a child to a container:
133-
// - child to a container
134-
// - container to a child
135-
// - container to a container
136-
if (opt.validateGraph) {
137-
graph.getLinks().forEach((link) => {
138-
const source = link.getSourceElement();
139-
const target = link.getTargetElement();
140-
// is container = is element && has at least one embedded element
141-
const isSourceContainer = source && source.getEmbeddedCells().some((cell) => cell.isElement());
142-
const isTargetContainer = target && target.getEmbeddedCells().some((cell) => cell.isElement());
143-
if ((isSourceContainer && target) || (source && isTargetContainer)) {
144-
// see https://github.com/clientIO/joint/issues/455
145-
throw new Error('DirectedGraph: It is not possible to connect a child to a container.');
146-
}
147-
});
148-
}
152+
// Check that we are not trying to connect a child to a container.
153+
if (opt.validateGraph) this.validateGraph(graph);
149154

150155
// Create a graphlib.Graph that represents the joint.dia.Graph
151156
var glGraph = DirectedGraph.toGraphLib(graph, {

0 commit comments

Comments
 (0)