traversalHelper.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * Traverse the tree from bottom to top and do something
  3. * @param {module:echarts/data/Tree~TreeNode} root The real root of the tree
  4. * @param {Function} callback
  5. */
  6. function eachAfter(root, callback, separation) {
  7. var nodes = [root];
  8. var next = [];
  9. var node;
  10. while (node = nodes.pop()) {
  11. // jshint ignore:line
  12. next.push(node);
  13. if (node.isExpand) {
  14. var children = node.children;
  15. if (children.length) {
  16. for (var i = 0; i < children.length; i++) {
  17. nodes.push(children[i]);
  18. }
  19. }
  20. }
  21. }
  22. while (node = next.pop()) {
  23. // jshint ignore:line
  24. callback(node, separation);
  25. }
  26. }
  27. /**
  28. * Traverse the tree from top to bottom and do something
  29. * @param {module:echarts/data/Tree~TreeNode} root The real root of the tree
  30. * @param {Function} callback
  31. */
  32. function eachBefore(root, callback) {
  33. var nodes = [root];
  34. var node;
  35. while (node = nodes.pop()) {
  36. // jshint ignore:line
  37. callback(node);
  38. if (node.isExpand) {
  39. var children = node.children;
  40. if (children.length) {
  41. for (var i = children.length - 1; i >= 0; i--) {
  42. nodes.push(children[i]);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. exports.eachAfter = eachAfter;
  49. exports.eachBefore = eachBefore;