index.js 9.1 KB

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