node.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { capitalize } from '../../../utils/strings.mjs';
  2. import { isEmpty, isUndefined } from '../../../utils/types.mjs';
  3. import { isFunction, isArray } from '@vue/shared';
  4. let uid = 0;
  5. const calculatePathNodes = (node) => {
  6. const nodes = [node];
  7. let { parent } = node;
  8. while (parent) {
  9. nodes.unshift(parent);
  10. parent = parent.parent;
  11. }
  12. return nodes;
  13. };
  14. class Node {
  15. constructor(data, config, parent, root = false) {
  16. this.data = data;
  17. this.config = config;
  18. this.parent = parent;
  19. this.root = root;
  20. this.uid = uid++;
  21. this.checked = false;
  22. this.indeterminate = false;
  23. this.loading = false;
  24. const { value: valueKey, label: labelKey, children: childrenKey } = config;
  25. const childrenData = data[childrenKey];
  26. const pathNodes = calculatePathNodes(this);
  27. this.level = root ? 0 : parent ? parent.level + 1 : 1;
  28. this.value = data[valueKey];
  29. this.label = data[labelKey];
  30. this.pathNodes = pathNodes;
  31. this.pathValues = pathNodes.map((node) => node.value);
  32. this.pathLabels = pathNodes.map((node) => node.label);
  33. this.childrenData = childrenData;
  34. this.children = (childrenData || []).map((child) => new Node(child, config, this));
  35. this.loaded = !config.lazy || this.isLeaf || !isEmpty(childrenData);
  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(event, ...args) {
  69. const handlerName = `onParent${capitalize(event)}`;
  70. this.children.forEach((child) => {
  71. if (child) {
  72. child.broadcast(event, ...args);
  73. child[handlerName] && child[handlerName](...args);
  74. }
  75. });
  76. }
  77. emit(event, ...args) {
  78. const { parent } = this;
  79. const handlerName = `onChild${capitalize(event)}`;
  80. if (parent) {
  81. parent[handlerName] && parent[handlerName](...args);
  82. parent.emit(event, ...args);
  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("check", checked);
  113. this.setCheckState(checked);
  114. this.emit("check");
  115. }
  116. }
  117. }
  118. export { Node as default };
  119. //# sourceMappingURL=node.mjs.map