UNPKG

7.24 kBJavaScriptView Raw
1import { isVNode as _isVNode, createVNode as _createVNode } from "vue";
2
3function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
4
5import Notification from '../vc-notification';
6import CheckCircleOutlined from '@ant-design/icons-vue/CheckCircleOutlined';
7import InfoCircleOutlined from '@ant-design/icons-vue/InfoCircleOutlined';
8import CloseCircleOutlined from '@ant-design/icons-vue/CloseCircleOutlined';
9import ExclamationCircleOutlined from '@ant-design/icons-vue/ExclamationCircleOutlined';
10import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
11
12function _isSlot(s) {
13 return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !_isVNode(s);
14}
15
16var notificationInstance = {};
17var defaultDuration = 4.5;
18var defaultTop = '24px';
19var defaultBottom = '24px';
20var defaultPlacement = 'topRight';
21
22var defaultGetContainer = function defaultGetContainer() {
23 return document.body;
24};
25
26var defaultCloseIcon = null;
27
28function setNotificationConfig(options) {
29 var duration = options.duration,
30 placement = options.placement,
31 bottom = options.bottom,
32 top = options.top,
33 getContainer = options.getContainer,
34 closeIcon = options.closeIcon;
35
36 if (duration !== undefined) {
37 defaultDuration = duration;
38 }
39
40 if (placement !== undefined) {
41 defaultPlacement = placement;
42 }
43
44 if (bottom !== undefined) {
45 defaultBottom = typeof bottom === 'number' ? "".concat(bottom, "px") : bottom;
46 }
47
48 if (top !== undefined) {
49 defaultTop = typeof top === 'number' ? "".concat(top, "px") : top;
50 }
51
52 if (getContainer !== undefined) {
53 defaultGetContainer = getContainer;
54 }
55
56 if (closeIcon !== undefined) {
57 defaultCloseIcon = closeIcon;
58 }
59}
60
61function getPlacementStyle(placement) {
62 var top = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultTop;
63 var bottom = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultBottom;
64 var style;
65
66 switch (placement) {
67 case 'topLeft':
68 style = {
69 left: '0px',
70 top: top,
71 bottom: 'auto'
72 };
73 break;
74
75 case 'topRight':
76 style = {
77 right: '0px',
78 top: top,
79 bottom: 'auto'
80 };
81 break;
82
83 case 'bottomLeft':
84 style = {
85 left: '0px',
86 top: 'auto',
87 bottom: bottom
88 };
89 break;
90
91 default:
92 style = {
93 right: '0px',
94 top: 'auto',
95 bottom: bottom
96 };
97 break;
98 }
99
100 return style;
101}
102
103function getNotificationInstance(_ref, callback) {
104 var prefixCls = _ref.prefixCls,
105 _ref$placement = _ref.placement,
106 placement = _ref$placement === void 0 ? defaultPlacement : _ref$placement,
107 _ref$getContainer = _ref.getContainer,
108 getContainer = _ref$getContainer === void 0 ? defaultGetContainer : _ref$getContainer,
109 top = _ref.top,
110 bottom = _ref.bottom,
111 _ref$closeIcon = _ref.closeIcon,
112 _closeIcon = _ref$closeIcon === void 0 ? defaultCloseIcon : _ref$closeIcon;
113
114 var cacheKey = "".concat(prefixCls, "-").concat(placement);
115
116 if (notificationInstance[cacheKey]) {
117 callback(notificationInstance[cacheKey]);
118 return;
119 }
120
121 Notification.newInstance({
122 prefixCls: prefixCls,
123 class: "".concat(prefixCls, "-").concat(placement),
124 style: getPlacementStyle(placement, top, bottom),
125 getContainer: getContainer,
126 closeIcon: function closeIcon() {
127 var closeIconToRender = _createVNode("span", {
128 "class": "".concat(prefixCls, "-close-x")
129 }, [_closeIcon || _createVNode(CloseOutlined, {
130 "class": "".concat(prefixCls, "-close-icon")
131 }, null)]);
132
133 return closeIconToRender;
134 }
135 }, function (notification) {
136 notificationInstance[cacheKey] = notification;
137 callback(notification);
138 });
139}
140
141var typeToIcon = {
142 success: CheckCircleOutlined,
143 info: InfoCircleOutlined,
144 error: CloseCircleOutlined,
145 warning: ExclamationCircleOutlined
146};
147
148function notice(args) {
149 var icon = args.icon,
150 type = args.type,
151 description = args.description,
152 message = args.message,
153 btn = args.btn;
154 var outerPrefixCls = args.prefixCls || 'ant-notification';
155 var prefixCls = "".concat(outerPrefixCls, "-notice");
156 var duration = args.duration === undefined ? defaultDuration : args.duration;
157 var iconNode = null;
158
159 if (icon) {
160 iconNode = function iconNode() {
161 return _createVNode("span", {
162 "class": "".concat(prefixCls, "-icon")
163 }, _isSlot(icon) ? icon : {
164 default: function _default() {
165 return [icon];
166 }
167 });
168 };
169 } else if (type) {
170 var Icon = typeToIcon[type];
171
172 iconNode = function iconNode() {
173 return _createVNode(Icon, {
174 "class": "".concat(prefixCls, "-icon ").concat(prefixCls, "-icon-").concat(type)
175 }, null);
176 };
177 }
178
179 var placement = args.placement,
180 top = args.top,
181 bottom = args.bottom,
182 getContainer = args.getContainer,
183 closeIcon = args.closeIcon;
184 getNotificationInstance({
185 prefixCls: outerPrefixCls,
186 placement: placement,
187 top: top,
188 bottom: bottom,
189 getContainer: getContainer,
190 closeIcon: closeIcon
191 }, function (notification) {
192 notification.notice({
193 content: function content() {
194 return _createVNode("div", {
195 "class": iconNode ? "".concat(prefixCls, "-with-icon") : ''
196 }, [iconNode && iconNode(), _createVNode("div", {
197 "class": "".concat(prefixCls, "-message")
198 }, [!description && iconNode ? _createVNode("span", {
199 "class": "".concat(prefixCls, "-message-single-line-auto-margin")
200 }, null) : null, message]), _createVNode("div", {
201 "class": "".concat(prefixCls, "-description")
202 }, _isSlot(description) ? description : {
203 default: function _default() {
204 return [description];
205 }
206 }), btn ? _createVNode("span", {
207 "class": "".concat(prefixCls, "-btn")
208 }, _isSlot(btn) ? btn : {
209 default: function _default() {
210 return [btn];
211 }
212 }) : null]);
213 },
214 duration: duration,
215 closable: true,
216 onClose: args.onClose,
217 onClick: args.onClick,
218 key: args.key,
219 style: args.style || {},
220 class: args.class
221 });
222 });
223}
224
225var api = {
226 open: notice,
227 close: function close(key) {
228 Object.keys(notificationInstance).forEach(function (cacheKey) {
229 return notificationInstance[cacheKey].removeNotice(key);
230 });
231 },
232 config: setNotificationConfig,
233 destroy: function destroy() {
234 Object.keys(notificationInstance).forEach(function (cacheKey) {
235 notificationInstance[cacheKey].destroy();
236 delete notificationInstance[cacheKey];
237 });
238 }
239};
240['success', 'info', 'warning', 'error'].forEach(function (type) {
241 api[type] = function (args) {
242 return api.open(_extends(_extends({}, args), {
243 type: type
244 }));
245 };
246});
247api.warn = api.warning;
248export default api;
\No newline at end of file