f04cde735667e072c837d9da6ab928a00988ee146cfe8f293a3acf29daffebd3d8643138e63839942e9840a4323b1b9e0cb671879d1485dfae22405338ebe5 6.2 KB

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