index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import Vue from 'vue';
  2. import { VxeUI } from '@vxe-ui/core';
  3. import XEUtils from 'xe-utils';
  4. import VxeModalComponent, { allActiveModals } from './src/modal';
  5. import { dynamicApp, dynamicStore, checkDynamic } from '../dynamics';
  6. function handleModal(options) {
  7. // 使用动态组件渲染动态弹框
  8. checkDynamic();
  9. return new Promise(resolve => {
  10. const opts = Object.assign({}, options);
  11. if (opts.id && allActiveModals.some(comp => comp.id === opts.id)) {
  12. resolve('exist');
  13. }
  14. else {
  15. const events = Object.assign({}, opts.events);
  16. const modalOpts = {
  17. key: XEUtils.uniqueId(),
  18. props: Object.assign(opts, {
  19. value: true
  20. }),
  21. on: Object.assign(Object.assign({}, events), { hide(params) {
  22. const modalList = dynamicStore.modals;
  23. if (events.hide) {
  24. events.hide.call(this, params);
  25. }
  26. dynamicStore.modals = modalList.filter(item => item.key !== modalOpts.key);
  27. resolve(params.type);
  28. } })
  29. };
  30. dynamicStore.modals.push(modalOpts);
  31. }
  32. });
  33. }
  34. function getModal(id) {
  35. return XEUtils.find(allActiveModals, $modal => $modal.id === id);
  36. }
  37. /**
  38. * 全局关闭动态的活动窗口(只能用于关闭动态的创建的活动窗口)
  39. * 如果传 id 则关闭指定的窗口
  40. * 如果不传则关闭所有窗口
  41. */
  42. function closeModal(id) {
  43. const modals = id ? [getModal(id)] : allActiveModals;
  44. const restPromises = [];
  45. modals.forEach($modal => {
  46. if ($modal) {
  47. restPromises.push($modal.close());
  48. }
  49. });
  50. return Promise.all(restPromises);
  51. }
  52. function handleOpen(defOpts, content, title, options) {
  53. let opts;
  54. if (XEUtils.isObject(content)) {
  55. opts = content;
  56. }
  57. else {
  58. opts = { content: XEUtils.toValueString(content), title };
  59. }
  60. return handleModal(Object.assign(Object.assign(Object.assign({}, defOpts), options), opts));
  61. }
  62. function openModal(options) {
  63. return handleOpen({
  64. type: 'modal'
  65. }, options);
  66. }
  67. function openAlert(content, title, options) {
  68. return handleOpen({
  69. type: 'alert',
  70. lockScroll: true,
  71. showHeader: true,
  72. showFooter: true
  73. }, content, title, options);
  74. }
  75. function openConfirm(content, title, options) {
  76. return handleOpen({
  77. type: 'confirm',
  78. status: 'question',
  79. lockScroll: true,
  80. showHeader: true,
  81. showFooter: true
  82. }, content, title, options);
  83. }
  84. function openMessage(content, options) {
  85. return handleOpen({
  86. type: 'message',
  87. mask: false,
  88. lockView: false,
  89. lockScroll: false,
  90. showHeader: false
  91. }, content, '', options);
  92. }
  93. function openNotification(content, title, options) {
  94. return handleOpen({
  95. type: 'notification',
  96. mask: false,
  97. lockView: false,
  98. lockScroll: false,
  99. showHeader: true,
  100. draggable: false,
  101. position: 'top-right',
  102. width: 320
  103. }, content, title, options);
  104. }
  105. export const ModalController = {
  106. get: getModal,
  107. close: closeModal,
  108. open: openModal,
  109. alert: openAlert,
  110. confirm: openConfirm,
  111. message: openMessage,
  112. notification: openNotification
  113. };
  114. export const VxeModal = Object.assign(VxeModalComponent, {
  115. install: function (app) {
  116. app.component(VxeModalComponent.name, VxeModalComponent);
  117. // 兼容老版本
  118. if (!Vue.prototype.$vxe) {
  119. Vue.prototype.$vxe = { modal: ModalController };
  120. }
  121. else {
  122. Vue.prototype.$vxe.modal = ModalController;
  123. }
  124. }
  125. });
  126. VxeUI.modal = ModalController;
  127. dynamicApp.use(VxeModal);
  128. VxeUI.component(VxeModalComponent);
  129. export const Modal = VxeModal;
  130. export default VxeModal;