index.js 6.5 KB

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