index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import _extends from 'babel-runtime/helpers/extends';
  2. import Notification from '../vc-notification';
  3. import Icon from '../icon';
  4. var notificationInstance = {};
  5. var defaultDuration = 4.5;
  6. var defaultTop = '24px';
  7. var defaultBottom = '24px';
  8. var defaultPlacement = 'topRight';
  9. var defaultGetContainer = function defaultGetContainer() {
  10. return document.body;
  11. };
  12. var defaultCloseIcon = null;
  13. function setNotificationConfig(options) {
  14. var duration = options.duration,
  15. placement = options.placement,
  16. bottom = options.bottom,
  17. top = options.top,
  18. getContainer = options.getContainer,
  19. closeIcon = options.closeIcon;
  20. if (duration !== undefined) {
  21. defaultDuration = duration;
  22. }
  23. if (placement !== undefined) {
  24. defaultPlacement = placement;
  25. }
  26. if (bottom !== undefined) {
  27. defaultBottom = typeof bottom === 'number' ? bottom + 'px' : bottom;
  28. }
  29. if (top !== undefined) {
  30. defaultTop = typeof top === 'number' ? top + 'px' : top;
  31. }
  32. if (getContainer !== undefined) {
  33. defaultGetContainer = getContainer;
  34. }
  35. if (closeIcon !== undefined) {
  36. defaultCloseIcon = closeIcon;
  37. }
  38. }
  39. function getPlacementStyle(placement) {
  40. var top = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultTop;
  41. var bottom = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultBottom;
  42. var style = void 0;
  43. switch (placement) {
  44. case 'topLeft':
  45. style = {
  46. left: 0,
  47. top: top,
  48. bottom: 'auto'
  49. };
  50. break;
  51. case 'topRight':
  52. style = {
  53. right: 0,
  54. top: top,
  55. bottom: 'auto'
  56. };
  57. break;
  58. case 'bottomLeft':
  59. style = {
  60. left: 0,
  61. top: 'auto',
  62. bottom: bottom
  63. };
  64. break;
  65. default:
  66. style = {
  67. right: 0,
  68. top: 'auto',
  69. bottom: bottom
  70. };
  71. break;
  72. }
  73. return style;
  74. }
  75. function getNotificationInstance(_ref, callback) {
  76. var prefixCls = _ref.prefixCls,
  77. _ref$placement = _ref.placement,
  78. placement = _ref$placement === undefined ? defaultPlacement : _ref$placement,
  79. _ref$getContainer = _ref.getContainer,
  80. getContainer = _ref$getContainer === undefined ? defaultGetContainer : _ref$getContainer,
  81. top = _ref.top,
  82. bottom = _ref.bottom,
  83. _ref$closeIcon = _ref.closeIcon,
  84. _closeIcon = _ref$closeIcon === undefined ? defaultCloseIcon : _ref$closeIcon;
  85. var cacheKey = prefixCls + '-' + placement;
  86. if (notificationInstance[cacheKey]) {
  87. callback(notificationInstance[cacheKey]);
  88. return;
  89. }
  90. Notification.newInstance({
  91. prefixCls: prefixCls,
  92. 'class': prefixCls + '-' + placement,
  93. style: getPlacementStyle(placement, top, bottom),
  94. getContainer: getContainer,
  95. closeIcon: function closeIcon(h) {
  96. var icon = typeof _closeIcon === 'function' ? _closeIcon(h) : _closeIcon;
  97. var closeIconToRender = h(
  98. 'span',
  99. { 'class': prefixCls + '-close-x' },
  100. [icon || h(Icon, { 'class': prefixCls + '-close-icon', attrs: { type: 'close' }
  101. })]
  102. );
  103. return closeIconToRender;
  104. }
  105. }, function (notification) {
  106. notificationInstance[cacheKey] = notification;
  107. callback(notification);
  108. });
  109. }
  110. var typeToIcon = {
  111. success: 'check-circle-o',
  112. info: 'info-circle-o',
  113. error: 'close-circle-o',
  114. warning: 'exclamation-circle-o'
  115. };
  116. function notice(args) {
  117. var icon = args.icon,
  118. type = args.type,
  119. description = args.description,
  120. message = args.message,
  121. btn = args.btn;
  122. var outerPrefixCls = args.prefixCls || 'ant-notification';
  123. var prefixCls = outerPrefixCls + '-notice';
  124. var duration = args.duration === undefined ? defaultDuration : args.duration;
  125. var iconNode = null;
  126. if (icon) {
  127. iconNode = function iconNode(h) {
  128. return h(
  129. 'span',
  130. { 'class': prefixCls + '-icon' },
  131. [typeof icon === 'function' ? icon(h) : icon]
  132. );
  133. };
  134. } else if (type) {
  135. var iconType = typeToIcon[type];
  136. iconNode = function iconNode(h) {
  137. return h(Icon, { 'class': prefixCls + '-icon ' + prefixCls + '-icon-' + type, attrs: { type: iconType }
  138. });
  139. }; // eslint-disable-line
  140. }
  141. var placement = args.placement,
  142. top = args.top,
  143. bottom = args.bottom,
  144. getContainer = args.getContainer,
  145. closeIcon = args.closeIcon;
  146. getNotificationInstance({
  147. prefixCls: outerPrefixCls,
  148. placement: placement,
  149. top: top,
  150. bottom: bottom,
  151. getContainer: getContainer,
  152. closeIcon: closeIcon
  153. }, function (notification) {
  154. notification.notice({
  155. content: function content(h) {
  156. return h(
  157. 'div',
  158. { 'class': iconNode ? prefixCls + '-with-icon' : '' },
  159. [iconNode && iconNode(h), h(
  160. 'div',
  161. { 'class': prefixCls + '-message' },
  162. [!description && iconNode ? h('span', { 'class': prefixCls + '-message-single-line-auto-margin' }) : null, typeof message === 'function' ? message(h) : message]
  163. ), h(
  164. 'div',
  165. { 'class': prefixCls + '-description' },
  166. [typeof description === 'function' ? description(h) : description]
  167. ), btn ? h(
  168. 'span',
  169. { 'class': prefixCls + '-btn' },
  170. [typeof btn === 'function' ? btn(h) : btn]
  171. ) : null]
  172. );
  173. },
  174. duration: duration,
  175. closable: true,
  176. onClose: args.onClose,
  177. onClick: args.onClick,
  178. key: args.key,
  179. style: args.style || {},
  180. 'class': args['class']
  181. });
  182. });
  183. }
  184. var api = {
  185. open: notice,
  186. close: function close(key) {
  187. Object.keys(notificationInstance).forEach(function (cacheKey) {
  188. return notificationInstance[cacheKey].removeNotice(key);
  189. });
  190. },
  191. config: setNotificationConfig,
  192. destroy: function destroy() {
  193. Object.keys(notificationInstance).forEach(function (cacheKey) {
  194. notificationInstance[cacheKey].destroy();
  195. delete notificationInstance[cacheKey];
  196. });
  197. }
  198. };
  199. ['success', 'info', 'warning', 'error'].forEach(function (type) {
  200. api[type] = function (args) {
  201. return api.open(_extends({}, args, {
  202. type: type
  203. }));
  204. };
  205. });
  206. api.warn = api.warning;
  207. export default api;