1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.withContext = undefined;
|
5 |
|
6 | var _extends2 = require('babel-runtime/helpers/extends');
|
7 |
|
8 | var _extends3 = _interopRequireDefault(_extends2);
|
9 |
|
10 | var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
|
11 |
|
12 | var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
|
13 |
|
14 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
15 |
|
16 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
17 |
|
18 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
19 |
|
20 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
21 |
|
22 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
23 |
|
24 | var _inherits3 = _interopRequireDefault(_inherits2);
|
25 |
|
26 | var _class, _temp2;
|
27 |
|
28 | var _react = require('react');
|
29 |
|
30 | var _react2 = _interopRequireDefault(_react);
|
31 |
|
32 | var _reactDom = require('react-dom');
|
33 |
|
34 | var _reactDom2 = _interopRequireDefault(_reactDom);
|
35 |
|
36 | var _propTypes = require('prop-types');
|
37 |
|
38 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
39 |
|
40 | var _overlay = require('../overlay');
|
41 |
|
42 | var _overlay2 = _interopRequireDefault(_overlay);
|
43 |
|
44 | var _configProvider = require('../config-provider');
|
45 |
|
46 | var _configProvider2 = _interopRequireDefault(_configProvider);
|
47 |
|
48 | var _util = require('../util');
|
49 |
|
50 | var _message = require('./message');
|
51 |
|
52 | var _message2 = _interopRequireDefault(_message);
|
53 |
|
54 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
55 |
|
56 | var config = _configProvider2.default.config;
|
57 |
|
58 |
|
59 | var instance = void 0;
|
60 | var timeouts = {};
|
61 |
|
62 | var Mask = (_temp2 = _class = function (_React$Component) {
|
63 | (0, _inherits3.default)(Mask, _React$Component);
|
64 |
|
65 | function Mask() {
|
66 | var _temp, _this, _ret;
|
67 |
|
68 | (0, _classCallCheck3.default)(this, Mask);
|
69 |
|
70 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
71 | args[_key] = arguments[_key];
|
72 | }
|
73 |
|
74 | return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
|
75 | visible: true
|
76 | }, _this.handleClose = function () {
|
77 | var silent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
78 |
|
79 | _this.setState({
|
80 | visible: false
|
81 | });
|
82 |
|
83 | if (!silent) {
|
84 | _this.props.onClose && _this.props.onClose();
|
85 | }
|
86 | }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
|
87 | }
|
88 |
|
89 | Mask.prototype.componentWillUnmount = function componentWillUnmount() {
|
90 | var timeoutId = this.props.timeoutId;
|
91 |
|
92 |
|
93 | if (timeoutId in timeouts) {
|
94 | var timeout = timeouts[timeoutId];
|
95 | clearTimeout(timeout);
|
96 | delete timeouts[timeoutId];
|
97 | }
|
98 | };
|
99 |
|
100 | Mask.prototype.render = function render() {
|
101 |
|
102 | var _props = this.props,
|
103 | prefix = _props.prefix,
|
104 | type = _props.type,
|
105 | title = _props.title,
|
106 | content = _props.content,
|
107 | align = _props.align,
|
108 | offset = _props.offset,
|
109 | hasMask = _props.hasMask,
|
110 | afterClose = _props.afterClose,
|
111 | animation = _props.animation,
|
112 | overlayProps = _props.overlayProps,
|
113 | timeoutId = _props.timeoutId,
|
114 | className = _props.className,
|
115 | style = _props.style,
|
116 | others = (0, _objectWithoutProperties3.default)(_props, ['prefix', 'type', 'title', 'content', 'align', 'offset', 'hasMask', 'afterClose', 'animation', 'overlayProps', 'timeoutId', 'className', 'style']);
|
117 |
|
118 |
|
119 | var visible = this.state.visible;
|
120 |
|
121 | return _react2.default.createElement(
|
122 | _overlay2.default,
|
123 | (0, _extends3.default)({}, overlayProps, {
|
124 | prefix: prefix,
|
125 | animation: animation,
|
126 | visible: visible,
|
127 | align: align,
|
128 | offset: offset,
|
129 | hasMask: hasMask,
|
130 | afterClose: afterClose
|
131 | }),
|
132 | _react2.default.createElement(
|
133 | _message2.default,
|
134 | (0, _extends3.default)({}, others, {
|
135 | prefix: prefix,
|
136 | visible: true,
|
137 | type: type,
|
138 | shape: 'toast',
|
139 | title: title,
|
140 | style: style,
|
141 | className: prefix + 'message-wrapper ' + className,
|
142 | onClose: this.handleClose
|
143 | }),
|
144 | content
|
145 | )
|
146 | );
|
147 | };
|
148 |
|
149 | return Mask;
|
150 | }(_react2.default.Component), _class.contextTypes = {
|
151 | prefix: _propTypes2.default.string
|
152 | }, _class.propTypes = {
|
153 | prefix: _propTypes2.default.string,
|
154 | type: _propTypes2.default.string,
|
155 | title: _propTypes2.default.node,
|
156 | content: _propTypes2.default.node,
|
157 | align: _propTypes2.default.string,
|
158 | offset: _propTypes2.default.array,
|
159 | hasMask: _propTypes2.default.bool,
|
160 | afterClose: _propTypes2.default.func,
|
161 | animation: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.bool]),
|
162 | overlayProps: _propTypes2.default.object,
|
163 | onClose: _propTypes2.default.func,
|
164 | timeoutId: _propTypes2.default.string,
|
165 | style: _propTypes2.default.object,
|
166 | className: _propTypes2.default.string
|
167 | }, _class.defaultProps = {
|
168 | prefix: 'next-',
|
169 | align: 'tc tc',
|
170 | offset: [0, 30],
|
171 | hasMask: false,
|
172 | animation: {
|
173 | in: 'pulse',
|
174 | out: 'zoomOut'
|
175 | },
|
176 | style: {},
|
177 | className: ''
|
178 | }, _temp2);
|
179 | Mask.displayName = 'Mask';
|
180 |
|
181 |
|
182 | var NewMask = config(Mask);
|
183 |
|
184 | var create = function create(props) {
|
185 |
|
186 | var duration = props.duration,
|
187 | afterClose = props.afterClose,
|
188 | contextConfig = props.contextConfig,
|
189 | others = (0, _objectWithoutProperties3.default)(props, ['duration', 'afterClose', 'contextConfig']);
|
190 |
|
191 |
|
192 | var div = document.createElement('div');
|
193 | document.body.appendChild(div);
|
194 | var closeChain = function closeChain() {
|
195 | _reactDom2.default.unmountComponentAtNode(div);
|
196 | document.body.removeChild(div);
|
197 | afterClose && afterClose();
|
198 | };
|
199 |
|
200 | var newContext = contextConfig;
|
201 | if (!newContext) newContext = _configProvider2.default.getContext();
|
202 |
|
203 | var mask = void 0,
|
204 | myRef = void 0,
|
205 | destroyed = false;
|
206 | var destroy = function destroy() {
|
207 | var inc = mask && mask.getInstance();
|
208 | inc && inc.handleClose(true);
|
209 | destroyed = true;
|
210 | };
|
211 |
|
212 | _reactDom2.default.render(_react2.default.createElement(
|
213 | _configProvider2.default,
|
214 | newContext,
|
215 | _react2.default.createElement(NewMask, (0, _extends3.default)({
|
216 | afterClose: closeChain
|
217 | }, others, {
|
218 | ref: function ref(_ref) {
|
219 | myRef = _ref;
|
220 | }
|
221 | }))
|
222 | ), div, function () {
|
223 | mask = myRef;
|
224 | if (mask && destroyed) {
|
225 | destroy();
|
226 | }
|
227 | });
|
228 |
|
229 | return {
|
230 | component: mask,
|
231 | destroy: destroy
|
232 | };
|
233 | };
|
234 |
|
235 | function handleConfig(config, type) {
|
236 | var newConfig = {};
|
237 |
|
238 | if (typeof config === 'string' || _react2.default.isValidElement(config)) {
|
239 | newConfig.title = config;
|
240 | } else if (isObject(config)) {
|
241 | newConfig = (0, _extends3.default)({}, config);
|
242 | }
|
243 | if (typeof newConfig.duration !== 'number') {
|
244 | newConfig.duration = 3000;
|
245 | }
|
246 | if (type) {
|
247 | newConfig.type = type;
|
248 | }
|
249 |
|
250 | return newConfig;
|
251 | }
|
252 |
|
253 | function isObject(obj) {
|
254 | return {}.toString.call(obj) === '[object Object]';
|
255 | }
|
256 |
|
257 | function open(config, type) {
|
258 | close();
|
259 | config = handleConfig(config, type);
|
260 | var timeoutId = (0, _util.guid)();
|
261 | instance = create((0, _extends3.default)({}, config, { timeoutId: timeoutId }));
|
262 |
|
263 | if (config.duration > 0) {
|
264 | var timeout = setTimeout(close, config.duration);
|
265 | timeouts[timeoutId] = timeout;
|
266 | }
|
267 | }
|
268 |
|
269 | function close() {
|
270 | if (instance) {
|
271 | instance.destroy();
|
272 | instance = null;
|
273 | }
|
274 | }
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | function _show(config) {
|
282 | open(config);
|
283 | }
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | function hide() {
|
290 | close();
|
291 | }
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | function _success(config) {
|
299 | open(config, 'success');
|
300 | }
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | function _warning(config) {
|
308 | open(config, 'warning');
|
309 | }
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | function _error(config) {
|
317 | open(config, 'error');
|
318 | }
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | function _help(config) {
|
326 | open(config, 'help');
|
327 | }
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 | function _loading(config) {
|
335 | open(config, 'loading');
|
336 | }
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | function _notice(config) {
|
344 | open(config, 'notice');
|
345 | }
|
346 |
|
347 | exports.default = {
|
348 | show: _show,
|
349 | hide: hide,
|
350 | success: _success,
|
351 | warning: _warning,
|
352 | error: _error,
|
353 | help: _help,
|
354 | loading: _loading,
|
355 | notice: _notice
|
356 | };
|
357 | var withContext = exports.withContext = function withContext(WrappedComponent) {
|
358 | var HOC = function HOC(props) {
|
359 | return _react2.default.createElement(
|
360 | _configProvider2.default.Consumer,
|
361 | null,
|
362 | function (contextConfig) {
|
363 | return _react2.default.createElement(WrappedComponent, (0, _extends3.default)({}, props, {
|
364 | contextMessage: {
|
365 | show: function show() {
|
366 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
367 | return _show((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
368 | },
|
369 | hide: hide,
|
370 | success: function success() {
|
371 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
372 | return _success((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
373 | },
|
374 | warning: function warning() {
|
375 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
376 | return _warning((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
377 | },
|
378 | error: function error() {
|
379 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
380 | return _error((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
381 | },
|
382 | help: function help() {
|
383 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
384 | return _help((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
385 | },
|
386 | loading: function loading() {
|
387 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
388 | return _loading((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
389 | },
|
390 | notice: function notice() {
|
391 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
392 | return _notice((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
393 | }
|
394 | }
|
395 | }));
|
396 | }
|
397 | );
|
398 | };
|
399 | return HOC;
|
400 | }; |
\ | No newline at end of file |