api.ts 2.9 KB

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