UNPKG

7.17 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; };
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _beeNotification = require('bee-notification');
14
15var _beeNotification2 = _interopRequireDefault(_beeNotification);
16
17var _classnames = require('classnames');
18
19var _classnames2 = _interopRequireDefault(_classnames);
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
22
23var defaultDuration = 1.5;
24var newDuration = void 0;
25var defaultTop = 0;
26var defaultBottom = 48;
27var bottom = 90;
28var padding = 30;
29var width = 200;
30var messageInstance = void 0;
31var key = 1;
32var clsPrefix = 'u-message';
33var noop = function noop() {};
34
35var positionObj = {
36 "top": {
37 messageStyle: {
38 width: "100%"
39 },
40 notificationStyle: {
41 top: defaultTop,
42 width: "100%"
43 },
44 transitionName: 'top'
45 },
46 "bottom": {
47 messageStyle: {
48 width: "100%"
49 },
50 notificationStyle: {
51 bottom: defaultBottom,
52 width: "100%"
53 },
54 transitionName: 'bottom'
55 },
56 "topRight": {
57 messageStyle: {
58 width: width
59 },
60 notificationStyle: {
61 top: padding,
62 right: padding,
63 width: width
64 },
65 transitionName: 'right'
66 },
67 "bottomRight": {
68 messageStyle: {
69 width: width
70 },
71 notificationStyle: {
72 bottom: bottom,
73 right: padding,
74 width: width
75 },
76 transitionName: 'right'
77 },
78 "topLeft": {
79 messageStyle: {
80 width: width
81 },
82 notificationStyle: {
83 top: padding,
84 left: padding,
85 width: width
86 },
87 transitionName: 'left'
88 },
89 "bottomLeft": {
90 messageStyle: {
91 width: width
92 },
93 notificationStyle: {
94 bottom: bottom,
95 left: padding,
96 width: width
97 },
98 transitionName: 'left'
99 }
100};
101
102function getMessageInstance() {
103 var position = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'top';
104 var callback = arguments[1];
105 var keyboard = arguments[2];
106 var onEscapeKeyUp = arguments[3];
107
108 if (messageInstance) {
109 callback(messageInstance);
110 return;
111 }
112 switch (position) {
113 case 'top':
114 positionObj[position].notificationStyle.top = defaultTop;
115 break;
116 case 'bottom':
117 positionObj[position].notificationStyle.bottom = defaultBottom;
118 break;
119 case 'bottomRight':
120 positionObj[position].notificationStyle.bottom = bottom;
121 break;
122 case 'bottomLeft':
123 positionObj[position].notificationStyle.bottom = bottom;
124 break;
125 default:
126 break;
127 }
128 if (position !== 'top' && position !== 'bottom') {
129 positionObj[position].messageStyle.width = width;
130 }
131 var style = positionObj[position].notificationStyle;
132 var instanceObj = {
133 clsPrefix: clsPrefix,
134 transitionName: clsPrefix + '-' + positionObj[position].transitionName,
135 style: style, // 覆盖原来的样式
136 position: ''
137 };
138 if (typeof keyboard === 'boolean') {
139 instanceObj.keyboard = keyboard;
140 }
141 if (typeof onEscapeKeyUp === 'function') {
142 instanceObj.onEscapeKeyUp = onEscapeKeyUp;
143 }
144 _beeNotification2["default"].newInstance(instanceObj, function (instance) {
145 messageInstance = instance;
146 callback(instance);
147 });
148}
149
150function notice(content, duration, type, onClose, position, style, keyboard, onEscapeKeyUp, showIcon) {
151 var iconType = {
152 info: 'uf uf-i-c-2',
153 success: 'uf uf-correct',
154 danger: 'uf uf-close-c',
155 warning: 'uf uf-exc-t',
156 light: 'uf uf-notification',
157 dark: 'uf uf-bubble',
158 news: 'uf uf-bell',
159 infolight: 'uf uf-i-c-2',
160 successlight: 'uf uf-correct',
161 dangerlight: 'uf uf-close-c',
162 warninglight: 'uf uf-exc-t'
163 }[type];
164
165 var positionStyle = positionObj[position].messageStyle;
166 getMessageInstance(position, function (instance) {
167 instance.notice({
168 key: key,
169 duration: duration,
170 color: type,
171 style: _extends({}, positionStyle, style),
172 content: _react2["default"].createElement(
173 'div',
174 null,
175 showIcon ? _react2["default"].createElement(
176 'div',
177 { className: clsPrefix + '-notice-description-icon' },
178 _react2["default"].createElement('i', { className: (0, _classnames2["default"])(iconType) })
179 ) : null,
180 _react2["default"].createElement(
181 'div',
182 { className: clsPrefix + '-notice-description-content' },
183 content
184 )
185 ),
186 onClose: onClose
187 });
188 }, keyboard, onEscapeKeyUp);
189 return function () {
190 var target = key++;
191 return function () {
192 if (messageInstance) {
193 messageInstance.removeNotice(target);
194 }
195 };
196 }();
197}
198
199exports["default"] = {
200 create: function create(obj) {
201 if (newDuration) {
202 //如果在config方法里设置了duration
203 obj.duration = newDuration;
204 }
205 var content = obj.content || '';
206 var duration = typeof obj.duration == 'undefined' ? defaultDuration : obj.duration;
207 var color = obj.color || 'dark';
208 var onClose = obj.onClose || noop;
209 var position = obj.position || "top";
210 var style = obj.style || {};
211 var showIcon = obj.showIcon || false;
212 return notice(content, duration, color, onClose, position, style, obj.keyboard, obj.onEscapeKeyUp, showIcon);
213 },
214 config: function config(options) {
215 if (options.top !== undefined) {
216 defaultTop = options.top;
217 }
218 if (options.duration !== undefined) {
219 defaultDuration = options.duration;
220 newDuration = defaultDuration;
221 }
222 if (options.clsPrefix !== undefined) {
223 clsPrefix = options.clsPrefix;
224 }
225 if (options.defaultBottom !== undefined) {
226 defaultBottom = options.defaultBottom;
227 }
228 if (options.bottom !== undefined) {
229 bottom = options.bottom;
230 }
231 if (options.width !== undefined) {
232 width = options.width;
233 }
234 },
235 destroy: function destroy() {
236 if (messageInstance) {
237 messageInstance.destroy();
238 messageInstance = null;
239 }
240 }
241};
242module.exports = exports['default'];
\No newline at end of file