cc895419ee32d2cc584b9543acf232915232435718af3b0086186f1c4fbe9c12725932265df96c751d41c90bd8ddae105ca0461ad039b42a19cff6be9fa707 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { isEmpty, isUndefined } from '../../../utils/types.mjs';
  2. import { isFunction, isArray } from '@vue/shared';
  3. let uid = 0;
  4. const calculatePathNodes = (node) => {
  5. const nodes = [node];
  6. let { parent } = node;
  7. while (parent) {
  8. nodes.unshift(parent);
  9. parent = parent.parent;
  10. }
  11. return nodes;
  12. };
  13. class Node {
  14. constructor(data, config, parent, root = false) {
  15. this.data = data;
  16. this.config = config;
  17. this.parent = parent;
  18. this.root = root;
  19. this.uid = uid++;
  20. this.checked = false;
  21. this.indeterminate = false;
  22. this.loading = false;
  23. const { value: valueKey, label: labelKey, children: childrenKey } = config;
  24. const childrenData = data[childrenKey];
  25. const pathNodes = calculatePathNodes(this);
  26. this.level = root ? 0 : parent ? parent.level + 1 : 1;
  27. this.value = data[valueKey];
  28. this.label = data[labelKey];
  29. this.pathNodes = pathNodes;
  30. this.pathValues = pathNodes.map((node) => node.value);
  31. this.pathLabels = pathNodes.map((node) => node.label);
  32. this.childrenData = childrenData;
  33. this.children = (childrenData || []).map((child) => new Node(child, config, this));
  34. this.loaded = !config.lazy || this.isLeaf || !isEmpty(childrenData);
  35. this.text = "";
  36. }
  37. get isDisabled() {
  38. const { data, parent, config } = this;
  39. const { disabled, checkStrictly } = config;
  40. const isDisabled = isFunction(disabled) ? disabled(data, this) : !!data[disabled];
  41. return isDisabled || !checkStrictly && !!(parent == null ? void 0 : parent.isDisabled);
  42. }
  43. get isLeaf() {
  44. const { data, config, childrenData, loaded } = this;
  45. const { lazy, leaf } = config;
  46. const isLeaf = isFunction(leaf) ? leaf(data, this) : data[leaf];
  47. return isUndefined(isLeaf) ? lazy && !loaded ? false : !(isArray(childrenData) && childrenData.length) : !!isLeaf;
  48. }
  49. get valueByOption() {
  50. return this.config.emitPath ? this.pathValues : this.value;
  51. }
  52. appendChild(childData) {
  53. const { childrenData, children } = this;
  54. const node = new Node(childData, this.config, this);
  55. if (isArray(childrenData)) {
  56. childrenData.push(childData);
  57. } else {
  58. this.childrenData = [childData];
  59. }
  60. children.push(node);
  61. return node;
  62. }
  63. calcText(allLevels, separator) {
  64. const text = allLevels ? this.pathLabels.join(separator) : this.label;
  65. this.text = text;
  66. return text;
  67. }
  68. broadcast(checked) {
  69. this.children.forEach((child) => {
  70. var _a;
  71. if (child) {
  72. child.broadcast(checked);
  73. (_a = child.onParentCheck) == null ? void 0 : _a.call(child, checked);
  74. }
  75. });
  76. }
  77. emit() {
  78. var _a;
  79. const { parent } = this;
  80. if (parent) {
  81. (_a = parent.onChildCheck) == null ? void 0 : _a.call(parent);
  82. parent.emit();
  83. }
  84. }
  85. onParentCheck(checked) {
  86. if (!this.isDisabled) {
  87. this.setCheckState(checked);
  88. }
  89. }
  90. onChildCheck() {
  91. const { children } = this;
  92. const validChildren = children.filter((child) => !child.isDisabled);
  93. const checked = validChildren.length ? validChildren.every((child) => child.checked) : false;
  94. this.setCheckState(checked);
  95. }
  96. setCheckState(checked) {
  97. const totalNum = this.children.length;
  98. const checkedNum = this.children.reduce((c, p) => {
  99. const num = p.checked ? 1 : p.indeterminate ? 0.5 : 0;
  100. return c + num;
  101. }, 0);
  102. this.checked = this.loaded && this.children.filter((child) => !child.isDisabled).every((child) => child.loaded && child.checked) && checked;
  103. this.indeterminate = this.loaded && checkedNum !== totalNum && checkedNum > 0;
  104. }
  105. doCheck(checked) {
  106. if (this.checked === checked)
  107. return;
  108. const { checkStrictly, multiple } = this.config;
  109. if (checkStrictly || !multiple) {
  110. this.checked = checked;
  111. } else {
  112. this.broadcast(checked);
  113. this.setCheckState(checked);
  114. this.emit();
  115. }
  116. }
  117. }
  118. export { Node as default };
  119. //# sourceMappingURL=node.mjs.map