index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends2 = require('babel-runtime/helpers/extends');
  6. var _extends3 = _interopRequireDefault(_extends2);
  7. var _vcNotification = require('../vc-notification');
  8. var _vcNotification2 = _interopRequireDefault(_vcNotification);
  9. var _icon = require('../icon');
  10. var _icon2 = _interopRequireDefault(_icon);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  12. var defaultDuration = 3;
  13. var defaultTop = void 0;
  14. var messageInstance = void 0;
  15. var key = 1;
  16. var prefixCls = 'ant-message';
  17. var transitionName = 'move-up';
  18. var getContainer = function getContainer() {
  19. return document.body;
  20. };
  21. var maxCount = void 0;
  22. function getMessageInstance(callback) {
  23. if (messageInstance) {
  24. callback(messageInstance);
  25. return;
  26. }
  27. _vcNotification2['default'].newInstance({
  28. prefixCls: prefixCls,
  29. transitionName: transitionName,
  30. style: { top: defaultTop }, // 覆盖原来的样式
  31. getContainer: getContainer,
  32. maxCount: maxCount
  33. }, function (instance) {
  34. if (messageInstance) {
  35. callback(messageInstance);
  36. return;
  37. }
  38. messageInstance = instance;
  39. callback(instance);
  40. });
  41. }
  42. // type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading';
  43. function notice(args) {
  44. var duration = args.duration !== undefined ? args.duration : defaultDuration;
  45. var iconType = {
  46. info: 'info-circle',
  47. success: 'check-circle',
  48. error: 'close-circle',
  49. warning: 'exclamation-circle',
  50. loading: 'loading'
  51. }[args.type];
  52. var target = args.key || key++;
  53. var closePromise = new Promise(function (resolve) {
  54. var callback = function callback() {
  55. if (typeof args.onClose === 'function') {
  56. args.onClose();
  57. }
  58. return resolve(true);
  59. };
  60. getMessageInstance(function (instance) {
  61. instance.notice({
  62. key: target,
  63. duration: duration,
  64. style: {},
  65. content: function content(h) {
  66. var iconNode = h(_icon2['default'], {
  67. attrs: { type: iconType, theme: iconType === 'loading' ? 'outlined' : 'filled' }
  68. });
  69. var switchIconNode = iconType ? iconNode : '';
  70. return h(
  71. 'div',
  72. {
  73. 'class': prefixCls + '-custom-content' + (args.type ? ' ' + prefixCls + '-' + args.type : '')
  74. },
  75. [args.icon ? typeof args.icon === 'function' ? args.icon(h) : args.icon : switchIconNode, h('span', [typeof args.content === 'function' ? args.content(h) : args.content])]
  76. );
  77. },
  78. onClose: callback
  79. });
  80. });
  81. });
  82. var result = function result() {
  83. if (messageInstance) {
  84. messageInstance.removeNotice(target);
  85. }
  86. };
  87. result.then = function (filled, rejected) {
  88. return closePromise.then(filled, rejected);
  89. };
  90. result.promise = closePromise;
  91. return result;
  92. }
  93. // type ConfigContent = React.ReactNode | string;
  94. // type ConfigDuration = number | (() => void);
  95. // export type ConfigOnClose = () => void;
  96. function isArgsProps(content) {
  97. return Object.prototype.toString.call(content) === '[object Object]' && !!content.content;
  98. }
  99. // export interface ConfigOptions {
  100. // top?: number;
  101. // duration?: number;
  102. // prefixCls?: string;
  103. // getContainer?: () => HTMLElement;
  104. // transitionName?: string;
  105. // }
  106. var api = {
  107. open: notice,
  108. config: function config(options) {
  109. if (options.top !== undefined) {
  110. defaultTop = options.top;
  111. messageInstance = null; // delete messageInstance for new defaultTop
  112. }
  113. if (options.duration !== undefined) {
  114. defaultDuration = options.duration;
  115. }
  116. if (options.prefixCls !== undefined) {
  117. prefixCls = options.prefixCls;
  118. }
  119. if (options.getContainer !== undefined) {
  120. getContainer = options.getContainer;
  121. }
  122. if (options.transitionName !== undefined) {
  123. transitionName = options.transitionName;
  124. messageInstance = null; // delete messageInstance for new transitionName
  125. }
  126. if (options.maxCount !== undefined) {
  127. maxCount = options.maxCount;
  128. messageInstance = null;
  129. }
  130. },
  131. destroy: function destroy() {
  132. if (messageInstance) {
  133. messageInstance.destroy();
  134. messageInstance = null;
  135. }
  136. }
  137. };
  138. ['success', 'info', 'warning', 'error', 'loading'].forEach(function (type) {
  139. api[type] = function (content, duration, onClose) {
  140. if (isArgsProps(content)) {
  141. return api.open((0, _extends3['default'])({}, content, { type: type }));
  142. }
  143. if (typeof duration === 'function') {
  144. onClose = duration;
  145. duration = undefined;
  146. }
  147. return api.open({ content: content, duration: duration, type: type, onClose: onClose });
  148. };
  149. });
  150. api.warn = api.warning;
  151. exports['default'] = api;