How can i restrict/allow specific node to have Unconnected Links

This is my diagram configuration FYI.

myDiagram = $$(go.Diagram, "diagram",  
{ 
    initialContentAlignment: go.Spot.Center, 
    scrollMode: go.Diagram.InfiniteScroll,
    allowDrop: true, 
    "undoManager.isEnabled": true,
    contextMenu: myDiagramContextMenu,
    **"linkingTool.isUnconnectedLinkValid": true,**

** “linkingTool.portGravity”: 20,**
** “relinkingTool.isUnconnectedLinkValid”: true,**
** “relinkingTool.portGravity”: 20,**
“relinkingTool.fromHandleArchetype”:
$$(go.Shape, “Diamond”, { segmentIndex: 0, cursor: “pointer”, desiredSize: new go.Size(8, 8), fill: “tomato”, stroke: “darkred” }),
“relinkingTool.toHandleArchetype”:
$$(go.Shape, “Diamond”, { segmentIndex: -1, cursor: “pointer”, desiredSize: new go.Size(8, 8), fill: “darkred”, stroke: “tomato” }),
“linkReshapingTool.handleArchetype”:
$$(go.Shape, “Diamond”, { desiredSize: new go.Size(7, 7), fill: “lightblue”, stroke: “deepskyblue” }),
“commandHandler.pasteFromClipboard”: function(){
var curOffset = go.CommandHandler.prototype.pasteFromClipboard.call(this);
var newOffset = new go.Point(20, 20);
this.diagram.moveParts(curOffset, newOffset);
}
});

I think you still need to set the two properties, just as the Draggable Link sample does, isUnconnectedLinkValid on the two linking tools.

But you also need to add a linkValidation predicate, either on those tools or in your case on the Node, to permit or to disallow such unconnected links.

Thanks for your quick response.

I set the properties as follows, because i need to allow few nodes to have unconnected links

"linkingTool.isUnconnectedLinkValid": true,
"linkingTool.portGravity": 20,
"relinkingTool.isUnconnectedLinkValid": true,
"relinkingTool.portGravity": 20,

and this is my actual link validation function

function linkValidation(fromNode, fromPort, toNode, toPort){
    var fromNodeData = fromNode.data;
    var toNodeData = toNode.data;
    
    if(fromNodeData.compId === 1)
        return false;
    
    return true;
}

in the above link validation function, i tried to restrict users to draw link from node if its component id is 1.

But, its not working

Still user can draw a link from that node as unconnected. it only restrict them to connect to other node. if its not possible using link validation. is it possible to allow few nodes to have unconnected links?.

I was wrong about linkValidation being called when isUnconnectedLinkValid is true and there is no other node. So forget about linkValidation (unless you need it for other reasons) and put the check in the LinkingTool.insertLink method. For example:

$(go.Diagram, . . .,
  { . . .,
    "linkingTool.isUnconnectedLinkValid": true,
    "linkingTool.portGravity": 20,
    "linkingTool.direction": go.LinkingTool.ForwardsOnly,
    "linkingTool.insertLink": function(fromnode, fromport, tonode, toport) {
      if (fromnode && fromnode.data.key === 1 && !tonode) return null;
      return go.LinkingTool.prototype.insertLink.call(this, fromnode, fromport, tonode, toport);
    },
    "relinkingTool.isUnconnectedLinkValid": true,
    "relinkingTool.portGravity": 20,
    "relinkingTool.reconnectLink": function(existinglink, newnode, newport, toend) {
      if (existinglink.fromNode && existinglink.fromNode.data.key === 1 && !newnode) return false;
      return go.RelinkingTool.prototype.reconnectLink.call(this, existinglink, newnode, newport, toend);
    },
    . . .
  })
1 Like

Thanks Walter. It’s work like a charm.