UNPKG

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