1 | import _objectSpread from "@babel/runtime/helpers/objectSpread2";
|
2 | import React from 'react';
|
3 | import Icon from '../icon';
|
4 | import Notification from '../rc-components/notification';
|
5 | import { getPrefixCls } from '../configure';
|
6 | var notificationInstance = {};
|
7 | var defaultDuration = 4.5;
|
8 | var defaultTop = 24;
|
9 | var defaultBottom = 24;
|
10 | var defaultPlacement = 'topRight';
|
11 | var defaultGetContainer;
|
12 |
|
13 | function setNotificationConfig(options) {
|
14 | var duration = options.duration,
|
15 | placement = options.placement,
|
16 | bottom = options.bottom,
|
17 | top = options.top,
|
18 | getContainer = options.getContainer;
|
19 |
|
20 | if (duration !== undefined) {
|
21 | defaultDuration = duration;
|
22 | }
|
23 |
|
24 | if (placement !== undefined) {
|
25 | defaultPlacement = placement;
|
26 | }
|
27 |
|
28 | if (bottom !== undefined) {
|
29 | defaultBottom = bottom;
|
30 | }
|
31 |
|
32 | if (top !== undefined) {
|
33 | defaultTop = top;
|
34 | }
|
35 |
|
36 | if (getContainer !== undefined) {
|
37 | defaultGetContainer = getContainer;
|
38 | }
|
39 | }
|
40 |
|
41 | function getPlacementStyle(placement) {
|
42 | var style;
|
43 |
|
44 | switch (placement) {
|
45 | case 'topLeft':
|
46 | style = {
|
47 | left: 0,
|
48 | top: defaultTop,
|
49 | bottom: 'auto'
|
50 | };
|
51 | break;
|
52 |
|
53 | case 'topRight':
|
54 | style = {
|
55 | right: 0,
|
56 | top: defaultTop,
|
57 | bottom: 'auto'
|
58 | };
|
59 | break;
|
60 |
|
61 | case 'bottomLeft':
|
62 | style = {
|
63 | left: 0,
|
64 | top: 'auto',
|
65 | bottom: defaultBottom
|
66 | };
|
67 | break;
|
68 |
|
69 | default:
|
70 | style = {
|
71 | right: 0,
|
72 | top: 'auto',
|
73 | bottom: defaultBottom
|
74 | };
|
75 | break;
|
76 | }
|
77 |
|
78 | return style;
|
79 | }
|
80 |
|
81 | function getNotificationInstance(prefixCls, placement, callback) {
|
82 | var cacheKey = "".concat(prefixCls, "-").concat(placement);
|
83 |
|
84 | if (notificationInstance[cacheKey]) {
|
85 | callback(notificationInstance[cacheKey]);
|
86 | return;
|
87 | }
|
88 |
|
89 | Notification.newInstance({
|
90 | prefixCls: prefixCls,
|
91 | className: "".concat(prefixCls, "-").concat(placement),
|
92 | style: getPlacementStyle(placement),
|
93 | getContainer: defaultGetContainer,
|
94 | closeIcon: React.createElement(Icon, {
|
95 | className: "".concat(prefixCls, "-close-icon"),
|
96 | type: "close"
|
97 | })
|
98 | }, function (notification) {
|
99 | notificationInstance[cacheKey] = notification;
|
100 | callback(notification);
|
101 | });
|
102 | }
|
103 |
|
104 | var typeToIcon = {
|
105 | success: 'check',
|
106 | info: 'info',
|
107 | error: 'error',
|
108 | warning: 'warning'
|
109 | };
|
110 |
|
111 | function notice(args) {
|
112 | var outerPrefixCls = getPrefixCls('notification', args.prefixCls);
|
113 | var prefixCls = "".concat(outerPrefixCls, "-notice");
|
114 | var duration = args.duration === undefined ? defaultDuration : args.duration;
|
115 | var iconNode = null;
|
116 |
|
117 | if (args.icon) {
|
118 | iconNode = React.createElement("span", {
|
119 | className: "".concat(prefixCls, "-icon")
|
120 | }, args.icon);
|
121 | } else if (args.type) {
|
122 | var iconType = typeToIcon[args.type];
|
123 | iconNode = React.createElement(Icon, {
|
124 | className: "".concat(prefixCls, "-icon ").concat(prefixCls, "-icon-").concat(args.type),
|
125 | type: iconType
|
126 | });
|
127 | }
|
128 |
|
129 | var autoMarginTag = !args.description && iconNode ? React.createElement("span", {
|
130 | className: "".concat(prefixCls, "-message-single-line-auto-margin")
|
131 | }) : null;
|
132 | getNotificationInstance(outerPrefixCls, args.placement || defaultPlacement, function (notification) {
|
133 | notification.notice({
|
134 | content: React.createElement("div", {
|
135 | className: iconNode ? "".concat(prefixCls, "-with-icon") : ''
|
136 | }, iconNode, React.createElement("div", {
|
137 | className: "".concat(prefixCls, "-message")
|
138 | }, autoMarginTag, args.message), React.createElement("div", {
|
139 | className: "".concat(prefixCls, "-description")
|
140 | }, args.description), args.btn ? React.createElement("span", {
|
141 | className: "".concat(prefixCls, "-btn")
|
142 | }, args.btn) : null),
|
143 | duration: duration,
|
144 | closable: true,
|
145 | onClose: args.onClose,
|
146 | key: args.key,
|
147 | style: args.style || {},
|
148 | className: args.className
|
149 | });
|
150 | });
|
151 | }
|
152 |
|
153 | var api = {
|
154 | open: notice,
|
155 | close: function close(key) {
|
156 | Object.keys(notificationInstance).forEach(function (cacheKey) {
|
157 | return notificationInstance[cacheKey].removeNotice(key);
|
158 | });
|
159 | },
|
160 | config: setNotificationConfig,
|
161 | destroy: function destroy() {
|
162 | Object.keys(notificationInstance).forEach(function (cacheKey) {
|
163 | notificationInstance[cacheKey].destroy();
|
164 | delete notificationInstance[cacheKey];
|
165 | });
|
166 | }
|
167 | };
|
168 | ['success', 'info', 'warning', 'error'].forEach(function (type) {
|
169 | api[type] = function (args) {
|
170 | return api.open(_objectSpread({}, args, {
|
171 | type: type
|
172 | }));
|
173 | };
|
174 | });
|
175 | api.warn = api.warning;
|
176 | export default api;
|
177 |
|