3ae22ce2dce2120cff46a460ce6b77e7b4feccf01f79d56a0708d90a444d8044373472a72c2e5b6b5c029206f9fc5aa4abfbc89f269fc777f11b3465105825 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { ref, getCurrentInstance, computed, watch, unref } from 'vue';
  2. import { getRowIdentity, walkTreeNode } from '../util.mjs';
  3. import { isArray } from '@vue/shared';
  4. import { isUndefined } from '../../../../utils/types.mjs';
  5. function useTree(watcherData) {
  6. const expandRowKeys = ref([]);
  7. const treeData = ref({});
  8. const indent = ref(16);
  9. const lazy = ref(false);
  10. const lazyTreeNodeMap = ref({});
  11. const lazyColumnIdentifier = ref("hasChildren");
  12. const childrenColumnName = ref("children");
  13. const checkStrictly = ref(false);
  14. const instance = getCurrentInstance();
  15. const normalizedData = computed(() => {
  16. if (!watcherData.rowKey.value)
  17. return {};
  18. const data = watcherData.data.value || [];
  19. return normalize(data);
  20. });
  21. const normalizedLazyNode = computed(() => {
  22. const rowKey = watcherData.rowKey.value;
  23. const keys = Object.keys(lazyTreeNodeMap.value);
  24. const res = {};
  25. if (!keys.length)
  26. return res;
  27. keys.forEach((key) => {
  28. if (lazyTreeNodeMap.value[key].length) {
  29. const item = { children: [] };
  30. lazyTreeNodeMap.value[key].forEach((row) => {
  31. const currentRowKey = getRowIdentity(row, rowKey);
  32. item.children.push(currentRowKey);
  33. if (row[lazyColumnIdentifier.value] && !res[currentRowKey]) {
  34. res[currentRowKey] = { children: [] };
  35. }
  36. });
  37. res[key] = item;
  38. }
  39. });
  40. return res;
  41. });
  42. const normalize = (data) => {
  43. const rowKey = watcherData.rowKey.value;
  44. const res = {};
  45. walkTreeNode(data, (parent, children, level) => {
  46. const parentId = getRowIdentity(parent, rowKey);
  47. if (isArray(children)) {
  48. res[parentId] = {
  49. children: children.map((row) => getRowIdentity(row, rowKey)),
  50. level
  51. };
  52. } else if (lazy.value) {
  53. res[parentId] = {
  54. children: [],
  55. lazy: true,
  56. level
  57. };
  58. }
  59. }, childrenColumnName.value, lazyColumnIdentifier.value, lazy.value);
  60. return res;
  61. };
  62. const updateTreeData = (ifChangeExpandRowKeys = false, ifExpandAll) => {
  63. var _a, _b;
  64. ifExpandAll || (ifExpandAll = (_a = instance.store) == null ? void 0 : _a.states.defaultExpandAll.value);
  65. const nested = normalizedData.value;
  66. const normalizedLazyNode_ = normalizedLazyNode.value;
  67. const keys = Object.keys(nested);
  68. const newTreeData = {};
  69. if (keys.length) {
  70. const oldTreeData = unref(treeData);
  71. const rootLazyRowKeys = [];
  72. const getExpanded = (oldValue, key) => {
  73. if (ifChangeExpandRowKeys) {
  74. if (expandRowKeys.value) {
  75. return ifExpandAll || expandRowKeys.value.includes(key);
  76. } else {
  77. return !!(ifExpandAll || (oldValue == null ? void 0 : oldValue.expanded));
  78. }
  79. } else {
  80. const included = ifExpandAll || expandRowKeys.value && expandRowKeys.value.includes(key);
  81. return !!((oldValue == null ? void 0 : oldValue.expanded) || included);
  82. }
  83. };
  84. keys.forEach((key) => {
  85. const oldValue = oldTreeData[key];
  86. const newValue = { ...nested[key] };
  87. newValue.expanded = getExpanded(oldValue, key);
  88. if (newValue.lazy) {
  89. const { loaded = false, loading = false } = oldValue || {};
  90. newValue.loaded = !!loaded;
  91. newValue.loading = !!loading;
  92. rootLazyRowKeys.push(key);
  93. }
  94. newTreeData[key] = newValue;
  95. });
  96. const lazyKeys = Object.keys(normalizedLazyNode_);
  97. if (lazy.value && lazyKeys.length && rootLazyRowKeys.length) {
  98. lazyKeys.forEach((key) => {
  99. var _a2;
  100. const oldValue = oldTreeData[key];
  101. const lazyNodeChildren = normalizedLazyNode_[key].children;
  102. if (rootLazyRowKeys.includes(key)) {
  103. if (((_a2 = newTreeData[key].children) == null ? void 0 : _a2.length) !== 0) {
  104. throw new Error("[ElTable]children must be an empty array.");
  105. }
  106. newTreeData[key].children = lazyNodeChildren;
  107. } else {
  108. const { loaded = false, loading = false } = oldValue || {};
  109. newTreeData[key] = {
  110. lazy: true,
  111. loaded: !!loaded,
  112. loading: !!loading,
  113. expanded: getExpanded(oldValue, key),
  114. children: lazyNodeChildren,
  115. level: void 0
  116. };
  117. }
  118. });
  119. }
  120. }
  121. treeData.value = newTreeData;
  122. (_b = instance.store) == null ? void 0 : _b.updateTableScrollY();
  123. };
  124. watch(() => expandRowKeys.value, () => {
  125. updateTreeData(true);
  126. });
  127. watch(() => normalizedData.value, () => {
  128. updateTreeData();
  129. });
  130. watch(() => normalizedLazyNode.value, () => {
  131. updateTreeData();
  132. });
  133. const updateTreeExpandKeys = (value) => {
  134. expandRowKeys.value = value;
  135. updateTreeData();
  136. };
  137. const isUseLazy = (data) => {
  138. return lazy.value && data && "loaded" in data && !data.loaded;
  139. };
  140. const toggleTreeExpansion = (row, expanded) => {
  141. instance.store.assertRowKey();
  142. const rowKey = watcherData.rowKey.value;
  143. const id = getRowIdentity(row, rowKey);
  144. const data = id && treeData.value[id];
  145. if (id && data && "expanded" in data) {
  146. const oldExpanded = data.expanded;
  147. expanded = isUndefined(expanded) ? !data.expanded : expanded;
  148. treeData.value[id].expanded = expanded;
  149. if (oldExpanded !== expanded) {
  150. instance.emit("expand-change", row, expanded);
  151. }
  152. isUseLazy(data) && loadData(row, id, data);
  153. instance.store.updateTableScrollY();
  154. }
  155. };
  156. const loadOrToggle = (row) => {
  157. instance.store.assertRowKey();
  158. const rowKey = watcherData.rowKey.value;
  159. const id = getRowIdentity(row, rowKey);
  160. const data = treeData.value[id];
  161. if (isUseLazy(data)) {
  162. loadData(row, id, data);
  163. } else {
  164. toggleTreeExpansion(row, void 0);
  165. }
  166. };
  167. const loadData = (row, key, treeNode) => {
  168. const { load } = instance.props;
  169. if (load && !treeData.value[key].loaded) {
  170. treeData.value[key].loading = true;
  171. load(row, treeNode, (data) => {
  172. if (!isArray(data)) {
  173. throw new TypeError("[ElTable] data must be an array");
  174. }
  175. treeData.value[key].loading = false;
  176. treeData.value[key].loaded = true;
  177. treeData.value[key].expanded = true;
  178. if (data.length) {
  179. lazyTreeNodeMap.value[key] = data;
  180. }
  181. instance.emit("expand-change", row, true);
  182. });
  183. }
  184. };
  185. const updateKeyChildren = (key, data) => {
  186. const { lazy: lazy2, rowKey } = instance.props;
  187. if (!lazy2)
  188. return;
  189. if (!rowKey)
  190. throw new Error("[Table] rowKey is required in updateKeyChild");
  191. if (lazyTreeNodeMap.value[key]) {
  192. lazyTreeNodeMap.value[key] = data;
  193. }
  194. };
  195. return {
  196. loadData,
  197. loadOrToggle,
  198. toggleTreeExpansion,
  199. updateTreeExpandKeys,
  200. updateTreeData,
  201. updateKeyChildren,
  202. normalize,
  203. states: {
  204. expandRowKeys,
  205. treeData,
  206. indent,
  207. lazy,
  208. lazyTreeNodeMap,
  209. lazyColumnIdentifier,
  210. childrenColumnName,
  211. checkStrictly
  212. }
  213. };
  214. }
  215. export { useTree as default };
  216. //# sourceMappingURL=tree.mjs.map