Skip to content

fix(layout.DirectedGraph): better error message when dagre fails to connect to container #2838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
add checkContainerConnections option, use Array.some()
  • Loading branch information
zbynekstara committed May 20, 2025
commit ee540fab152fff7d4e4f2c1b1089d19657bd9f12
35 changes: 19 additions & 16 deletions packages/joint-layout-directed-graph/DirectedGraph.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,33 @@ export const DirectedGraph = {
// This is not needed anymore.
graphOrCells = null;

// 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
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().filter((cell) => cell.isElement()).length !== 0);
const isTargetContainer = target && (target.getEmbeddedCells().filter((cell) => cell.isElement()).length !== 0);
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.');
}
});

opt = util.defaults(opt || {}, {
checkContainerConnections: true,
resizeClusters: true,
clusterPadding: 10,
exportElement: this.exportElement,
exportLink: this.exportLink,
disableOptimalOrderHeuristic: false
});

// 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.checkContainerConnections) {
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.');
}
});
}

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