dff4acf2145b505d00d0b354f658b6b07a38560a53be6df99199c5ef3eb5727aa61211a9962e7abf2d433e29f5e3be0110ddcb5dcd39697448168be3964fa5 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { getCurrentInstance, unref, nextTick } from 'vue';
  2. import { isNull } from 'lodash-unified';
  3. import useWatcher from './watcher.mjs';
  4. import { useNamespace } from '../../../../hooks/use-namespace/index.mjs';
  5. function replaceColumn(array, column) {
  6. return array.map((item) => {
  7. var _a;
  8. if (item.id === column.id) {
  9. return column;
  10. } else if ((_a = item.children) == null ? void 0 : _a.length) {
  11. item.children = replaceColumn(item.children, column);
  12. }
  13. return item;
  14. });
  15. }
  16. function sortColumn(array) {
  17. array.forEach((item) => {
  18. var _a, _b;
  19. item.no = (_a = item.getColumnIndex) == null ? void 0 : _a.call(item);
  20. if ((_b = item.children) == null ? void 0 : _b.length) {
  21. sortColumn(item.children);
  22. }
  23. });
  24. array.sort((cur, pre) => cur.no - pre.no);
  25. }
  26. function useStore() {
  27. const instance = getCurrentInstance();
  28. const watcher = useWatcher();
  29. const ns = useNamespace("table");
  30. const mutations = {
  31. setData(states, data) {
  32. const dataInstanceChanged = unref(states._data) !== data;
  33. states.data.value = data;
  34. states._data.value = data;
  35. instance.store.execQuery();
  36. instance.store.updateCurrentRowData();
  37. instance.store.updateExpandRows();
  38. instance.store.updateTreeData(instance.store.states.defaultExpandAll.value);
  39. if (unref(states.reserveSelection)) {
  40. instance.store.assertRowKey();
  41. } else {
  42. if (dataInstanceChanged) {
  43. instance.store.clearSelection();
  44. } else {
  45. instance.store.cleanSelection();
  46. }
  47. }
  48. instance.store.updateAllSelected();
  49. if (instance.$ready) {
  50. instance.store.scheduleLayout();
  51. }
  52. },
  53. insertColumn(states, column, parent, updateColumnOrder) {
  54. var _a;
  55. const array = unref(states._columns);
  56. let newColumns = [];
  57. if (!parent) {
  58. array.push(column);
  59. newColumns = array;
  60. } else {
  61. if (parent && !parent.children) {
  62. parent.children = [];
  63. }
  64. (_a = parent.children) == null ? void 0 : _a.push(column);
  65. newColumns = replaceColumn(array, parent);
  66. }
  67. sortColumn(newColumns);
  68. states._columns.value = newColumns;
  69. states.updateOrderFns.push(updateColumnOrder);
  70. if (column.type === "selection") {
  71. states.selectable.value = column.selectable;
  72. states.reserveSelection.value = column.reserveSelection;
  73. }
  74. if (instance.$ready) {
  75. instance.store.updateColumns();
  76. instance.store.scheduleLayout();
  77. }
  78. },
  79. updateColumnOrder(states, column) {
  80. var _a;
  81. const newColumnIndex = (_a = column.getColumnIndex) == null ? void 0 : _a.call(column);
  82. if (newColumnIndex === column.no)
  83. return;
  84. sortColumn(states._columns.value);
  85. if (instance.$ready) {
  86. instance.store.updateColumns();
  87. }
  88. },
  89. removeColumn(states, column, parent, updateColumnOrder) {
  90. var _a;
  91. const array = unref(states._columns) || [];
  92. if (parent) {
  93. (_a = parent.children) == null ? void 0 : _a.splice(parent.children.findIndex((item) => item.id === column.id), 1);
  94. nextTick(() => {
  95. var _a2;
  96. if (((_a2 = parent.children) == null ? void 0 : _a2.length) === 0) {
  97. delete parent.children;
  98. }
  99. });
  100. states._columns.value = replaceColumn(array, parent);
  101. } else {
  102. const index = array.indexOf(column);
  103. if (index > -1) {
  104. array.splice(index, 1);
  105. states._columns.value = array;
  106. }
  107. }
  108. const updateFnIndex = states.updateOrderFns.indexOf(updateColumnOrder);
  109. updateFnIndex > -1 && states.updateOrderFns.splice(updateFnIndex, 1);
  110. if (instance.$ready) {
  111. instance.store.updateColumns();
  112. instance.store.scheduleLayout();
  113. }
  114. },
  115. sort(states, options) {
  116. const { prop, order, init } = options;
  117. if (prop) {
  118. const column = unref(states.columns).find((column2) => column2.property === prop);
  119. if (column) {
  120. column.order = order;
  121. instance.store.updateSort(column, prop, order);
  122. instance.store.commit("changeSortCondition", { init });
  123. }
  124. }
  125. },
  126. changeSortCondition(states, options) {
  127. const { sortingColumn, sortProp, sortOrder } = states;
  128. const columnValue = unref(sortingColumn), propValue = unref(sortProp), orderValue = unref(sortOrder);
  129. if (isNull(orderValue)) {
  130. states.sortingColumn.value = null;
  131. states.sortProp.value = null;
  132. }
  133. const ignore = { filter: true };
  134. instance.store.execQuery(ignore);
  135. if (!options || !(options.silent || options.init)) {
  136. instance.emit("sort-change", {
  137. column: columnValue,
  138. prop: propValue,
  139. order: orderValue
  140. });
  141. }
  142. instance.store.updateTableScrollY();
  143. },
  144. filterChange(_states, options) {
  145. const { column, values, silent } = options;
  146. const newFilters = instance.store.updateFilters(column, values);
  147. instance.store.execQuery();
  148. if (!silent) {
  149. instance.emit("filter-change", newFilters);
  150. }
  151. instance.store.updateTableScrollY();
  152. },
  153. toggleAllSelection() {
  154. var _a, _b;
  155. (_b = (_a = instance.store).toggleAllSelection) == null ? void 0 : _b.call(_a);
  156. },
  157. rowSelectedChanged(_states, row) {
  158. instance.store.toggleRowSelection(row);
  159. instance.store.updateAllSelected();
  160. },
  161. setHoverRow(states, row) {
  162. states.hoverRow.value = row;
  163. },
  164. setCurrentRow(_states, row) {
  165. instance.store.updateCurrentRow(row);
  166. }
  167. };
  168. const commit = function(name, ...args) {
  169. const mutations2 = instance.store.mutations;
  170. if (mutations2[name]) {
  171. mutations2[name].apply(instance, [
  172. instance.store.states,
  173. ...args
  174. ]);
  175. } else {
  176. throw new Error(`Action not found: ${name}`);
  177. }
  178. };
  179. const updateTableScrollY = function() {
  180. nextTick(() => instance.layout.updateScrollY.apply(instance.layout));
  181. };
  182. return {
  183. ns,
  184. ...watcher,
  185. mutations,
  186. commit,
  187. updateTableScrollY
  188. };
  189. }
  190. export { useStore as default };
  191. //# sourceMappingURL=index.mjs.map