node.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(
  34. (child) => new Node(child, config, this)
  35. );
  36. this.loaded = !config.lazy || this.isLeaf || !isEmpty(childrenData);
  37. this.text = "";
  38. }
  39. get isDisabled() {
  40. const { data, parent, config } = this;
  41. const { disabled, checkStrictly } = config;
  42. const isDisabled = isFunction(disabled) ? disabled(data, this) : !!data[disabled];
  43. return isDisabled || !checkStrictly && !!(parent == null ? void 0 : parent.isDisabled);
  44. }
  45. get isLeaf() {
  46. const { data, config, childrenData, loaded } = this;
  47. const { lazy, leaf } = config;
  48. const isLeaf = isFunction(leaf) ? leaf(data, this) : data[leaf];
  49. return isUndefined(isLeaf) ? lazy && !loaded ? false : !(isArray(childrenData) && childrenData.length) : !!isLeaf;
  50. }
  51. get valueByOption() {
  52. return this.config.emitPath ? this.pathValues : this.value;
  53. }
  54. appendChild(childData) {
  55. const { childrenData, children } = this;
  56. const node = new Node(childData, this.config, this);
  57. if (isArray(childrenData)) {
  58. childrenData.push(childData);
  59. } else {
  60. this.childrenData = [childData];
  61. }
  62. children.push(node);
  63. return node;
  64. }
  65. calcText(allLevels, separator) {
  66. const text = allLevels ? this.pathLabels.join(separator) : this.label;
  67. this.text = text;
  68. return text;
  69. }
  70. broadcast(checked) {
  71. this.children.forEach((child) => {
  72. var _a;
  73. if (child) {
  74. child.broadcast(checked);
  75. (_a = child.onParentCheck) == null ? void 0 : _a.call(child, checked);
  76. }
  77. });
  78. }
  79. emit() {
  80. var _a;
  81. const { parent } = this;
  82. if (parent) {
  83. (_a = parent.onChildCheck) == null ? void 0 : _a.call(parent);
  84. parent.emit();
  85. }
  86. }
  87. onParentCheck(checked) {
  88. if (!this.isDisabled) {
  89. this.setCheckState(checked);
  90. }
  91. }
  92. onChildCheck() {
  93. const { children } = this;
  94. const validChildren = children.filter((child) => !child.isDisabled);
  95. const checked = validChildren.length ? validChildren.every((child) => child.checked) : false;
  96. this.setCheckState(checked);
  97. }
  98. setCheckState(checked) {
  99. const totalNum = this.children.length;
  100. const checkedNum = this.children.reduce((c, p) => {
  101. const num = p.checked ? 1 : p.indeterminate ? 0.5 : 0;
  102. return c + num;
  103. }, 0);
  104. this.checked = this.loaded && this.children.filter((child) => !child.isDisabled).every((child) => child.loaded && child.checked) && checked;
  105. this.indeterminate = this.loaded && checkedNum !== totalNum && checkedNum > 0;
  106. }
  107. doCheck(checked) {
  108. if (this.checked === checked)
  109. return;
  110. const { checkStrictly, multiple } = this.config;
  111. if (checkStrictly || !multiple) {
  112. this.checked = checked;
  113. } else {
  114. this.broadcast(checked);
  115. this.setCheckState(checked);
  116. this.emit();
  117. }
  118. }
  119. }
  120. export { Node as default };
  121. //# sourceMappingURL=node.mjs.map