React-diagrams: Finding connected nodes

Created on 30 Apr 2019  路  1Comment  路  Source: projectstorm/react-diagrams

Traversing the diagram seems quite complex.

As far as I can tell any two nodes are connected via:

Screenshot from 2019-04-30 16-22-51

(child) Node 1 -- (source) Out port -- link -- (target) In port -- (parent) Node 2

This is the code that I have for finding the child nodes connected via a port to a parent node:

/**
 * Find children of a node
 * @param   {Node}    node     Parent node
 * @param   {string}  portName Port connecting to children e.g. 'In'
 * @returns {[Node]}           List of Nodes
 */
const children = (node, portName) => { 
  const links = Object.values(node.getPort(portName).getLinks());
  return links.map(link => link.getSourcePort().getNode());
};

Links and Ports are both objects so to iterate over them you have to do something like Object.values or Object.entries each time

Is there some other/neater way of doing this?

answered feature request question

Most helpful comment

Hi,

What I suggest to do is to create your own class that you extends from NodeModel

Then you declare a child attribute

export class BrickModel extends NodeModel {

  ports: { [s: string]: PortModel };
  child: string[]; <---- You can change here according to your needs

And so, each time you'll link a node to another one, you'll add the new node ID to the the parent node
child attribute.

You can work as a "double linked list" so you can get your first node or the last one.

Sorry about my English, don't hesitate if you have any questions ;)

>All comments

Hi,

What I suggest to do is to create your own class that you extends from NodeModel

Then you declare a child attribute

export class BrickModel extends NodeModel {

  ports: { [s: string]: PortModel };
  child: string[]; <---- You can change here according to your needs

And so, each time you'll link a node to another one, you'll add the new node ID to the the parent node
child attribute.

You can work as a "double linked list" so you can get your first node or the last one.

Sorry about my English, don't hesitate if you have any questions ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

affanshahid picture affanshahid  路  3Comments

shortwavedave picture shortwavedave  路  3Comments

DanieLazarLDAPPS picture DanieLazarLDAPPS  路  3Comments

msaglietto picture msaglietto  路  3Comments

schecter22107 picture schecter22107  路  3Comments