index.js 2.3 KB

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