Skip to content

Commit

Permalink
Add selectConnectedNodes argument to selectNodeById and selectNodeByI…
Browse files Browse the repository at this point in the history
…ndex

#13
  • Loading branch information
Stukova committed Nov 9, 2022
1 parent 1f0e824 commit 18615fb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ The `left` and `right` values should be from 0 to the width of the canvas in pix

The `top` and `bottom` values should be from 0 to the height of the canvas in pixels.

<a name="select_node_by_id" href="#select_node_by_id">#</a> graph.<b>selectNodeById</b>(<i>id</i>)
<a name="select_node_by_id" href="#select_node_by_id">#</a> graph.<b>selectNodeById</b>(<i>id</i>, [<i>selectConnectedNodes</i>])

Select a node by <i>id</i>.
Select a node by <i>id</i> and its connected nodes if parameter <i>selectConnectedNodes</i> is set to `true` (`false` by default).

<a name="select_node_by_index" href="#select_node_by_index">#</a> graph.<b>selectNodeByIndex</b>(<i>index</i>)
<a name="select_node_by_index" href="#select_node_by_index">#</a> graph.<b>selectNodeByIndex</b>(<i>index</i>, [<i>selectConnectedNodes</i>])

Select a node by <i>index</i>.
Select a node by <i>index</i> and its connected nodes if parameter <i>selectConnectedNodes</i> is set to `true` (`false` by default).

<a name="select_node_by_ids" href="#select_node_by_ids">#</a> graph.<b>selectNodesByIds</b>(<i>ids</i>)

Expand Down
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,25 @@ export class Graph<N extends InputNode, L extends InputLink> {
}

/**
* Select a node by id.
* Select a node by id and its connected nodes if parameter `selectConnectedNodes` is set to `true`.
* @param id Id of the node.
* @param selectConnectedNodes When set to `true`, selects connected nodes (`false` by default).
*/
public selectNodeById (id: string): void {
this.selectNodesByIds([id])
public selectNodeById (id: string, selectConnectedNodes = false): void {
if (selectConnectedNodes) {
const connectedNodes = this.graph.getConnectedNodesToNodeId(id) ?? []
this.selectNodesByIds([id, ...connectedNodes.map(d => d.id)])
} else this.selectNodesByIds([id])
}

/**
* Select a node by index.
* Select a node by index and its connected nodes if parameter `selectConnectedNodes` is set to `true`.
* @param index The index of the node in the array of nodes.
* @param selectConnectedNodes When set to `true`, selects connected nodes (`false` by default).
*/
public selectNodeByIndex (index: number): void {
this.selectNodesByIndices([this.graph.getSortedIndexByInputIndex(index)])
public selectNodeByIndex (index: number, selectConnectedNodes = false): void {
const node = this.graph.getNodeByIndex(index)
if (node) this.selectNodeById(node.id, selectConnectedNodes)
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/modules/GraphData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,13 @@ export class GraphData <N extends InputNode, L extends InputLink> {
public getSortedIndexById (id: string | undefined): number | undefined {
return id !== undefined ? this.idToSortedIndexMap.get(id) : undefined
}

public getConnectedNodesToNodeId (id: string): N[] | undefined {
const index = this.getSortedIndexById(id)
if (index === undefined) return undefined
const outgoingSet = this.groupedSourceToTargetLinks.get(index) ?? []
const incomingSet = this.groupedTargetToSourceLinks.get(index) ?? []
return [...new Set([...outgoingSet, ...incomingSet])]
.map(index => this.getNodeByIndex(this.getInputIndexBySortedIndex(index) as number) as N)
}
}

0 comments on commit 18615fb

Please sign in to comment.