helper.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { isObject } from "../../../../utils/types.mjs";
  2. import useStore from "./index.mjs";
  3. import { debounce } from "lodash-unified";
  4. import { watch } from "vue";
  5. //#region ../../packages/components/table/src/store/helper.ts
  6. const InitialStateMap = {
  7. rowKey: "rowKey",
  8. defaultExpandAll: "defaultExpandAll",
  9. rowExpandable: "rowExpandable",
  10. selectOnIndeterminate: "selectOnIndeterminate",
  11. indent: "indent",
  12. lazy: "lazy",
  13. ["treeProps.hasChildren"]: {
  14. key: "lazyColumnIdentifier",
  15. default: "hasChildren"
  16. },
  17. ["treeProps.children"]: {
  18. key: "childrenColumnName",
  19. default: "children"
  20. },
  21. ["treeProps.checkStrictly"]: {
  22. key: "checkStrictly",
  23. default: false
  24. }
  25. };
  26. function createStore(table, props) {
  27. if (!table) throw new Error("Table is required.");
  28. const store = useStore();
  29. store.toggleAllSelection = debounce(store._toggleAllSelection, 10);
  30. Object.keys(InitialStateMap).forEach((key) => {
  31. handleValue(getArrKeysValue(props, key), key, store);
  32. });
  33. proxyTableProps(store, props);
  34. return store;
  35. }
  36. function proxyTableProps(store, props) {
  37. Object.keys(InitialStateMap).forEach((key) => {
  38. watch(() => getArrKeysValue(props, key), (value) => {
  39. handleValue(value, key, store);
  40. });
  41. });
  42. }
  43. function handleValue(value, propsKey, store) {
  44. let newVal = value;
  45. let storeKey = InitialStateMap[propsKey];
  46. if (isObject(storeKey)) {
  47. newVal = newVal || storeKey.default;
  48. storeKey = storeKey.key;
  49. }
  50. store.states[storeKey].value = newVal;
  51. }
  52. function getArrKeysValue(props, key) {
  53. if (key.includes(".")) {
  54. const keyList = key.split(".");
  55. let value = props;
  56. keyList.forEach((k) => {
  57. value = value[k];
  58. });
  59. return value;
  60. } else return props[key];
  61. }
  62. //#endregion
  63. export { createStore };
  64. //# sourceMappingURL=helper.mjs.map