index.js 6.9 KB

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