index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  4. import { createVNode as _createVNode } from "vue";
  5. import _regeneratorRuntime from "@babel/runtime/regenerator";
  6. import Notification from '../vc-notification';
  7. import CheckCircleOutlined from "@ant-design/icons-vue/es/icons/CheckCircleOutlined";
  8. import InfoCircleOutlined from "@ant-design/icons-vue/es/icons/InfoCircleOutlined";
  9. import CloseCircleOutlined from "@ant-design/icons-vue/es/icons/CloseCircleOutlined";
  10. import ExclamationCircleOutlined from "@ant-design/icons-vue/es/icons/ExclamationCircleOutlined";
  11. import CloseOutlined from "@ant-design/icons-vue/es/icons/CloseOutlined";
  12. import { renderHelper } from '../_util/util';
  13. import { globalConfig } from '../config-provider';
  14. import classNames from '../_util/classNames';
  15. var notificationInstance = {};
  16. var defaultDuration = 4.5;
  17. var defaultTop = '24px';
  18. var defaultBottom = '24px';
  19. var defaultPrefixCls = '';
  20. var defaultPlacement = 'topRight';
  21. var defaultGetContainer = function defaultGetContainer() {
  22. return document.body;
  23. };
  24. var defaultCloseIcon = null;
  25. var rtl = false;
  26. var maxCount;
  27. function setNotificationConfig(options) {
  28. var duration = options.duration,
  29. placement = options.placement,
  30. bottom = options.bottom,
  31. top = options.top,
  32. getContainer = options.getContainer,
  33. closeIcon = options.closeIcon,
  34. prefixCls = options.prefixCls;
  35. if (prefixCls !== undefined) {
  36. defaultPrefixCls = prefixCls;
  37. }
  38. if (duration !== undefined) {
  39. defaultDuration = duration;
  40. }
  41. if (placement !== undefined) {
  42. defaultPlacement = placement;
  43. }
  44. if (bottom !== undefined) {
  45. defaultBottom = typeof bottom === 'number' ? "".concat(bottom, "px") : bottom;
  46. }
  47. if (top !== undefined) {
  48. defaultTop = typeof top === 'number' ? "".concat(top, "px") : top;
  49. }
  50. if (getContainer !== undefined) {
  51. defaultGetContainer = getContainer;
  52. }
  53. if (closeIcon !== undefined) {
  54. defaultCloseIcon = closeIcon;
  55. }
  56. if (options.rtl !== undefined) {
  57. rtl = options.rtl;
  58. }
  59. if (options.maxCount !== undefined) {
  60. maxCount = options.maxCount;
  61. }
  62. }
  63. function getPlacementStyle(placement) {
  64. var top = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultTop;
  65. var bottom = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultBottom;
  66. var style;
  67. switch (placement) {
  68. case 'topLeft':
  69. style = {
  70. left: '0px',
  71. top: top,
  72. bottom: 'auto'
  73. };
  74. break;
  75. case 'topRight':
  76. style = {
  77. right: '0px',
  78. top: top,
  79. bottom: 'auto'
  80. };
  81. break;
  82. case 'bottomLeft':
  83. style = {
  84. left: '0px',
  85. top: 'auto',
  86. bottom: bottom
  87. };
  88. break;
  89. default:
  90. style = {
  91. right: '0px',
  92. top: 'auto',
  93. bottom: bottom
  94. };
  95. break;
  96. }
  97. return style;
  98. }
  99. function getNotificationInstance(_ref, callback) {
  100. var customizePrefixCls = _ref.prefixCls,
  101. _ref$placement = _ref.placement,
  102. placement = _ref$placement === void 0 ? defaultPlacement : _ref$placement,
  103. _ref$getContainer = _ref.getContainer,
  104. getContainer = _ref$getContainer === void 0 ? defaultGetContainer : _ref$getContainer,
  105. top = _ref.top,
  106. bottom = _ref.bottom,
  107. _ref$closeIcon = _ref.closeIcon,
  108. _closeIcon = _ref$closeIcon === void 0 ? defaultCloseIcon : _ref$closeIcon,
  109. appContext = _ref.appContext;
  110. var _globalConfig = globalConfig(),
  111. getPrefixCls = _globalConfig.getPrefixCls;
  112. var prefixCls = getPrefixCls('notification', customizePrefixCls || defaultPrefixCls);
  113. var cacheKey = "".concat(prefixCls, "-").concat(placement, "-").concat(rtl);
  114. var cacheInstance = notificationInstance[cacheKey];
  115. if (cacheInstance) {
  116. Promise.resolve(cacheInstance).then(function (instance) {
  117. callback(instance);
  118. });
  119. return;
  120. }
  121. var notificationClass = classNames("".concat(prefixCls, "-").concat(placement), _defineProperty({}, "".concat(prefixCls, "-rtl"), rtl === true));
  122. Notification.newInstance({
  123. name: 'notification',
  124. prefixCls: customizePrefixCls || defaultPrefixCls,
  125. class: notificationClass,
  126. style: getPlacementStyle(placement, top, bottom),
  127. appContext: appContext,
  128. getContainer: getContainer,
  129. closeIcon: function closeIcon(_ref2) {
  130. var prefixCls = _ref2.prefixCls;
  131. var closeIconToRender = _createVNode("span", {
  132. "class": "".concat(prefixCls, "-close-x")
  133. }, [renderHelper(_closeIcon, {}, _createVNode(CloseOutlined, {
  134. "class": "".concat(prefixCls, "-close-icon")
  135. }, null))]);
  136. return closeIconToRender;
  137. },
  138. maxCount: maxCount,
  139. hasTransitionName: true
  140. }, function (notification) {
  141. notificationInstance[cacheKey] = notification;
  142. callback(notification);
  143. });
  144. }
  145. var typeToIcon = {
  146. success: CheckCircleOutlined,
  147. info: InfoCircleOutlined,
  148. error: CloseCircleOutlined,
  149. warning: ExclamationCircleOutlined
  150. };
  151. function notice(args) {
  152. var icon = args.icon,
  153. type = args.type,
  154. description = args.description,
  155. message = args.message,
  156. btn = args.btn;
  157. var duration = args.duration === undefined ? defaultDuration : args.duration;
  158. getNotificationInstance(args, function (notification) {
  159. notification.notice({
  160. content: function content(_ref3) {
  161. var outerPrefixCls = _ref3.prefixCls;
  162. var prefixCls = "".concat(outerPrefixCls, "-notice");
  163. var iconNode = null;
  164. if (icon) {
  165. iconNode = function iconNode() {
  166. return _createVNode("span", {
  167. "class": "".concat(prefixCls, "-icon")
  168. }, [renderHelper(icon)]);
  169. };
  170. } else if (type) {
  171. var Icon = typeToIcon[type];
  172. iconNode = function iconNode() {
  173. return _createVNode(Icon, {
  174. "class": "".concat(prefixCls, "-icon ").concat(prefixCls, "-icon-").concat(type)
  175. }, null);
  176. };
  177. }
  178. return _createVNode("div", {
  179. "class": iconNode ? "".concat(prefixCls, "-with-icon") : ''
  180. }, [iconNode && iconNode(), _createVNode("div", {
  181. "class": "".concat(prefixCls, "-message")
  182. }, [!description && iconNode ? _createVNode("span", {
  183. "class": "".concat(prefixCls, "-message-single-line-auto-margin")
  184. }, null) : null, renderHelper(message)]), _createVNode("div", {
  185. "class": "".concat(prefixCls, "-description")
  186. }, [renderHelper(description)]), btn ? _createVNode("span", {
  187. "class": "".concat(prefixCls, "-btn")
  188. }, [renderHelper(btn)]) : null]);
  189. },
  190. duration: duration,
  191. closable: true,
  192. onClose: args.onClose,
  193. onClick: args.onClick,
  194. key: args.key,
  195. style: args.style || {},
  196. class: args.class
  197. });
  198. });
  199. }
  200. var api = {
  201. open: notice,
  202. close: function close(key) {
  203. Object.keys(notificationInstance).forEach(function (cacheKey) {
  204. return Promise.resolve(notificationInstance[cacheKey]).then(function (instance) {
  205. instance.removeNotice(key);
  206. });
  207. });
  208. },
  209. config: setNotificationConfig,
  210. destroy: function destroy() {
  211. Object.keys(notificationInstance).forEach(function (cacheKey) {
  212. Promise.resolve(notificationInstance[cacheKey]).then(function (instance) {
  213. instance.destroy();
  214. });
  215. delete notificationInstance[cacheKey]; // lgtm[js/missing-await]
  216. });
  217. }
  218. };
  219. var iconTypes = ['success', 'info', 'warning', 'error'];
  220. iconTypes.forEach(function (type) {
  221. api[type] = function (args) {
  222. return api.open(_objectSpread(_objectSpread({}, args), {}, {
  223. type: type
  224. }));
  225. };
  226. });
  227. api.warn = api.warning;
  228. /** @private test Only function. Not work on production */
  229. export var getInstance = /*#__PURE__*/function () {
  230. var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(cacheKey) {
  231. return _regeneratorRuntime.wrap(function _callee$(_context) {
  232. while (1) {
  233. switch (_context.prev = _context.next) {
  234. case 0:
  235. return _context.abrupt("return", process.env.NODE_ENV === 'test' ? notificationInstance[cacheKey] : null);
  236. case 1:
  237. case "end":
  238. return _context.stop();
  239. }
  240. }
  241. }, _callee);
  242. }));
  243. return function getInstance(_x) {
  244. return _ref4.apply(this, arguments);
  245. };
  246. }();
  247. export default api;