util.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getAllNodes = exports.getField = exports.NODE_ANCESTORS_FIELD = exports.CHILD_NODE_COUNT = exports.NODE_INDEX_FIELD = void 0;
  4. var util_1 = require("@antv/util");
  5. /** export 一些字段常量 */
  6. /** 在同层级,同一父节点下的节点索引顺序 */
  7. exports.NODE_INDEX_FIELD = 'nodeIndex';
  8. /** child 节点数量 */
  9. exports.CHILD_NODE_COUNT = 'childNodeCount';
  10. /** 节点的祖先节点 */
  11. exports.NODE_ANCESTORS_FIELD = 'nodeAncestor';
  12. var INVALID_FIELD_ERR_MSG = 'Invalid field: it must be a string!';
  13. function getField(options, defaultField) {
  14. var field = options.field, fields = options.fields;
  15. if ((0, util_1.isString)(field)) {
  16. return field;
  17. }
  18. if ((0, util_1.isArray)(field)) {
  19. console.warn(INVALID_FIELD_ERR_MSG);
  20. return field[0];
  21. }
  22. console.warn("".concat(INVALID_FIELD_ERR_MSG, " will try to get fields instead."));
  23. if ((0, util_1.isString)(fields)) {
  24. return fields;
  25. }
  26. if ((0, util_1.isArray)(fields) && fields.length) {
  27. return fields[0];
  28. }
  29. if (defaultField) {
  30. return defaultField;
  31. }
  32. throw new TypeError(INVALID_FIELD_ERR_MSG);
  33. }
  34. exports.getField = getField;
  35. function getAllNodes(root) {
  36. var nodes = [];
  37. if (root && root.each) {
  38. var parent_1;
  39. var index_1;
  40. // d3-hierarchy: Invokes the specified function for node and each descendant in **breadth-first order**
  41. root.each(function (node) {
  42. var _a, _b;
  43. if (node.parent !== parent_1) {
  44. parent_1 = node.parent;
  45. index_1 = 0;
  46. }
  47. else {
  48. index_1 += 1;
  49. }
  50. var ancestors = (0, util_1.filter)((((_a = node.ancestors) === null || _a === void 0 ? void 0 : _a.call(node)) || []).map(function (d) { return nodes.find(function (n) { return n.name === d.name; }) || d; }), function (_a) {
  51. var depth = _a.depth;
  52. return depth > 0 && depth < node.depth;
  53. });
  54. node[exports.NODE_ANCESTORS_FIELD] = ancestors;
  55. node[exports.CHILD_NODE_COUNT] = ((_b = node.children) === null || _b === void 0 ? void 0 : _b.length) || 0;
  56. node[exports.NODE_INDEX_FIELD] = index_1;
  57. nodes.push(node);
  58. });
  59. }
  60. else if (root && root.eachNode) {
  61. // @antv/hierarchy
  62. root.eachNode(function (node) {
  63. nodes.push(node);
  64. });
  65. }
  66. return nodes;
  67. }
  68. exports.getAllNodes = getAllNodes;
  69. //# sourceMappingURL=util.js.map