index.js 6.4 KB

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