index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  9. var _vcNotification = _interopRequireDefault(require("../vc-notification"));
  10. var _CheckCircleOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/CheckCircleOutlined"));
  11. var _InfoCircleOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/InfoCircleOutlined"));
  12. var _CloseCircleOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/CloseCircleOutlined"));
  13. var _ExclamationCircleOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/ExclamationCircleOutlined"));
  14. var _CloseOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/CloseOutlined"));
  15. var _util = require("../_util/util");
  16. var _configProvider = require("../config-provider");
  17. var _classNames = _interopRequireDefault(require("../_util/classNames"));
  18. var _style = _interopRequireDefault(require("./style"));
  19. var _useNotification = _interopRequireDefault(require("./useNotification"));
  20. var _util2 = require("./util");
  21. var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
  22. function adopt(value) {
  23. return value instanceof P ? value : new P(function (resolve) {
  24. resolve(value);
  25. });
  26. }
  27. return new (P || (P = Promise))(function (resolve, reject) {
  28. function fulfilled(value) {
  29. try {
  30. step(generator.next(value));
  31. } catch (e) {
  32. reject(e);
  33. }
  34. }
  35. function rejected(value) {
  36. try {
  37. step(generator["throw"](value));
  38. } catch (e) {
  39. reject(e);
  40. }
  41. }
  42. function step(result) {
  43. result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
  44. }
  45. step((generator = generator.apply(thisArg, _arguments || [])).next());
  46. });
  47. };
  48. const notificationInstance = {};
  49. let defaultDuration = 4.5;
  50. let defaultTop = '24px';
  51. let defaultBottom = '24px';
  52. let defaultPrefixCls = '';
  53. let defaultPlacement = 'topRight';
  54. let defaultGetContainer = () => document.body;
  55. let defaultCloseIcon = null;
  56. let rtl = false;
  57. let maxCount;
  58. function setNotificationConfig(options) {
  59. const {
  60. duration,
  61. placement,
  62. bottom,
  63. top,
  64. getContainer,
  65. closeIcon,
  66. prefixCls
  67. } = options;
  68. if (prefixCls !== undefined) {
  69. defaultPrefixCls = prefixCls;
  70. }
  71. if (duration !== undefined) {
  72. defaultDuration = duration;
  73. }
  74. if (placement !== undefined) {
  75. defaultPlacement = placement;
  76. }
  77. if (bottom !== undefined) {
  78. defaultBottom = typeof bottom === 'number' ? `${bottom}px` : bottom;
  79. }
  80. if (top !== undefined) {
  81. defaultTop = typeof top === 'number' ? `${top}px` : top;
  82. }
  83. if (getContainer !== undefined) {
  84. defaultGetContainer = getContainer;
  85. }
  86. if (closeIcon !== undefined) {
  87. defaultCloseIcon = closeIcon;
  88. }
  89. if (options.rtl !== undefined) {
  90. rtl = options.rtl;
  91. }
  92. if (options.maxCount !== undefined) {
  93. maxCount = options.maxCount;
  94. }
  95. }
  96. function getNotificationInstance(_ref, callback) {
  97. let {
  98. prefixCls: customizePrefixCls,
  99. placement = defaultPlacement,
  100. getContainer = defaultGetContainer,
  101. top,
  102. bottom,
  103. closeIcon = defaultCloseIcon,
  104. appContext
  105. } = _ref;
  106. const {
  107. getPrefixCls
  108. } = (0, _configProvider.globalConfig)();
  109. const prefixCls = getPrefixCls('notification', customizePrefixCls || defaultPrefixCls);
  110. const cacheKey = `${prefixCls}-${placement}-${rtl}`;
  111. const cacheInstance = notificationInstance[cacheKey];
  112. if (cacheInstance) {
  113. Promise.resolve(cacheInstance).then(instance => {
  114. callback(instance);
  115. });
  116. return;
  117. }
  118. const notificationClass = (0, _classNames.default)(`${prefixCls}-${placement}`, {
  119. [`${prefixCls}-rtl`]: rtl === true
  120. });
  121. _vcNotification.default.newInstance({
  122. name: 'notification',
  123. prefixCls: customizePrefixCls || defaultPrefixCls,
  124. useStyle: _style.default,
  125. class: notificationClass,
  126. style: (0, _util2.getPlacementStyle)(placement, top !== null && top !== void 0 ? top : defaultTop, bottom !== null && bottom !== void 0 ? bottom : defaultBottom),
  127. appContext,
  128. getContainer,
  129. closeIcon: _ref2 => {
  130. let {
  131. prefixCls
  132. } = _ref2;
  133. const closeIconToRender = (0, _vue.createVNode)("span", {
  134. "class": `${prefixCls}-close-x`
  135. }, [(0, _util.renderHelper)(closeIcon, {}, (0, _vue.createVNode)(_CloseOutlined.default, {
  136. "class": `${prefixCls}-close-icon`
  137. }, null))]);
  138. return closeIconToRender;
  139. },
  140. maxCount,
  141. hasTransitionName: true
  142. }, notification => {
  143. notificationInstance[cacheKey] = notification;
  144. callback(notification);
  145. });
  146. }
  147. const typeToIcon = {
  148. success: _CheckCircleOutlined.default,
  149. info: _InfoCircleOutlined.default,
  150. error: _CloseCircleOutlined.default,
  151. warning: _ExclamationCircleOutlined.default
  152. };
  153. function notice(args) {
  154. const {
  155. icon,
  156. type,
  157. description,
  158. message,
  159. btn
  160. } = args;
  161. const duration = args.duration === undefined ? defaultDuration : args.duration;
  162. getNotificationInstance(args, notification => {
  163. notification.notice({
  164. content: _ref3 => {
  165. let {
  166. prefixCls: outerPrefixCls
  167. } = _ref3;
  168. const prefixCls = `${outerPrefixCls}-notice`;
  169. let iconNode = null;
  170. if (icon) {
  171. iconNode = () => (0, _vue.createVNode)("span", {
  172. "class": `${prefixCls}-icon`
  173. }, [(0, _util.renderHelper)(icon)]);
  174. } else if (type) {
  175. const Icon = typeToIcon[type];
  176. iconNode = () => (0, _vue.createVNode)(Icon, {
  177. "class": `${prefixCls}-icon ${prefixCls}-icon-${type}`
  178. }, null);
  179. }
  180. return (0, _vue.createVNode)("div", {
  181. "class": iconNode ? `${prefixCls}-with-icon` : ''
  182. }, [iconNode && iconNode(), (0, _vue.createVNode)("div", {
  183. "class": `${prefixCls}-message`
  184. }, [!description && iconNode ? (0, _vue.createVNode)("span", {
  185. "class": `${prefixCls}-message-single-line-auto-margin`
  186. }, null) : null, (0, _util.renderHelper)(message)]), (0, _vue.createVNode)("div", {
  187. "class": `${prefixCls}-description`
  188. }, [(0, _util.renderHelper)(description)]), btn ? (0, _vue.createVNode)("span", {
  189. "class": `${prefixCls}-btn`
  190. }, [(0, _util.renderHelper)(btn)]) : null]);
  191. },
  192. duration,
  193. closable: true,
  194. onClose: args.onClose,
  195. onClick: args.onClick,
  196. key: args.key,
  197. style: args.style || {},
  198. class: args.class
  199. });
  200. });
  201. }
  202. const api = {
  203. open: notice,
  204. close(key) {
  205. Object.keys(notificationInstance).forEach(cacheKey => Promise.resolve(notificationInstance[cacheKey]).then(instance => {
  206. instance.removeNotice(key);
  207. }));
  208. },
  209. config: setNotificationConfig,
  210. destroy() {
  211. Object.keys(notificationInstance).forEach(cacheKey => {
  212. Promise.resolve(notificationInstance[cacheKey]).then(instance => {
  213. instance.destroy();
  214. });
  215. delete notificationInstance[cacheKey]; // lgtm[js/missing-await]
  216. });
  217. }
  218. };
  219. const iconTypes = ['success', 'info', 'warning', 'error'];
  220. iconTypes.forEach(type => {
  221. api[type] = args => api.open((0, _extends2.default)((0, _extends2.default)({}, args), {
  222. type
  223. }));
  224. });
  225. api.warn = api.warning;
  226. api.useNotification = _useNotification.default;
  227. /** @private test Only function. Not work on production */
  228. const getInstance = cacheKey => __awaiter(void 0, void 0, void 0, function* () {
  229. return process.env.NODE_ENV === 'test' ? notificationInstance[cacheKey] : null;
  230. });
  231. exports.getInstance = getInstance;
  232. var _default = exports.default = api;