layouter-utils.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { Map as makeMap } from 'immutable';
  2. import { EDGE_ID_SEPARATOR } from '../constants/naming';
  3. export function getNodesFromEdgeId(edgeId) {
  4. return edgeId.split(EDGE_ID_SEPARATOR);
  5. }
  6. export function constructEdgeId(source, target) {
  7. return [source, target].join(EDGE_ID_SEPARATOR);
  8. }
  9. // Constructs the edges for the layout engine from the nodes' adjacency table.
  10. // We don't collapse edge pairs (A->B, B->A) here as we want to let the layout
  11. // engine decide how to handle bidirectional edges.
  12. export function initEdgesFromNodes(nodes) {
  13. let edges = makeMap();
  14. nodes.forEach((node, nodeId) => {
  15. (node.get('adjacency') || []).forEach((adjacentId) => {
  16. const source = nodeId;
  17. const target = adjacentId;
  18. if (nodes.has(target)) {
  19. // The direction source->target is important since dagre takes
  20. // directionality into account when calculating the layout.
  21. const edgeId = constructEdgeId(source, target);
  22. const edge = makeMap({
  23. id: edgeId, source, target, value: 1
  24. });
  25. edges = edges.set(edgeId, edge);
  26. }
  27. });
  28. });
  29. return edges;
  30. }