NgDiagramModelService
since v0.8.0The NgDiagramModelService provides methods for accessing and manipulating the diagram’s model.
Example usage
Section titled “Example usage”private modelService = inject(NgDiagramModelService);
// Add nodesthis.modelService.addNodes([node1, node2]);Extends
Section titled “Extends”NgDiagramBaseService
Implements
Section titled “Implements”OnDestroy
Properties
Section titled “Properties”
readonlyedges:Signal<Edge<object>[]>
Readonly signal of current edges in the diagram.
metadata
Section titled “metadata”
readonlymetadata:Signal<Metadata<object>>
Readonly signal of current diagram metadata.
readonlynodes:Signal<Node[]>
Readonly signal of current nodes in the diagram.
Methods
Section titled “Methods”addEdges()
Section titled “addEdges()”addEdges(
edges,options?):Promise<void>
Adds new edges to the diagram.
Parameters
Section titled “Parameters”Edge<object>[]
Array of edges to add.
options?
Section titled “options?”Optional settings. Set waitForMeasurements: true to resolve only after the
added elements (e.g. edge labels) have been measured. Available since 1.3.0.
waitForMeasurements?
Section titled “waitForMeasurements?”boolean
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.
addNodes()
Section titled “addNodes()”addNodes(
nodes,options?):Promise<void>
Adds new nodes to the diagram.
Parameters
Section titled “Parameters”Node[]
Array of nodes to add.
options?
Section titled “options?”Optional settings. Set waitForMeasurements: true to resolve only after the
added nodes have been measured — useful before calling zoomToFit() or centerOnNode().
Available since 1.3.0.
waitForMeasurements?
Section titled “waitForMeasurements?”boolean
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.
attachEdge()
Section titled “attachEdge()”attachEdge(
edgeId,end,nodeId,portId?):Promise<boolean>
Attaches one endpoint of an edge to a node and, optionally, to a port. This is the opposite of detachEdge.
The same checks as for a relink drop apply: the node must exist and be
visible, and the port must exist, be visible and have the right direction.
The connection is then validated with linking.validateConnection, which
receives the attached node as source or target according to end,
and a context with reason: 'attach'.
Parameters
Section titled “Parameters”edgeId
Section titled “edgeId”string
ID of the edge to attach.
Which endpoint to attach.
nodeId
Section titled “nodeId”string
ID of the node to attach to.
portId?
Section titled “portId?”string
ID of the port to attach to. When omitted, the endpoint is attached to the node without a port.
Returns
Section titled “Returns”Promise<boolean>
Whether the connection was valid and applied.
1.4.0
computePartsBounds()
Section titled “computePartsBounds()”computePartsBounds(
nodes,edges):Rect
Parameters
Section titled “Parameters”Node[]
Array of nodes
Edge<object>[]
Array of edges
Returns
Section titled “Returns”Bounding rectangle containing all visible nodes and edges. When there is nothing to
measure (both arrays are empty, no visible node has measuredBounds, and no visible edge
has points), returns a zero-size rectangle at the origin ({ x: 0, y: 0, width: 0, height: 0 }).
0.9.0
Computes the axis-aligned bounding rectangle that contains all specified nodes and edges.
Node bounds come from measuredBounds, which includes the measured ports and the rotation,
not from the raw position and size in the model. Nodes must therefore already be measured
(rendered), and the result can be larger than the node rectangles because of the ports.
Edges contribute their routed points and their measured labels.
Remarks
Section titled “Remarks”Since 1.4.0, effectively hidden elements (computedHidden) are left out, so their
old geometry does not enlarge the result. If all given elements are hidden, the result is
the same zero-size rectangle at the origin as for unmeasured elements.
deleteEdges()
Section titled “deleteEdges()”deleteEdges(
ids):Promise<void>
Deletes edges by their IDs.
Parameters
Section titled “Parameters”string[]
Array of edge IDs to delete.
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.
deleteNodes()
Section titled “deleteNodes()”deleteNodes(
ids):Promise<void>
Deletes nodes by their IDs.
Deleting a group also deletes all of its descendants (children, grandchildren, and so on). Edges connected to any deleted node are removed as well. This is the same behavior as deleting the selection.
Parameters
Section titled “Parameters”string[]
Array of node IDs to delete.
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.
detachEdge()
Section titled “detachEdge()”detachEdge(
edgeId,end,position?):Promise<void>
Detaches one endpoint of an edge, so that it becomes a free (dangling) endpoint.
When position is omitted, the endpoint stays anchored where it is now:
at the current position of the port when the edge was connected to a
port, otherwise at the routed endpoint of the edge.
Requires danglingEdges.enabled. With the feature off, this method does
nothing and logs a console warning.
Parameters
Section titled “Parameters”edgeId
Section titled “edgeId”string
ID of the edge to detach.
Which endpoint to detach.
position?
Section titled “position?”Optional anchor position for the freed endpoint, in flow coordinates.
Returns
Section titled “Returns”Promise<void>
1.4.0
getChildren()
Section titled “getChildren()”getChildren<
T>(groupId):Node<T>[]
Gets all children nodes for a given group node id
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the nodes’ data property. Defaults to DataObject.
Parameters
Section titled “Parameters”groupId
Section titled “groupId”string
group node id
Returns
Section titled “Returns”Node<T>[]
Array of child nodes
getChildrenNested()
Section titled “getChildrenNested()”getChildrenNested<
T>(groupId):Node<T>[]
Gets all nested children (descendants) of a group node
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the nodes’ data property. Defaults to DataObject.
Parameters
Section titled “Parameters”groupId
Section titled “groupId”string
Group node id
Returns
Section titled “Returns”Node<T>[]
Array of all descendant nodes (children, grandchildren, etc.)
getConnectedEdges()
Section titled “getConnectedEdges()”getConnectedEdges<
T>(nodeId):Edge<T>[]
Gets all edges connected to a node
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the edges’ data property. Defaults to DataObject.
Parameters
Section titled “Parameters”nodeId
Section titled “nodeId”string
Node id
Returns
Section titled “Returns”Edge<T>[]
Array of edges where the node is either source or target
getConnectedNodes()
Section titled “getConnectedNodes()”getConnectedNodes<
T>(nodeId):Node<T>[]
Gets all nodes connected to a node via edges
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the nodes’ data property. Defaults to DataObject.
Parameters
Section titled “Parameters”nodeId
Section titled “nodeId”string
Node id
Returns
Section titled “Returns”Node<T>[]
Array of nodes connected to the given node
getDanglingEndpoints()
Section titled “getDanglingEndpoints()”getDanglingEndpoints():
DanglingEndpoint[]
Returns the free (unconnected) endpoints of all edges in the model. A dual dangling edge gives two entries. Temporary and effectively hidden edges are skipped.
Returns
Section titled “Returns”The free endpoints with their edge, end and anchor position.
1.4.0
getEdgeById()
Section titled “getEdgeById()”getEdgeById<
T>(edgeId):null|Edge<T>
Gets an edge by id.
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the edge’s data property. Defaults to DataObject.
Parameters
Section titled “Parameters”edgeId
Section titled “edgeId”string
Edge id.
Returns
Section titled “Returns”null | Edge<T>
Edge or null if not found.
getModel()
Section titled “getModel()”getModel():
ModelAdapter
Returns the current model that NgDiagram instance is using. Returns null if flowCore is not initialized.
Returns
Section titled “Returns”getNearestDanglingEndpointInRange()
Section titled “getNearestDanglingEndpointInRange()”getNearestDanglingEndpointInRange(
point,range):null|DanglingEndpoint
Finds the free edge endpoint nearest to a point within a range. It works like getNearestPortInRange, but for the free endpoints of dangling edges. Temporary and effectively hidden edges are skipped.
Parameters
Section titled “Parameters”Point to check from.
number
Range to check in.
Returns
Section titled “Returns”null | DanglingEndpoint
Nearest free endpoint in range, or null.
1.4.0
getNearestNodeInRange()
Section titled “getNearestNodeInRange()”getNearestNodeInRange<
T>(point,range):null|Node<T>
Gets the nearest node in a range from a point.
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the node’s data property. Defaults to DataObject.
Parameters
Section titled “Parameters”Point to check from.
number
Range to check in.
Returns
Section titled “Returns”null | Node<T>
Nearest node in range or null.
getNearestPortInRange()
Section titled “getNearestPortInRange()”getNearestPortInRange(
point,range):null|Port
Gets the nearest port in a range from a point.
Parameters
Section titled “Parameters”Point to check from.
number
Range to check in.
Returns
Section titled “Returns”null | Port
Nearest port in range or null.
getNodeById()
Section titled “getNodeById()”getNodeById<
T>(nodeId):null|Node<T>
Gets a node by id.
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the node’s data property. Defaults to DataObject.
Parameters
Section titled “Parameters”nodeId
Section titled “nodeId”string
Node id.
Returns
Section titled “Returns”null | Node<T>
Node or null if not found.
getNodeEnds()
Section titled “getNodeEnds()”getNodeEnds<
S,T>(edgeId):null| {source:Node<S>;target:Node<T>; }
Gets the source and target nodes of an edge
Type Parameters
Section titled “Type Parameters”S extends object = object
The type of the source node’s data property. Defaults to DataObject.
T extends object = object
The type of the target node’s data property. Defaults to DataObject.
Parameters
Section titled “Parameters”edgeId
Section titled “edgeId”string
Edge id
Returns
Section titled “Returns”null | { source: Node<S>; target: Node<T>; }
Object containing source and target nodes, or null if edge doesn’t exist
getNodesInRange()
Section titled “getNodesInRange()”getNodesInRange<
T>(point,range):Node<T>[]
Gets all nodes in a range from a point.
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the nodes’ data property. Defaults to DataObject.
Parameters
Section titled “Parameters”Point to check from.
number
Range to check in.
Returns
Section titled “Returns”Node<T>[]
Array of nodes in range.
getOverlappingNodes()
Section titled “getOverlappingNodes()”Call Signature
Section titled “Call Signature”getOverlappingNodes<
T>(nodeId):Node<T>[]
Detects collision with other nodes by finding all nodes whose rectangles intersect with the specified node’s bounding rectangle.
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the nodes’ data property. Defaults to DataObject.
Parameters
Section titled “Parameters”nodeId
Section titled “nodeId”string
The ID of the node to check for collisions
Returns
Section titled “Returns”Node<T>[]
An array of Nodes that overlap with the specified node
Call Signature
Section titled “Call Signature”getOverlappingNodes<
T>(node):Node<T>[]
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the nodes’ data property. Defaults to DataObject.
Parameters
Section titled “Parameters”Node<T>
The node to check for collisions
Returns
Section titled “Returns”Node<T>[]
An array of Nodes that overlap with the specified node
0.9.0
Detects collision with other nodes by finding all nodes whose rectangles intersect with the specified node’s bounding rectangle.
getParentHierarchy()
Section titled “getParentHierarchy()”getParentHierarchy<
T>(nodeId):GroupNode<T>[]
Gets the full chain of parent group Nodes for a given nodeId.
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the group nodes’ data property. Defaults to DataObject.
Parameters
Section titled “Parameters”nodeId
Section titled “nodeId”string
Node id
Returns
Section titled “Returns”GroupNode<T>[]
Array of parent group Node objects, from closest parent to farthest ancestor
isNestedChild()
Section titled “isNestedChild()”isNestedChild(
nodeId,groupId):boolean
Checks if a node is a nested child (descendant) of a group node
Parameters
Section titled “Parameters”nodeId
Section titled “nodeId”string
Node id
groupId
Section titled “groupId”string
Group node id
Returns
Section titled “Returns”boolean
True if the node is part of the group’s nested subgraph
toJSON()
Section titled “toJSON()”toJSON():
string
Serializes the current model to a JSON string.
Returns
Section titled “Returns”string
The model as a JSON string.
updateEdge()
Section titled “updateEdge()”updateEdge(
edgeId,edge,options?):Promise<void>
Updates the properties of an edge.
Parameters
Section titled “Parameters”edgeId
Section titled “edgeId”string
Edge id.
Partial<Edge>
New edge properties.
options?
Section titled “options?”Optional settings. Set waitForMeasurements: true to resolve only after
measurements triggered by the update have completed. Available since 1.3.0.
waitForMeasurements?
Section titled “waitForMeasurements?”boolean
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.
updateEdgeData()
Section titled “updateEdgeData()”updateEdgeData<
T>(edgeId,data,options?):Promise<void>
Updates the data of an edge.
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the edge’s data property. Defaults to DataObject.
Parameters
Section titled “Parameters”edgeId
Section titled “edgeId”string
Edge id.
T
New data to set for the edge.
options?
Section titled “options?”Optional settings. Set waitForMeasurements: true to resolve only after
measurements triggered by the update (e.g. re-rendered edge labels) have completed.
Available since 1.3.0.
waitForMeasurements?
Section titled “waitForMeasurements?”boolean
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.
updateEdges()
Section titled “updateEdges()”updateEdges(
edges,options?):Promise<void>
Updates multiple edges at once.
Parameters
Section titled “Parameters”Pick<Edge<object>, "id"> & Partial<Edge<object>>[]
Array of edge updates (must include id and any properties to update).
options?
Section titled “options?”Optional settings. Set waitForMeasurements: true to resolve only after
measurements triggered by the update have completed. Available since 1.3.0.
waitForMeasurements?
Section titled “waitForMeasurements?”boolean
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.
updateNode()
Section titled “updateNode()”updateNode(
nodeId,node,options?):Promise<void>
Updates the properties of a node.
Parameters
Section titled “Parameters”nodeId
Section titled “nodeId”string
Node id.
Partial<Node>
New node properties.
options?
Section titled “options?”Optional settings. Set waitForMeasurements: true to resolve only after
measurements triggered by the update have completed. Available since 1.3.0.
waitForMeasurements?
Section titled “waitForMeasurements?”boolean
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.
updateNodeData()
Section titled “updateNodeData()”updateNodeData<
T>(nodeId,data,options?):Promise<void>
Updates the data of a node.
Type Parameters
Section titled “Type Parameters”T extends object = object
The type of the node’s data property. Defaults to DataObject.
Parameters
Section titled “Parameters”nodeId
Section titled “nodeId”string
Node id.
T
New data to set for the node.
options?
Section titled “options?”Optional settings. Set waitForMeasurements: true to resolve only after
measurements triggered by the update (e.g. a template resized by the new data) have
completed. Available since 1.3.0.
waitForMeasurements?
Section titled “waitForMeasurements?”boolean
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.
updateNodes()
Section titled “updateNodes()”updateNodes(
nodes,options?):Promise<void>
Updates multiple nodes at once.
Parameters
Section titled “Parameters”Pick<Node, "id"> & Partial<Node>[]
Array of node updates (must include id and any properties to update).
options?
Section titled “options?”Optional settings. Set waitForMeasurements: true to resolve only after
measurements triggered by the update have completed. Available since 1.3.0.
waitForMeasurements?
Section titled “waitForMeasurements?”boolean
Returns
Section titled “Returns”Promise<void>
A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.