Skip to content

Commit

Permalink
move validateGraph logic to a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
zbynekstara committed Jan 14, 2025
1 parent 3c2b434 commit 8204e21
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions packages/joint-layout-directed-graph/DirectedGraph.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ export const DirectedGraph = {
}
},

// Validate that the graph does not have an arrangement of cells that Dagre considers invalid:
// - child connected to a container
// - container connected to a child
// - container connected to a container
// Throw an understandable error if one of the above situations is the case.
validateGraph: function(graph) {

graph.getLinks().forEach((link) => {
const source = link.getSourceElement();
const target = link.getTargetElement();
// is container = is element && has at least one embedded element
const isSourceContainer = source && source.getEmbeddedCells().some((cell) => cell.isElement());
const isTargetContainer = target && target.getEmbeddedCells().some((cell) => cell.isElement());
if ((isSourceContainer && target) || (source && isTargetContainer)) {
// see https://github.com/clientIO/joint/issues/455
throw new Error('DirectedGraph: It is not possible to connect a child to a container.');
}
});
},

layout: function(graphOrCells, opt) {

var graph;
Expand All @@ -117,23 +137,8 @@ export const DirectedGraph = {
exportLink: this.exportLink
});

// Check that we are not trying to connect a child to a container:
// - child to a container
// - container to a child
// - container to a container
if (opt.validateGraph) {
graph.getLinks().forEach((link) => {
const source = link.getSourceElement();
const target = link.getTargetElement();
// is container = is element && has at least one embedded element
const isSourceContainer = source && source.getEmbeddedCells().some((cell) => cell.isElement());
const isTargetContainer = target && target.getEmbeddedCells().some((cell) => cell.isElement());
if ((isSourceContainer && target) || (source && isTargetContainer)) {
// see https://github.com/clientIO/joint/issues/455
throw new Error('DirectedGraph: It is not possible to connect a child to a container.');
}
});
}
// Check that we are not trying to connect a child to a container.
if (opt.validateGraph) this.validateGraph(graph);

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

0 comments on commit 8204e21

Please sign in to comment.