pathFinder.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Module contains utility to search graph nodes.
  3. *
  4. * You can use and change this freely as long as you keep this note.
  5. * @copyright: Simon Speich, 2013
  6. *
  7. * Note: Can't use dojo/dom-class since SVGElements are not supported
  8. * @see https://bugs.dojotoolkit.org/ticket/16309
  9. * @module dgraph/pathFinder
  10. */
  11. import {svgCssUtil} from './svgCssUtil.js';
  12. export let pathFinder = {
  13. /**
  14. * Find connected target nodes using a breadth first search (BFS) search.
  15. * @param {graphNode} node graph node
  16. * @param {Array} nodelist list of graph nodes
  17. * @param {Function} fnc callback
  18. */
  19. searchByTargets: function(node, nodelist, fnc) {
  20. let openNodes = [],
  21. newNode, neighbors, i, len, nextNode;
  22. openNodes.push(node);
  23. while (openNodes.length > 0) {
  24. newNode = openNodes.shift();
  25. neighbors = newNode.trgNodes;
  26. len = neighbors.length;
  27. // Add each neighbor to the beginning of openNodes
  28. for (i = 0; i < len; i++) {
  29. nextNode = nodelist[neighbors[i][1]][neighbors[i][0]];
  30. fnc.apply(this, [nextNode]);
  31. openNodes.unshift(nextNode);
  32. }
  33. }
  34. },
  35. /**
  36. * Find connected source nodes using a breadth first search (BFS) search.
  37. * @param {graphNode} node graph node
  38. * @param {Array} nodelist list of graph nodes
  39. * @param {Function} fnc callback
  40. */
  41. searchBySources: function(node, nodelist, fnc) {
  42. let openNodes = [],
  43. newNode, neighbors, i, len, nextNode;
  44. openNodes.push(node);
  45. while (openNodes.length > 0) {
  46. newNode = openNodes.shift();
  47. neighbors = newNode.srcNodes;
  48. len = neighbors.length;
  49. // Add each neighbor to the beginning of openNodes
  50. for (i = 0; i < len; i++) {
  51. nextNode = nodelist[neighbors[i][1]][neighbors[i][0]];
  52. fnc.apply(this, [newNode, nextNode]);
  53. openNodes.unshift(nextNode);
  54. }
  55. }
  56. },
  57. /**
  58. * Highlight all connected edges.
  59. * @param {graphNode} node graph node
  60. * @param {Array} nodelist list of graph nodes
  61. */
  62. highlightPath: function(node, nodelist) {
  63. let highlightSources, highlightTargets, el;
  64. highlightSources = function(node, srcNode) {
  65. var target, edge, el, z, lenZ, cl;
  66. lenZ = srcNode.svgEdges.length;
  67. for (z = 0; z < lenZ; z++) {
  68. target = srcNode.trgNodes[z];
  69. if (target[0] === node.x && target[1] === node.y) { // order of trgNode and svgEdges is same
  70. edge = srcNode.svgEdges[z];
  71. svgCssUtil.toggle(edge, 'srcEdgeHighlighted');
  72. if (srcNode.svgNode) {
  73. el = srcNode.svgNode;
  74. svgCssUtil.toggle(el, 'srcNodeHighlighted');
  75. }
  76. }
  77. }
  78. };
  79. highlightTargets = function(node) {
  80. let edge, el, z, lenZ;
  81. lenZ = node.trgNodes.length;
  82. for (z = 0; z < lenZ; z++) {
  83. edge = node.svgEdges[z];
  84. svgCssUtil.toggle(edge, 'targetEdgeHighlighted');
  85. el = nodelist[node.trgNodes[z][1]][node.trgNodes[z][0]];
  86. if (el.virt === false) {
  87. el = el.svgNode;
  88. svgCssUtil.toggle(el, 'trgNodeHighlighted');
  89. }
  90. }
  91. };
  92. el = node.svgNode; //.getElementsByTagName('circle')[0];
  93. svgCssUtil.toggle(el, 'nodeHighlighted');
  94. el = el.getElementsByTagName('circle')[0];
  95. el.setAttribute('r', el.getAttribute('r') === '5' ? 8: 5);
  96. highlightTargets(node);
  97. this.searchByTargets(node, nodelist, highlightTargets);
  98. this.searchBySources(node, nodelist, highlightSources);
  99. }
  100. };