node.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Node = void 0;
  4. /**
  5. * BFS nodes and execute callback.
  6. */
  7. function bfs(node, callback) {
  8. const discovered = [node];
  9. while (discovered.length) {
  10. const currentNode = discovered.shift();
  11. callback && callback(currentNode);
  12. const children = currentNode.children || [];
  13. for (const child of children) {
  14. discovered.push(child);
  15. }
  16. }
  17. }
  18. /**
  19. * Hierarchy container.
  20. */
  21. class Node {
  22. constructor(value = {}, type) {
  23. // The parent node.
  24. this.parentNode = null;
  25. // The children nodes.
  26. this.children = [];
  27. // The index of parent children.
  28. this.index = 0;
  29. this.type = type;
  30. this.value = value;
  31. }
  32. /**
  33. * Apply specified transform to current value. Mount the node
  34. * to replace the original one in the tree and then return it.
  35. */
  36. map(transform = (x) => x) {
  37. const newValue = transform(this.value);
  38. this.value = newValue;
  39. return this;
  40. }
  41. /**
  42. * Set or get the specified attribute. It the value is specified, update
  43. * the attribute of current value and return the node. Otherwise
  44. * return the the attribute of current value.
  45. */
  46. attr(key, value) {
  47. if (arguments.length === 1)
  48. return this.value[key];
  49. return this.map((v) => ((v[key] = value), v));
  50. }
  51. /**
  52. * Create a new node and append to children nodes.
  53. */
  54. append(Ctor) {
  55. const node = new Ctor({});
  56. node.children = [];
  57. this.push(node);
  58. return node;
  59. }
  60. push(node) {
  61. node.parentNode = this;
  62. node.index = this.children.length;
  63. this.children.push(node);
  64. return this;
  65. }
  66. /**
  67. * Remove current node from parentNode.
  68. */
  69. remove() {
  70. const parent = this.parentNode;
  71. if (parent) {
  72. const { children } = parent;
  73. const index = children.findIndex((item) => item === this);
  74. children.splice(index, 1);
  75. }
  76. return this;
  77. }
  78. getNodeByKey(key) {
  79. let targetNode = null;
  80. const callback = (node) => {
  81. if (key === node.attr('key')) {
  82. targetNode = node;
  83. }
  84. };
  85. bfs(this, callback);
  86. return targetNode;
  87. }
  88. getNodesByType(type) {
  89. const nodes = [];
  90. const callback = (node) => {
  91. if (type === node.type) {
  92. nodes.push(node);
  93. }
  94. };
  95. bfs(this, callback);
  96. return nodes;
  97. }
  98. getNodeByType(type) {
  99. let node = null;
  100. bfs(this, (current) => {
  101. if (node)
  102. return;
  103. if (type === current.type)
  104. node = current;
  105. });
  106. return node;
  107. }
  108. /**
  109. * Apply specified callback to the node value.
  110. */
  111. call(callback, ...params) {
  112. callback(this.map(), ...params);
  113. return this;
  114. }
  115. getRoot() {
  116. // Find the root chart and render.
  117. let root = this;
  118. while (root && root.parentNode) {
  119. root = root.parentNode;
  120. }
  121. return root;
  122. }
  123. }
  124. exports.Node = Node;
  125. //# sourceMappingURL=node.js.map