# Class LinkingTool

GoJS® Diagramming Components
version 3.0.13
by Northwoods Software®

#### Hierarchy

- LinkingBaseTool
  - LinkingTool
    - PolylineLinkingTool

The LinkingTool lets a user draw a new Link between two ports, using a mouse-drag operation.

By default an instance of this tool is installed as a mouse-move tool in the Diagram.toolManager as the ToolManager.linkingTool. However this tool may be used modally, as described below.

canStart calls findLinkablePort to find a valid "port" element from which (or to which) the user may interactively draw a new link. doActivate sets up a temporary link and two temporary nodes, one at the start port and one following the mouse.

For a general discussion of validation, see Introduction to Validation.

This tool does not utilize any Adornments or tool handles.

This tool conducts a transaction while the tool is active. A successful linking will result in a "LinkDrawn" DiagramEvent and a "Linking" transaction.

If you want to programmatically start a new user mouse-gesture to draw a new link from a given GraphObject that either is a "port" or may be within the visual tree of a "port", set the startObject property to let findLinkablePort find the real "port" element. Then start and activate this tool:

      const tool = myDiagram.toolManager.linkingTool;
      tool.startObject = ...;
      myDiagram.currentTool = tool;
      tool.doActivate();

#####
Index

### Constructors

constructor

### Accessors

archetypeLabelNodeData
archetypeLinkData
direction
startObject

### Methods

canStart
doActivate
doDeactivate
doMouseUp
doNoLink
findLinkablePort
insertLink

### Properties

BackwardsOnly
Either
ForwardsOnly

#####
Inherited Index

### Accessors

LinkingBaseTool.isForwards
LinkingBaseTool.isUnconnectedLinkValid
LinkingBaseTool.linkValidation
LinkingBaseTool.linkingCursor
LinkingBaseTool.originalFromNode
LinkingBaseTool.originalFromPort
LinkingBaseTool.originalLink
LinkingBaseTool.originalToNode
LinkingBaseTool.originalToPort
LinkingBaseTool.portGravity
LinkingBaseTool.portTargeted
LinkingBaseTool.targetPort
LinkingBaseTool.temporaryFromNode
LinkingBaseTool.temporaryFromPort
LinkingBaseTool.temporaryLink
LinkingBaseTool.temporaryToNode
LinkingBaseTool.temporaryToPort
Tool.diagram
Tool.isActive
Tool.isEnabled
Tool.name
Tool.transactionResult

### Methods

LinkingBaseTool.copyPortProperties
LinkingBaseTool.doMouseMove
LinkingBaseTool.findTargetPort
LinkingBaseTool.isInSameNode
LinkingBaseTool.isLinked
LinkingBaseTool.isValidCycle
LinkingBaseTool.isValidFrom
LinkingBaseTool.isValidLink
LinkingBaseTool.isValidTo
LinkingBaseTool.setNoTargetPortProperties
Tool.canStartMultiTouch
Tool.cancelWaitAfter
Tool.doCancel
Tool.doKeyDown
Tool.doKeyUp
Tool.doMouseDown
Tool.doMouseWheel
Tool.doStart
Tool.doWaitAfter
Tool.findToolHandleAt
Tool.isBeyondDragSize
Tool.standardMouseClick
Tool.standardMouseOver
Tool.standardMouseSelect
Tool.standardMouseWheel
Tool.standardPinchZoomMove
Tool.standardPinchZoomStart
Tool.standardWaitAfter
Tool.startTransaction
Tool.stopTool
Tool.stopTransaction
Tool.updateAdornments

## Constructors

### constructor

- new LinkingTool(init?: Partial<LinkingTool>): LinkingTool

- You do not normally need to create an instance of this tool because one already exists as the ToolManager.linkingTool, which you can modify.

  The Tool.name of this tool is "Linking".

  #### Parameters

  - ##### `Optional` init: Partial<LinkingTool>

  #### Returns LinkingTool

## Accessors

### archetypeLabelNodeData
: ObjectData

- Gets or sets an optional node data object representing a link label, that is copied by insertLink and added to the GraphLinksModel when creating a new Link.

  The default value is null, which causes no such label node data to be added to the model along with the new link data.

  insertLink calls GraphLinksModel.addLabelKeyForLinkData to associate the label node data with the link data.

  Setting this property does not raise any events. This property is ignored if the Diagram.model is not a GraphLinksModel.

### archetypeLinkData
: ObjectData

- Gets or sets a data object that is copied by insertLink and added to the GraphLinksModel when creating a new Link. The default value is an empty Object, which will be copied. The value must be an Object for the linking operation to succeed. A null value will cause insertLink to fail.

  The copied link data's GraphLinksModel.linkFromKeyProperty and GraphLinksModel.linkToKeyProperty properties are set to the corresponding node's data's key values. If the ports have GraphObject.portId values that are not null, the link data's GraphLinksModel.linkFromPortIdProperty and GraphLinksModel.linkToPortIdProperty properties are also set.

  Setting this property does not raise any events. This property is ignored if the Diagram.model is not a GraphLinksModel.

### direction
: LinkingDirection

- Gets or sets the direction in which new links may be drawn. Possible values are LinkingDirection values.. This defaults to LinkingDirection.Either. Setting this property does not raise any events.

### startObject
: GraphObject

- Gets or sets the GraphObject at which findLinkablePort should start its search. The default value is null. Setting this property does not raise any events.

  If you want to explicitly start a new user mouse-gesture to draw a new link from a given GraphObject that may be a "port" object or may be within the visual tree of a "port" object, set this property to that object to let findLinkablePort find the real "port" object. Then start and activate this tool:

        const tool = myDiagram.toolManager.linkingTool;
        tool.startObject = ...;
        myDiagram.currentTool = tool;
        tool.doActivate();

## Methods

### `Override` canStart

- canStart(): boolean

- This tool can run when the diagram allows linking, the model is modifiable, the left-button mouse drag has moved far enough away to not be a click, and when findLinkablePort has returned a valid port or when startObject is a valid port.

  This method may be overridden, but we recommend that you call this base method.

  #### Returns boolean

### `Override` doActivate

- doActivate(): void

- Start the linking operation.

  This calls findLinkablePort to find the port from which to start drawing a new link. If the startObject is already set, it uses that object to find the starting port. If it is not set, it looks for a linkable port at the Diagram.firstInput point. If it finds one, it remembers it as the starting port, otherwise it stops this tool.

  It then starts a transaction, captures the mouse, and changes the cursor. Next it initializes and adds the LinkingBaseTool.temporaryFromNode, LinkingBaseTool.temporaryToNode, and LinkingBaseTool.temporaryLink to the diagram. The temporary nodes that are positioned and sized to be like the real LinkingBaseTool.originalFromPort and LinkingBaseTool.originalToPort ports. The temporary link connects the two temporary ports, of course.

  This method may be overridden, but we recommend that you call this base method.

  #### Returns void

### `Override` doDeactivate

- doDeactivate(): void

- Finishing the linking operation stops the transaction, releases the mouse, and resets the cursor.

  This method may be overridden, but we recommend that you call this base method.

  #### Returns void

### `Override` doMouseUp

- doMouseUp(): void

- A mouse-up ends the linking operation; if there is a valid targetPort nearby, this adds a new Link by calling insertLink.

  If there is a new link, it is selected and the "LinkDrawn" DiagramEvent is raised with the new link as the DiagramEvent.subject. If there is no new link, this calls doNoLink. In any case this stops the tool.

  This method also raises the "ChangingSelection" and "ChangedSelection" diagram events. Changes are performed in a "Linking" transaction, but the "ChangedSelection" event is raised outside the transaction.

  This method may be overridden, but we recommend that you call this base method. It is usually easier to override insertLink or just set archetypeLinkData. It is also common to implement a "LinkDrawn" DiagramEvent listener on the Diagram.

  #### Returns void

### `Virtual` doNoLink

- doNoLink(fromnode: Node, fromport: GraphObject, tonode: Node, toport: GraphObject): void

- This method is called upon a mouse up when no new link is drawn, either because no valid LinkingBaseTool.targetPort was found or because insertLink returned null.

  This method may be overridden. By default this method does nothing. If you want to successfully perform any side-effects, you will need to set Tool.transactionResult to a string; otherwise this tool's transaction will be rolled-back. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Parameters

  - ##### fromnode: Node

  - ##### fromport: GraphObject

    the from-port, or null to use the node itself

  - ##### tonode: Node

  - ##### toport: GraphObject

    the to-port, or null to use the node itself

  #### Returns void

### `Virtual` findLinkablePort

- findLinkablePort(): GraphObject

- Return the GraphObject at the mouse-down point, if it is part of a node and if it is valid to link with it.

  This starts looking for a port at the startObject if it is non-null, otherwise it looks for an object at the Diagram.firstInput's InputEvent.documentPoint. If it finds no object, or if the object it finds is not in a node, this method returns null. If it does find a port, it should set isForwards to declare which direction the new link is being drawn.

  This method may be overridden, but we recommend that you call this base method. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Returns GraphObject

  If the direction is LinkingDirection.Either or LinkingDirection.ForwardsOnly, this checks the element and its parent Node by calling LinkingBaseTool.isValidFrom. If the direction is LinkingDirection.Either or LinkingDirection.BackwardsOnly, this checks the element and its parent Node by calling LinkingBaseTool.isValidTo. In either case finding a matching port will return that port and set LinkingBaseTool.isForwards appropriately. Otherwise this will return null.

### `Virtual` insertLink

- insertLink(fromnode: Node, fromport: GraphObject, tonode: Node, toport: GraphObject): Link

- Make a copy of the archetypeLinkData, set its node and port properties, and add it to the GraphLinksModel. If the model is a TreeModel, set the parent key of the child's node data object.

  For GraphLinksModels, if archetypeLabelNodeData is non-null, this method also adds that node data as an initial label node for the new link data.

  For TreeModels, if Diagram.isTreePathToChildren is false, the roles of the fromnode and tonode arguments are exchanged.

  This method may be overridden, but we recommend that you call this base method. Usually though it is easiest to just set archetypeLinkData to control what kind of link is created. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Parameters

  - ##### fromnode: Node

  - ##### fromport: GraphObject

    the from-port, or null to use the node itself

  - ##### tonode: Node

  - ##### toport: GraphObject

    the to-port, or null to use the node itself

  #### Returns Link

  the newly created Link, or null if it failed.

  `see`

  doNoLink

## Properties

### `Static` `Readonly` BackwardsOnly

`deprecated`

See LinkingDirection.BackwardsOnly.

### `Static` `Readonly` Either

`deprecated`

See LinkingDirection.Either.

### `Static` `Readonly` ForwardsOnly

`deprecated`

See LinkingDirection.ForwardsOnly.

Copyright © 1998-2024 by Northwoods Software Corporation.