index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import { createVNode as _createVNode } from "vue";
  4. import Notification from '../vc-notification';
  5. import LoadingOutlined from "@ant-design/icons-vue/es/icons/LoadingOutlined";
  6. import ExclamationCircleFilled from "@ant-design/icons-vue/es/icons/ExclamationCircleFilled";
  7. import CloseCircleFilled from "@ant-design/icons-vue/es/icons/CloseCircleFilled";
  8. import CheckCircleFilled from "@ant-design/icons-vue/es/icons/CheckCircleFilled";
  9. import InfoCircleFilled from "@ant-design/icons-vue/es/icons/InfoCircleFilled";
  10. import classNames from '../_util/classNames';
  11. var defaultDuration = 3;
  12. var defaultTop;
  13. var messageInstance;
  14. var key = 1;
  15. var localPrefixCls = '';
  16. var transitionName = 'move-up';
  17. var hasTransitionName = false;
  18. var getContainer = function getContainer() {
  19. return document.body;
  20. };
  21. var maxCount;
  22. var rtl = false;
  23. export function getKeyThenIncreaseKey() {
  24. return key++;
  25. }
  26. function setMessageConfig(options) {
  27. if (options.top !== undefined) {
  28. defaultTop = options.top;
  29. messageInstance = null; // delete messageInstance for new defaultTop
  30. }
  31. if (options.duration !== undefined) {
  32. defaultDuration = options.duration;
  33. }
  34. if (options.prefixCls !== undefined) {
  35. localPrefixCls = options.prefixCls;
  36. }
  37. if (options.getContainer !== undefined) {
  38. getContainer = options.getContainer;
  39. messageInstance = null; // delete messageInstance for new getContainer
  40. }
  41. if (options.transitionName !== undefined) {
  42. transitionName = options.transitionName;
  43. messageInstance = null; // delete messageInstance for new transitionName
  44. hasTransitionName = true;
  45. }
  46. if (options.maxCount !== undefined) {
  47. maxCount = options.maxCount;
  48. messageInstance = null;
  49. }
  50. if (options.rtl !== undefined) {
  51. rtl = options.rtl;
  52. }
  53. }
  54. function getMessageInstance(args, callback) {
  55. if (messageInstance) {
  56. callback(messageInstance);
  57. return;
  58. }
  59. Notification.newInstance({
  60. appContext: args.appContext,
  61. prefixCls: args.prefixCls || localPrefixCls,
  62. rootPrefixCls: args.rootPrefixCls,
  63. transitionName: transitionName,
  64. hasTransitionName: hasTransitionName,
  65. style: {
  66. top: defaultTop
  67. },
  68. getContainer: getContainer || args.getPopupContainer,
  69. maxCount: maxCount,
  70. name: 'message'
  71. }, function (instance) {
  72. if (messageInstance) {
  73. callback(messageInstance);
  74. return;
  75. }
  76. messageInstance = instance;
  77. callback(instance);
  78. });
  79. }
  80. var typeToIcon = {
  81. info: InfoCircleFilled,
  82. success: CheckCircleFilled,
  83. error: CloseCircleFilled,
  84. warning: ExclamationCircleFilled,
  85. loading: LoadingOutlined
  86. };
  87. function notice(args) {
  88. var duration = args.duration !== undefined ? args.duration : defaultDuration;
  89. var target = args.key || getKeyThenIncreaseKey();
  90. var closePromise = new Promise(function (resolve) {
  91. var callback = function callback() {
  92. if (typeof args.onClose === 'function') {
  93. args.onClose();
  94. }
  95. return resolve(true);
  96. };
  97. getMessageInstance(args, function (instance) {
  98. instance.notice({
  99. key: target,
  100. duration: duration,
  101. style: args.style || {},
  102. class: args.class,
  103. content: function content(_ref) {
  104. var _classNames;
  105. var prefixCls = _ref.prefixCls;
  106. var Icon = typeToIcon[args.type];
  107. var iconNode = Icon ? _createVNode(Icon, null, null) : '';
  108. var messageClass = classNames("".concat(prefixCls, "-custom-content"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(args.type), args.type), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), rtl === true), _classNames));
  109. return _createVNode("div", {
  110. "class": messageClass
  111. }, [typeof args.icon === 'function' ? args.icon() : args.icon || iconNode, _createVNode("span", null, [typeof args.content === 'function' ? args.content() : args.content])]);
  112. },
  113. onClose: callback,
  114. onClick: args.onClick
  115. });
  116. });
  117. });
  118. var result = function result() {
  119. if (messageInstance) {
  120. messageInstance.removeNotice(target);
  121. }
  122. };
  123. result.then = function (filled, rejected) {
  124. return closePromise.then(filled, rejected);
  125. };
  126. result.promise = closePromise;
  127. return result;
  128. }
  129. function isArgsProps(content) {
  130. return Object.prototype.toString.call(content) === '[object Object]' && !!content.content;
  131. }
  132. var api = {
  133. open: notice,
  134. config: setMessageConfig,
  135. destroy: function destroy(messageKey) {
  136. if (messageInstance) {
  137. if (messageKey) {
  138. var _messageInstance = messageInstance,
  139. removeNotice = _messageInstance.removeNotice;
  140. removeNotice(messageKey);
  141. } else {
  142. var _messageInstance2 = messageInstance,
  143. destroy = _messageInstance2.destroy;
  144. destroy();
  145. messageInstance = null;
  146. }
  147. }
  148. }
  149. };
  150. export function attachTypeApi(originalApi, type) {
  151. originalApi[type] = function (content, duration, onClose) {
  152. if (isArgsProps(content)) {
  153. return originalApi.open(_objectSpread(_objectSpread({}, content), {}, {
  154. type: type
  155. }));
  156. }
  157. if (typeof duration === 'function') {
  158. onClose = duration;
  159. duration = undefined;
  160. }
  161. return originalApi.open({
  162. content: content,
  163. duration: duration,
  164. type: type,
  165. onClose: onClose
  166. });
  167. };
  168. }
  169. ['success', 'info', 'warning', 'error', 'loading'].forEach(function (type) {
  170. return attachTypeApi(api, type);
  171. });
  172. api.warn = api.warning;
  173. /** @private test Only function. Not work on production */
  174. export var getInstance = function getInstance() {
  175. return process.env.NODE_ENV === 'test' ? messageInstance : null;
  176. };
  177. export default api;