Question on restricting max inbound links

So, I have a rectangular node that I want to allow only one (1) inbound link into. However, I want a port on each side of the node for routing convenience. I can set toMaxLinks on each port, but that would still allow a total of four (4) inbound links, one per port. I tried setting toMaxLinks on the node itself, but that didn’t work, as it appears that if a node has ports, they reign supreme.

One idea I had would be to somehow disable (or worst case, remove) the other ports if an inbound link is made.

Any thoughts on how to tackle this?

The general answer is to implement a validation predicate: GoJS Validation -- Northwoods Software

Your other suggest solution is also easy to implement in combination with setting GraphObject.toMaxLinks. Define event handlers on the Node, Node | GoJS API and Node | GoJS API, that toggle the visible property or set the toMaxLinks property of all the other ports.

Thanks for the reply - I’ll give that a look!

For future use by others, here is what I ended up doing. This ensures that only one (1) link can be made between nodes. You could further customize this based on the type of node, but in my case, I need it globally.

// This predicate is true if there are no existing links into the node, false otherwise
function enforceOneLink(fromNode, fromPort, toNode, toPort) {
  return toNode.findLinksInto().count ? false : true;
}

// This predicate is true if there is only one (this one) into the node, false otherwise
function enforceOneReLink(fromNode, fromPort, toNode, toPort) {
  return toNode.findLinksInto().count > 1 ? false : true;
}

// Only allow new links if there are no existing links!
myDiagram.toolManager.linkingTool.linkValidation = enforceOneLink;

// Same as above, for for relinking instead of linking
myDiagram.toolManager.relinkingTool.linkValidation = enforceOneReLink;