confirm.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import { createVNode as _createVNode } from "vue";
  4. import { createVNode, render as vueRender } from 'vue';
  5. import ConfirmDialog from './ConfirmDialog';
  6. import { destroyFns } from './Modal';
  7. import ConfigProvider, { globalConfigForApi } from '../config-provider';
  8. import omit from '../_util/omit';
  9. import InfoCircleOutlined from "@ant-design/icons-vue/es/icons/InfoCircleOutlined";
  10. import CheckCircleOutlined from "@ant-design/icons-vue/es/icons/CheckCircleOutlined";
  11. import CloseCircleOutlined from "@ant-design/icons-vue/es/icons/CloseCircleOutlined";
  12. import ExclamationCircleOutlined from "@ant-design/icons-vue/es/icons/ExclamationCircleOutlined";
  13. var confirm = function confirm(config) {
  14. var container = document.createDocumentFragment();
  15. var currentConfig = _objectSpread(_objectSpread({}, omit(config, ['parentContext', 'appContext'])), {}, {
  16. close: close,
  17. visible: true
  18. });
  19. var confirmDialogInstance = null;
  20. function destroy() {
  21. if (confirmDialogInstance) {
  22. // destroy
  23. vueRender(null, container);
  24. confirmDialogInstance.component.update();
  25. confirmDialogInstance = null;
  26. }
  27. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  28. args[_key] = arguments[_key];
  29. }
  30. var triggerCancel = args.some(function (param) {
  31. return param && param.triggerCancel;
  32. });
  33. if (config.onCancel && triggerCancel) {
  34. config.onCancel.apply(config, args);
  35. }
  36. for (var i = 0; i < destroyFns.length; i++) {
  37. var fn = destroyFns[i];
  38. if (fn === close) {
  39. destroyFns.splice(i, 1);
  40. break;
  41. }
  42. }
  43. }
  44. function close() {
  45. var _this = this;
  46. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  47. args[_key2] = arguments[_key2];
  48. }
  49. currentConfig = _objectSpread(_objectSpread({}, currentConfig), {}, {
  50. visible: false,
  51. afterClose: function afterClose() {
  52. if (typeof config.afterClose === 'function') {
  53. config.afterClose();
  54. }
  55. destroy.apply(_this, args);
  56. }
  57. });
  58. update(currentConfig);
  59. }
  60. function update(configUpdate) {
  61. if (typeof configUpdate === 'function') {
  62. currentConfig = configUpdate(currentConfig);
  63. } else {
  64. currentConfig = _objectSpread(_objectSpread({}, currentConfig), configUpdate);
  65. }
  66. if (confirmDialogInstance) {
  67. _extends(confirmDialogInstance.component.props, currentConfig);
  68. confirmDialogInstance.component.update();
  69. }
  70. }
  71. var Wrapper = function Wrapper(p) {
  72. var global = globalConfigForApi;
  73. var rootPrefixCls = global.prefixCls;
  74. var prefixCls = p.prefixCls || "".concat(rootPrefixCls, "-modal");
  75. return _createVNode(ConfigProvider, _objectSpread(_objectSpread({}, global), {}, {
  76. "notUpdateGlobalConfig": true,
  77. "prefixCls": rootPrefixCls
  78. }), {
  79. default: function _default() {
  80. return [_createVNode(ConfirmDialog, _objectSpread(_objectSpread({}, p), {}, {
  81. "rootPrefixCls": rootPrefixCls,
  82. "prefixCls": prefixCls
  83. }), null)];
  84. }
  85. });
  86. };
  87. function render(props) {
  88. var vm = createVNode(Wrapper, _objectSpread({}, props));
  89. vm.appContext = config.parentContext || config.appContext || vm.appContext;
  90. vueRender(vm, container);
  91. return vm;
  92. }
  93. confirmDialogInstance = render(currentConfig);
  94. destroyFns.push(close);
  95. return {
  96. destroy: close,
  97. update: update
  98. };
  99. };
  100. export default confirm;
  101. export function withWarn(props) {
  102. return _objectSpread(_objectSpread({
  103. icon: function icon() {
  104. return _createVNode(ExclamationCircleOutlined, null, null);
  105. },
  106. okCancel: false
  107. }, props), {}, {
  108. type: 'warning'
  109. });
  110. }
  111. export function withInfo(props) {
  112. return _objectSpread(_objectSpread({
  113. icon: function icon() {
  114. return _createVNode(InfoCircleOutlined, null, null);
  115. },
  116. okCancel: false
  117. }, props), {}, {
  118. type: 'info'
  119. });
  120. }
  121. export function withSuccess(props) {
  122. return _objectSpread(_objectSpread({
  123. icon: function icon() {
  124. return _createVNode(CheckCircleOutlined, null, null);
  125. },
  126. okCancel: false
  127. }, props), {}, {
  128. type: 'success'
  129. });
  130. }
  131. export function withError(props) {
  132. return _objectSpread(_objectSpread({
  133. icon: function icon() {
  134. return _createVNode(CloseCircleOutlined, null, null);
  135. },
  136. okCancel: false
  137. }, props), {}, {
  138. type: 'error'
  139. });
  140. }
  141. export function withConfirm(props) {
  142. return _objectSpread(_objectSpread({
  143. icon: function icon() {
  144. return _createVNode(ExclamationCircleOutlined, null, null);
  145. },
  146. okCancel: true
  147. }, props), {}, {
  148. type: 'confirm'
  149. });
  150. }