app-drawer.ts 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
import { IModalData } from '@ibiz-template/runtime';
import Vue from 'vue';
import { AppDrawerOptions } from '@/interface';
import { AppDrawerComponent } from './app-drawer-component';
import { piniaInstance } from '@/store';

const PlacementMap: IData = {
  DRAWER_LEFT: 'left',
  DRAWER_RIGHT: 'right',
  DRAWER_TOP: 'top',
  DRAWER_BOTTOM: 'bottom',
};

export class AppDrawer {
  /**
   * 获取打开方式对应的placement
   *
   * @author lxm
   * @date 2022-09-15 17:09:46
   * @static
   * @param {string} openMode
   * @returns {*}  {AppDrawerOptions['placement']}
   */
  static getPlacement(openMode: string): AppDrawerOptions['placement'] {
    return PlacementMap[openMode] || 'right';
  }

  /**
   * 打开抽屉
   *
   * @author lxm
   * @date 2022-09-09 17:09:55
   * @static
   * @param {string} componentName 模态内部绘制组件名称
   * @param {IData} props 模态内部绘制组件的props
   * @param {AppDrawerOptions} [opts] 模态相关的配置参数
   * @returns {*}  {Promise<IModalData>}
   */
  static openDrawer(
    componentName: string,
    props: IData,
    opts?: AppDrawerOptions,
  ): Promise<IModalData> {
    return new Promise((resolve, reject) => {
      const vm = new Vue({
        pinia: piniaInstance,
        render(h) {
          return h(AppDrawerComponent, {
            props: {
              componentName,
              componentProps: props,
              opts,
              resolve,
              reject,
            },
          });
        },
      }).$mount();
      document.body.appendChild(vm.$el);
    });
  }
}