index.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { VueConstructor } from 'vue'
  2. import { VxeUI } from '@vxe-ui/core'
  3. import XEUtils from 'xe-utils'
  4. import VxeDrawerComponent, { allActiveDrawers } from './src/drawer'
  5. import { dynamicApp, dynamicStore, checkDynamic } from '../dynamics'
  6. import { VxeDrawerPropTypes, DrawerEventTypes, VxeDrawerListeners, VxeDrawerDefines } from '../../types'
  7. function handleDrawer (options: VxeDrawerDefines.DrawerOptions): Promise<DrawerEventTypes> {
  8. // 使用动态组件渲染动态弹框
  9. checkDynamic()
  10. return new Promise(resolve => {
  11. const opts = Object.assign({}, options)
  12. if (opts.id && allActiveDrawers.some(comp => comp.id === opts.id)) {
  13. resolve('exist')
  14. } else {
  15. const events = Object.assign({}, opts.events)
  16. const drawerOpts: {
  17. key: number | string
  18. props: VxeDrawerDefines.DrawerOptions
  19. on: VxeDrawerListeners
  20. } = {
  21. key: XEUtils.uniqueId(),
  22. props: Object.assign(opts, {
  23. value: true
  24. }),
  25. on: {
  26. ...events,
  27. hide (params) {
  28. const modalList = dynamicStore.modals
  29. if (events.hide) {
  30. events.hide.call(this, params)
  31. }
  32. dynamicStore.modals = modalList.filter(item => item.key !== drawerOpts.key)
  33. resolve(params.type)
  34. }
  35. }
  36. }
  37. dynamicStore.drawers.push(drawerOpts)
  38. }
  39. })
  40. }
  41. function getDrawer (id: VxeDrawerPropTypes.ID) {
  42. return XEUtils.find(allActiveDrawers, $drawer => $drawer.id === id)
  43. }
  44. /**
  45. * 全局关闭动态的活动窗口(只能用于关闭动态的创建的活动窗口)
  46. * 如果传 id 则关闭指定的窗口
  47. * 如果不传则关闭所有窗口
  48. */
  49. function closeDrawer (id?: VxeDrawerPropTypes.ID) {
  50. const drawers = id ? [getDrawer(id)] : allActiveDrawers
  51. const restPromises: any[] = []
  52. drawers.forEach($drawer => {
  53. if ($drawer) {
  54. restPromises.push($drawer.close())
  55. }
  56. })
  57. return Promise.all(restPromises)
  58. }
  59. function openDrawer (options: VxeDrawerDefines.DrawerOptions) {
  60. return handleDrawer(Object.assign({}, options))
  61. }
  62. export const DrawerController = {
  63. get: getDrawer,
  64. close: closeDrawer,
  65. open: openDrawer
  66. }
  67. export const VxeDrawer = Object.assign(VxeDrawerComponent, {
  68. install: function (app: VueConstructor) {
  69. app.component(VxeDrawerComponent.name as string, VxeDrawerComponent)
  70. }
  71. })
  72. VxeUI.drawer = DrawerController
  73. dynamicApp.use(VxeDrawer)
  74. VxeUI.component(VxeDrawerComponent)
  75. export const Drawer = VxeDrawer
  76. export default VxeDrawer