api.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import type { ExtendedFormApi } from '@vben-core/form-ui';
  2. import type { VxeGridInstance } from 'vxe-table';
  3. import type { VxeGridProps } from './types';
  4. import { toRaw } from 'vue';
  5. import { Store } from '@vben-core/shared/store';
  6. import {
  7. bindMethods,
  8. isFunction,
  9. mergeWithArrayOverride,
  10. StateHandler,
  11. } from '@vben-core/shared/utils';
  12. function getDefaultState(): VxeGridProps {
  13. return {
  14. class: '',
  15. gridClass: '',
  16. gridOptions: {},
  17. gridEvents: {},
  18. formOptions: undefined,
  19. };
  20. }
  21. export class VxeGridApi {
  22. private isMounted = false;
  23. private stateHandler: StateHandler;
  24. public formApi = {} as ExtendedFormApi;
  25. // private prevState: null | VxeGridProps = null;
  26. public grid = {} as VxeGridInstance;
  27. public state: null | VxeGridProps = null;
  28. public store: Store<VxeGridProps>;
  29. constructor(options: VxeGridProps = {}) {
  30. const storeState = { ...options };
  31. const defaultState = getDefaultState();
  32. this.store = new Store<VxeGridProps>(
  33. mergeWithArrayOverride(storeState, defaultState),
  34. {
  35. onUpdate: () => {
  36. // this.prevState = this.state;
  37. this.state = this.store.state;
  38. },
  39. },
  40. );
  41. this.state = this.store.state;
  42. this.stateHandler = new StateHandler();
  43. bindMethods(this);
  44. }
  45. mount(instance: null | VxeGridInstance, formApi: ExtendedFormApi) {
  46. if (!this.isMounted && instance) {
  47. this.grid = instance;
  48. this.formApi = formApi;
  49. this.stateHandler.setConditionTrue();
  50. this.isMounted = true;
  51. }
  52. }
  53. async query(params: Record<string, any> = {}) {
  54. try {
  55. await this.grid.commitProxy('query', toRaw(params));
  56. } catch (error) {
  57. console.error('Error occurred while querying:', error);
  58. }
  59. }
  60. async reload(params: Record<string, any> = {}) {
  61. try {
  62. await this.grid.commitProxy('reload', toRaw(params));
  63. } catch (error) {
  64. console.error('Error occurred while reloading:', error);
  65. }
  66. }
  67. setGridOptions(options: Partial<VxeGridProps['gridOptions']>) {
  68. this.setState({
  69. gridOptions: options,
  70. });
  71. }
  72. setLoading(isLoading: boolean) {
  73. this.setState({
  74. gridOptions: {
  75. loading: isLoading,
  76. },
  77. });
  78. }
  79. setState(
  80. stateOrFn:
  81. | ((prev: VxeGridProps) => Partial<VxeGridProps>)
  82. | Partial<VxeGridProps>,
  83. ) {
  84. if (isFunction(stateOrFn)) {
  85. this.store.setState((prev) => {
  86. return mergeWithArrayOverride(stateOrFn(prev), prev);
  87. });
  88. } else {
  89. this.store.setState((prev) => mergeWithArrayOverride(stateOrFn, prev));
  90. }
  91. }
  92. unmount() {
  93. this.isMounted = false;
  94. this.stateHandler.reset();
  95. }
  96. }