Hi Team,
I want to prevent the Super Parent or Parent group node from overlapping with another node while dragging a child node.
The node hierarchy is as follows: Super Parent Node → Parent Node → Child Node.
Overlap Example:
Note: The parent group node is a transparent type of node, so it’s not visible, but it is present.
(Super parent node is visible as group node)
Code:
let nodeTemplate =
$(go.Node, “Auto”,
{
layoutConditions: go.Part.LayoutStandard & ~go.Part.LayoutNodeSized,
dragComputation: avoidNodeOverlap,
})
let groupTemplate = new go.Group(“Auto”, {
layoutConditions: go.Part.LayoutStandard & ~go.Part.LayoutNodeSized,
dragComputation: avoidNodeOverlap,
})
function avoidNodeOverlap(node: any, pt: any, gridpt: any) {
if (node.diagram instanceof go.Palette) return gridpt;
var bnds = node.actualBounds;
var loc = node.location;
var r = new go.Rect(gridpt.x - (loc.x - bnds.x), gridpt.y - (loc.y - bnds.y), bnds.width, bnds.height);
r.inflate(-0.5, -0.5); // by default, deflate to avoid edge overlaps with “exact” fits
if (!(node.diagram.currentTool instanceof go.DraggingTool) && (!node._temp || !node.layer.isTemporary)) {
node._temp = true; // flag to avoid repeated searches during external drag-and-drop
while (!isUnoccupied(r, node)) {
r.x += 10; // note that this is an unimaginative search algorithm –
r.y += 2; // you can improve the search here to be more appropriate for your app
}
r.inflate(0.5, 0.5); // restore to actual size
return new go.Point(r.x - (loc.x - bnds.x), r.y - (loc.y - bnds.y));
}
if (isUnoccupied(r, node)) return gridpt; // OK
return loc; // give up – don’t allow the node to be moved to the new location
}
function isUnoccupied(r: any, node: any) {
var diagram = node.diagram;
function navig(obj: any) {
var part = obj.part;
if (part === node) return null;
if (part instanceof go.Link) return null;
if (part.isMemberOf(node)) return null;
if (node.isMemberOf(part)) return null;
return part;
}
// only consider non-temporary Layers
var lit = diagram.layers;
while (lit.next()) {
var lay = lit.value;
if (lay.isTemporary) continue;
if (lay.findObjectsIn(r, navig, null, true).count > 0) return false;
}
return true;
}
