1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | var _typeof = require("@babel/runtime/helpers/typeof");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports["default"] = exports.getInstance = void 0;
|
11 |
|
12 | var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
13 |
|
14 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
15 |
|
16 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
17 |
|
18 | var React = _interopRequireWildcard(require("react"));
|
19 |
|
20 | var _rcNotification = _interopRequireDefault(require("rc-notification"));
|
21 |
|
22 | var _CloseOutlined = _interopRequireDefault(require("@ant-design/icons/CloseOutlined"));
|
23 |
|
24 | var _classnames = _interopRequireDefault(require("classnames"));
|
25 |
|
26 | var _CheckCircleOutlined = _interopRequireDefault(require("@ant-design/icons/CheckCircleOutlined"));
|
27 |
|
28 | var _CloseCircleOutlined = _interopRequireDefault(require("@ant-design/icons/CloseCircleOutlined"));
|
29 |
|
30 | var _ExclamationCircleOutlined = _interopRequireDefault(require("@ant-design/icons/ExclamationCircleOutlined"));
|
31 |
|
32 | var _InfoCircleOutlined = _interopRequireDefault(require("@ant-design/icons/InfoCircleOutlined"));
|
33 |
|
34 | var _useNotification = _interopRequireDefault(require("./hooks/useNotification"));
|
35 |
|
36 | var _configProvider = require("../config-provider");
|
37 |
|
38 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
39 |
|
40 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
41 |
|
42 | var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
|
43 | function adopt(value) {
|
44 | return value instanceof P ? value : new P(function (resolve) {
|
45 | resolve(value);
|
46 | });
|
47 | }
|
48 |
|
49 | return new (P || (P = Promise))(function (resolve, reject) {
|
50 | function fulfilled(value) {
|
51 | try {
|
52 | step(generator.next(value));
|
53 | } catch (e) {
|
54 | reject(e);
|
55 | }
|
56 | }
|
57 |
|
58 | function rejected(value) {
|
59 | try {
|
60 | step(generator["throw"](value));
|
61 | } catch (e) {
|
62 | reject(e);
|
63 | }
|
64 | }
|
65 |
|
66 | function step(result) {
|
67 | result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
68 | }
|
69 |
|
70 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
71 | });
|
72 | };
|
73 |
|
74 | var notificationInstance = {};
|
75 | var defaultDuration = 4.5;
|
76 | var defaultTop = 24;
|
77 | var defaultBottom = 24;
|
78 | var defaultPrefixCls = '';
|
79 | var defaultPlacement = 'topRight';
|
80 | var defaultGetContainer;
|
81 | var defaultCloseIcon;
|
82 | var rtl = false;
|
83 |
|
84 | function setNotificationConfig(options) {
|
85 | var duration = options.duration,
|
86 | placement = options.placement,
|
87 | bottom = options.bottom,
|
88 | top = options.top,
|
89 | getContainer = options.getContainer,
|
90 | closeIcon = options.closeIcon,
|
91 | prefixCls = options.prefixCls;
|
92 |
|
93 | if (prefixCls !== undefined) {
|
94 | defaultPrefixCls = prefixCls;
|
95 | }
|
96 |
|
97 | if (duration !== undefined) {
|
98 | defaultDuration = duration;
|
99 | }
|
100 |
|
101 | if (placement !== undefined) {
|
102 | defaultPlacement = placement;
|
103 | } else if (options.rtl) {
|
104 | defaultPlacement = 'topLeft';
|
105 | }
|
106 |
|
107 | if (bottom !== undefined) {
|
108 | defaultBottom = bottom;
|
109 | }
|
110 |
|
111 | if (top !== undefined) {
|
112 | defaultTop = top;
|
113 | }
|
114 |
|
115 | if (getContainer !== undefined) {
|
116 | defaultGetContainer = getContainer;
|
117 | }
|
118 |
|
119 | if (closeIcon !== undefined) {
|
120 | defaultCloseIcon = closeIcon;
|
121 | }
|
122 |
|
123 | if (options.rtl !== undefined) {
|
124 | rtl = options.rtl;
|
125 | }
|
126 | }
|
127 |
|
128 | function getPlacementStyle(placement) {
|
129 | var top = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultTop;
|
130 | var bottom = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultBottom;
|
131 | var style;
|
132 |
|
133 | switch (placement) {
|
134 | case 'topLeft':
|
135 | style = {
|
136 | left: 0,
|
137 | top: top,
|
138 | bottom: 'auto'
|
139 | };
|
140 | break;
|
141 |
|
142 | case 'topRight':
|
143 | style = {
|
144 | right: 0,
|
145 | top: top,
|
146 | bottom: 'auto'
|
147 | };
|
148 | break;
|
149 |
|
150 | case 'bottomLeft':
|
151 | style = {
|
152 | left: 0,
|
153 | top: 'auto',
|
154 | bottom: bottom
|
155 | };
|
156 | break;
|
157 |
|
158 | default:
|
159 | style = {
|
160 | right: 0,
|
161 | top: 'auto',
|
162 | bottom: bottom
|
163 | };
|
164 | break;
|
165 | }
|
166 |
|
167 | return style;
|
168 | }
|
169 |
|
170 | function getNotificationInstance(args, callback) {
|
171 | var _args$placement = args.placement,
|
172 | placement = _args$placement === void 0 ? defaultPlacement : _args$placement,
|
173 | top = args.top,
|
174 | bottom = args.bottom,
|
175 | _args$getContainer = args.getContainer,
|
176 | getContainer = _args$getContainer === void 0 ? defaultGetContainer : _args$getContainer,
|
177 | _args$closeIcon = args.closeIcon,
|
178 | closeIcon = _args$closeIcon === void 0 ? defaultCloseIcon : _args$closeIcon,
|
179 | customizePrefixCls = args.prefixCls;
|
180 |
|
181 | var _globalConfig = (0, _configProvider.globalConfig)(),
|
182 | getPrefixCls = _globalConfig.getPrefixCls;
|
183 |
|
184 | var prefixCls = getPrefixCls('notification', customizePrefixCls || defaultPrefixCls);
|
185 | var cacheKey = "".concat(prefixCls, "-").concat(placement);
|
186 | var cacheInstance = notificationInstance[cacheKey];
|
187 |
|
188 | if (cacheInstance) {
|
189 | Promise.resolve(cacheInstance).then(function (instance) {
|
190 | callback({
|
191 | prefixCls: "".concat(prefixCls, "-notice"),
|
192 | instance: instance
|
193 | });
|
194 | });
|
195 | return;
|
196 | }
|
197 |
|
198 | var closeIconToRender = React.createElement("span", {
|
199 | className: "".concat(prefixCls, "-close-x")
|
200 | }, closeIcon || React.createElement(_CloseOutlined["default"], {
|
201 | className: "".concat(prefixCls, "-close-icon")
|
202 | }));
|
203 | var notificationClass = (0, _classnames["default"])("".concat(prefixCls, "-").concat(placement), (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-rtl"), rtl === true));
|
204 | notificationInstance[cacheKey] = new Promise(function (resolve) {
|
205 | _rcNotification["default"].newInstance({
|
206 | prefixCls: prefixCls,
|
207 | className: notificationClass,
|
208 | style: getPlacementStyle(placement, top, bottom),
|
209 | getContainer: getContainer,
|
210 | closeIcon: closeIconToRender
|
211 | }, function (notification) {
|
212 | resolve(notification);
|
213 | callback({
|
214 | prefixCls: "".concat(prefixCls, "-notice"),
|
215 | instance: notification
|
216 | });
|
217 | });
|
218 | });
|
219 | }
|
220 |
|
221 | var typeToIcon = {
|
222 | success: _CheckCircleOutlined["default"],
|
223 | info: _InfoCircleOutlined["default"],
|
224 | error: _CloseCircleOutlined["default"],
|
225 | warning: _ExclamationCircleOutlined["default"]
|
226 | };
|
227 |
|
228 | function getRCNoticeProps(args, prefixCls) {
|
229 | var durationArg = args.duration,
|
230 | icon = args.icon,
|
231 | type = args.type,
|
232 | description = args.description,
|
233 | message = args.message,
|
234 | btn = args.btn,
|
235 | onClose = args.onClose,
|
236 | onClick = args.onClick,
|
237 | key = args.key,
|
238 | style = args.style,
|
239 | className = args.className;
|
240 | var duration = durationArg === undefined ? defaultDuration : durationArg;
|
241 | var iconNode = null;
|
242 |
|
243 | if (icon) {
|
244 | iconNode = React.createElement("span", {
|
245 | className: "".concat(prefixCls, "-icon")
|
246 | }, args.icon);
|
247 | } else if (type) {
|
248 | iconNode = React.createElement(typeToIcon[type] || null, {
|
249 | className: "".concat(prefixCls, "-icon ").concat(prefixCls, "-icon-").concat(type)
|
250 | });
|
251 | }
|
252 |
|
253 | var autoMarginTag = !description && iconNode ? React.createElement("span", {
|
254 | className: "".concat(prefixCls, "-message-single-line-auto-margin")
|
255 | }) : null;
|
256 | return {
|
257 | content: React.createElement("div", {
|
258 | className: iconNode ? "".concat(prefixCls, "-with-icon") : '',
|
259 | role: "alert"
|
260 | }, iconNode, React.createElement("div", {
|
261 | className: "".concat(prefixCls, "-message")
|
262 | }, autoMarginTag, message), React.createElement("div", {
|
263 | className: "".concat(prefixCls, "-description")
|
264 | }, description), btn ? React.createElement("span", {
|
265 | className: "".concat(prefixCls, "-btn")
|
266 | }, btn) : null),
|
267 | duration: duration,
|
268 | closable: true,
|
269 | onClose: onClose,
|
270 | onClick: onClick,
|
271 | key: key,
|
272 | style: style || {},
|
273 | className: (0, _classnames["default"])(className, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-").concat(type), !!type))
|
274 | };
|
275 | }
|
276 |
|
277 | function notice(args) {
|
278 | getNotificationInstance(args, function (_ref) {
|
279 | var prefixCls = _ref.prefixCls,
|
280 | instance = _ref.instance;
|
281 | instance.notice(getRCNoticeProps(args, prefixCls));
|
282 | });
|
283 | }
|
284 |
|
285 | var api = {
|
286 | open: notice,
|
287 | close: function close(key) {
|
288 | Object.keys(notificationInstance).forEach(function (cacheKey) {
|
289 | return Promise.resolve(notificationInstance[cacheKey]).then(function (instance) {
|
290 | instance.removeNotice(key);
|
291 | });
|
292 | });
|
293 | },
|
294 | config: setNotificationConfig,
|
295 | destroy: function destroy() {
|
296 | Object.keys(notificationInstance).forEach(function (cacheKey) {
|
297 | Promise.resolve(notificationInstance[cacheKey]).then(function (instance) {
|
298 | instance.destroy();
|
299 | });
|
300 | delete notificationInstance[cacheKey];
|
301 | });
|
302 | }
|
303 | };
|
304 | ['success', 'info', 'warning', 'error'].forEach(function (type) {
|
305 | api[type] = function (args) {
|
306 | return api.open((0, _extends2["default"])((0, _extends2["default"])({}, args), {
|
307 | type: type
|
308 | }));
|
309 | };
|
310 | });
|
311 | api.warn = api.warning;
|
312 | api.useNotification = (0, _useNotification["default"])(getNotificationInstance, getRCNoticeProps);
|
313 |
|
314 |
|
315 | var getInstance = function getInstance(cacheKey) {
|
316 | return __awaiter(void 0, void 0, void 0, _regenerator["default"].mark(function _callee() {
|
317 | return _regenerator["default"].wrap(function _callee$(_context) {
|
318 | while (1) {
|
319 | switch (_context.prev = _context.next) {
|
320 | case 0:
|
321 | return _context.abrupt("return", process.env.NODE_ENV === 'test' ? notificationInstance[cacheKey] : null);
|
322 |
|
323 | case 1:
|
324 | case "end":
|
325 | return _context.stop();
|
326 | }
|
327 | }
|
328 | }, _callee);
|
329 | }));
|
330 | };
|
331 |
|
332 | exports.getInstance = getInstance;
|
333 | var _default = api;
|
334 | exports["default"] = _default; |
\ | No newline at end of file |