node.mjs 4.1 KB

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