1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _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 |
|
9 | var _react = require('react');
|
10 |
|
11 | var _react2 = _interopRequireDefault(_react);
|
12 |
|
13 | var _Upload = require('./Upload');
|
14 |
|
15 | var _Upload2 = _interopRequireDefault(_Upload);
|
16 |
|
17 | var _uploadList = require('./uploadList');
|
18 |
|
19 | var _uploadList2 = _interopRequireDefault(_uploadList);
|
20 |
|
21 | var _getFileItem = require('./getFileItem');
|
22 |
|
23 | var _getFileItem2 = _interopRequireDefault(_getFileItem);
|
24 |
|
25 | var _classnames = require('classnames');
|
26 |
|
27 | var _classnames2 = _interopRequireDefault(_classnames);
|
28 |
|
29 | var _objectAssign = require('object-assign');
|
30 |
|
31 | var _objectAssign2 = _interopRequireDefault(_objectAssign);
|
32 |
|
33 | var _propTypes = require('prop-types');
|
34 |
|
35 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
36 |
|
37 | var _beeModal = require('bee-modal');
|
38 |
|
39 | var _beeModal2 = _interopRequireDefault(_beeModal);
|
40 |
|
41 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
42 |
|
43 | function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
|
44 |
|
45 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
46 |
|
47 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
48 |
|
49 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
50 |
|
51 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
|
52 |
|
53 |
|
54 |
|
55 | function T() {
|
56 | return true;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | function fileToObject(file) {
|
62 | return {
|
63 | lastModified: file.lastModified,
|
64 | lastModifiedDate: file.lastModifiedDate,
|
65 | name: file.filename || file.name,
|
66 | size: file.size,
|
67 | type: file.type,
|
68 | uid: file.uid,
|
69 | response: file.response,
|
70 | error: file.error,
|
71 | percent: 0,
|
72 | originFileObj: file,
|
73 | status: null
|
74 | };
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | function genPercentAdd() {
|
82 | var k = 0.1;
|
83 | var i = 0.01;
|
84 | var end = 0.98;
|
85 | return function (s) {
|
86 | var start = s;
|
87 | if (start >= end) {
|
88 | return start;
|
89 | }
|
90 |
|
91 | start += k;
|
92 | k = k - i;
|
93 | if (k < 0.001) {
|
94 | k = 0.001;
|
95 | }
|
96 | return start * 100;
|
97 | };
|
98 | }
|
99 |
|
100 | function Dragger(props) {
|
101 | return _react2["default"].createElement(Upload, _extends({}, props, { type: 'drag', style: { height: props.height } }));
|
102 | }
|
103 |
|
104 | function getBase64(file) {
|
105 | return new Promise(function (resolve, reject) {
|
106 | var reader = new FileReader();
|
107 | reader.readAsDataURL(file);
|
108 | reader.onload = function () {
|
109 | return resolve(reader.result);
|
110 | };
|
111 | reader.onerror = function (error) {
|
112 | return reject(error);
|
113 | };
|
114 | });
|
115 | }
|
116 |
|
117 | var File = {
|
118 | uid: _propTypes2["default"].number,
|
119 | size: _propTypes2["default"].number,
|
120 | name: _propTypes2["default"].string,
|
121 | lastModifiedDate: _propTypes2["default"].date,
|
122 | url: _propTypes2["default"].string,
|
123 | status: _propTypes2["default"].oneOf(['error', 'success', 'done', 'uploading', 'removed']),
|
124 | percent: _propTypes2["default"].number,
|
125 | thumbUrl: _propTypes2["default"].string,
|
126 | originFileObj: File
|
127 | };
|
128 |
|
129 | var UploadChangeParam = {
|
130 | file: File,
|
131 | fileList: _propTypes2["default"].array,
|
132 | event: _propTypes2["default"].object
|
133 | };
|
134 |
|
135 | var propTypes = {
|
136 | type: _propTypes2["default"].oneOf(['drag', 'select']),
|
137 | name: _propTypes2["default"].string,
|
138 | defaultFileList: _propTypes2["default"].array,
|
139 | fileList: _propTypes2["default"].array,
|
140 | action: _propTypes2["default"].string,
|
141 | data: _propTypes2["default"].oneOfType([_propTypes2["default"].object, _propTypes2["default"].func]),
|
142 | headers: _propTypes2["default"].oneOfType([_propTypes2["default"].object, _propTypes2["default"].string]),
|
143 | showUploadList: _propTypes2["default"].bool,
|
144 | multiple: _propTypes2["default"].bool,
|
145 | accept: _propTypes2["default"].string,
|
146 | beforeUpload: _propTypes2["default"].func,
|
147 | onChange: _propTypes2["default"].func,
|
148 | listType: _propTypes2["default"].oneOf(['text', 'picture', 'picture-card']),
|
149 | className: _propTypes2["default"].string,
|
150 |
|
151 | onRemove: _propTypes2["default"].func,
|
152 | supportServerRender: _propTypes2["default"].bool,
|
153 | style: _propTypes2["default"].object,
|
154 | disabled: _propTypes2["default"].bool,
|
155 | clsPrefix: _propTypes2["default"].string
|
156 | };
|
157 |
|
158 | var defaultProps = {
|
159 | clsPrefix: 'u-upload',
|
160 | type: 'select',
|
161 | multiple: false,
|
162 | action: '',
|
163 | data: {},
|
164 | accept: '',
|
165 | beforeUpload: T,
|
166 | showUploadList: true,
|
167 | listType: 'text',
|
168 | className: '',
|
169 | disabled: false,
|
170 | supportServerRender: true
|
171 | };
|
172 |
|
173 | var Upload = function (_Component) {
|
174 | _inherits(Upload, _Component);
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | function Upload(props) {
|
184 | _classCallCheck(this, Upload);
|
185 |
|
186 | var _this = _possibleConstructorReturn(this, _Component.call(this, props));
|
187 |
|
188 | _this.state = {
|
189 | previewVisible: false,
|
190 | previewImage: ''
|
191 | };
|
192 |
|
193 | _this.onStart = function (file) {
|
194 | var targetItem = void 0;
|
195 | var nextFileList = _this.state.fileList.concat();
|
196 | if (file.length > 0) {
|
197 | targetItem = file.map(function (f) {
|
198 | var fileObject = fileToObject(f);
|
199 | fileObject.status = 'uploading';
|
200 | return fileObject;
|
201 | });
|
202 | nextFileList = nextFileList.concat(targetItem);
|
203 | } else {
|
204 | targetItem = fileToObject(file);
|
205 | targetItem.status = 'uploading';
|
206 | nextFileList.push(targetItem);
|
207 | }
|
208 | _this.onChange({
|
209 | file: targetItem,
|
210 | fileList: nextFileList
|
211 | });
|
212 |
|
213 |
|
214 | if (!window.FormData) {
|
215 | _this.autoUpdateProgress(0, targetItem);
|
216 | }
|
217 | };
|
218 |
|
219 | _this.onSuccess = function (response, file) {
|
220 | _this.clearProgressTimer();
|
221 | try {
|
222 | if (typeof response === 'string') {
|
223 | response = JSON.parse(response);
|
224 | }
|
225 | } catch (e) {
|
226 | }
|
227 | var fileList = _this.state.fileList;
|
228 | var targetItem = (0, _getFileItem2["default"])(file, fileList);
|
229 |
|
230 | if (!targetItem) {
|
231 | return;
|
232 | }
|
233 | targetItem.status = 'done';
|
234 | targetItem.response = response;
|
235 | _this.onChange({
|
236 | file: targetItem,
|
237 | fileList: fileList
|
238 | });
|
239 | };
|
240 |
|
241 | _this.onProgress = function (e, file) {
|
242 | var fileList = _this.state.fileList;
|
243 | var targetItem = (0, _getFileItem2["default"])(file, fileList);
|
244 |
|
245 | if (!targetItem) {
|
246 | return;
|
247 | }
|
248 | targetItem.percent = e.percent;
|
249 | _this.onChange({
|
250 | event: e,
|
251 | file: targetItem,
|
252 | fileList: _this.state.fileList
|
253 | });
|
254 | };
|
255 |
|
256 | _this.onError = function (error, response, file) {
|
257 | _this.clearProgressTimer();
|
258 | var fileList = _this.state.fileList;
|
259 | var targetItem = (0, _getFileItem2["default"])(file, fileList);
|
260 |
|
261 | if (!targetItem) {
|
262 | return;
|
263 | }
|
264 | targetItem.error = error;
|
265 | targetItem.response = response;
|
266 | targetItem.status = 'error';
|
267 | _this.handleRemove(targetItem);
|
268 | };
|
269 |
|
270 | _this.handleManualRemove = function (file) {
|
271 | _this.refs.upload.abort(file);
|
272 | file.status = 'removed';
|
273 | _this.handleRemove(file);
|
274 | };
|
275 |
|
276 | _this.onChange = function (info) {
|
277 | if (!('fileList' in _this.props)) {
|
278 | _this.setState({ fileList: info.fileList });
|
279 | }
|
280 |
|
281 | var onChange = _this.props.onChange;
|
282 | if (onChange) {
|
283 | onChange(info);
|
284 | }
|
285 | };
|
286 |
|
287 | _this.onFileDrop = function (e) {
|
288 | _this.setState({
|
289 | dragState: e.type
|
290 | });
|
291 | };
|
292 |
|
293 | _this.handlePreview = function (file) {
|
294 | var displayPreview = function displayPreview() {
|
295 | _this.setState({
|
296 | previewImage: file.url || file.thumbUrl,
|
297 | previewVisible: true
|
298 | });
|
299 | };
|
300 | if (!file.url && !file.thumbUrl) {
|
301 | getBase64(file.originFileObj).then(displayPreview);
|
302 | } else {
|
303 | displayPreview();
|
304 | }
|
305 | };
|
306 |
|
307 | _this.handleCancel = function () {
|
308 | return _this.setState({ previewVisible: false });
|
309 | };
|
310 |
|
311 | _this.state = {
|
312 | fileList: _this.props.fileList || _this.props.defaultFileList || [],
|
313 | dragState: 'drop'
|
314 | };
|
315 | return _this;
|
316 | }
|
317 |
|
318 | Upload.prototype.autoUpdateProgress = function autoUpdateProgress(_, file) {
|
319 | var _this2 = this;
|
320 |
|
321 | var getPercent = genPercentAdd();
|
322 | var curPercent = 0;
|
323 | this.progressTimer = setInterval(function () {
|
324 | curPercent = getPercent(curPercent);
|
325 | _this2.onProgress({
|
326 | percent: curPercent
|
327 | }, file);
|
328 | }, 200);
|
329 | };
|
330 |
|
331 | Upload.prototype.removeFile = function removeFile(file) {
|
332 | var fileList = this.state.fileList;
|
333 | var targetItem = (0, _getFileItem2["default"])(file, fileList);
|
334 | var index = fileList.indexOf(targetItem);
|
335 | if (index !== -1) {
|
336 | fileList.splice(index, 1);
|
337 | return fileList;
|
338 | }
|
339 | return null;
|
340 | };
|
341 |
|
342 | Upload.prototype.handleRemove = function handleRemove(file) {
|
343 | var onRemove = this.props.onRemove;
|
344 | if (onRemove) {
|
345 | onRemove(file);
|
346 | }
|
347 | var fileList = this.removeFile(file);
|
348 | if (fileList) {
|
349 | this.onChange({
|
350 | file: file,
|
351 | fileList: fileList
|
352 | });
|
353 | }
|
354 | };
|
355 |
|
356 | Upload.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
357 | if ('fileList' in nextProps) {
|
358 | this.setState({
|
359 | fileList: nextProps.fileList || []
|
360 | });
|
361 | }
|
362 | };
|
363 |
|
364 | Upload.prototype.clearProgressTimer = function clearProgressTimer() {
|
365 | clearInterval(this.progressTimer);
|
366 | };
|
367 |
|
368 | Upload.prototype.render = function render() {
|
369 | var _classNames2;
|
370 |
|
371 | var _props = this.props,
|
372 | _props$clsPrefix = _props.clsPrefix,
|
373 | clsPrefix = _props$clsPrefix === undefined ? '' : _props$clsPrefix,
|
374 | showUploadList = _props.showUploadList,
|
375 | listType = _props.listType,
|
376 | type = _props.type,
|
377 | disabled = _props.disabled,
|
378 | children = _props.children,
|
379 | className = _props.className;
|
380 | var showRemoveIcon = showUploadList.showRemoveIcon,
|
381 | showPreviewIcon = showUploadList.showPreviewIcon;
|
382 | var _state = this.state,
|
383 | previewVisible = _state.previewVisible,
|
384 | previewImage = _state.previewImage;
|
385 |
|
386 |
|
387 | var rcUploadProps = (0, _objectAssign2["default"])({}, this.props, {
|
388 | onStart: this.onStart,
|
389 | onError: this.onError,
|
390 | onProgress: this.onProgress,
|
391 | onSuccess: this.onSuccess
|
392 | });
|
393 | delete rcUploadProps.className;
|
394 |
|
395 | var uploadList = showUploadList ? _react2["default"].createElement(_uploadList2["default"], {
|
396 | listType: listType,
|
397 | items: this.state.fileList,
|
398 | onPreview: this.handlePreview,
|
399 | onRemove: this.handleManualRemove,
|
400 | showRemoveIcon: !disabled && showRemoveIcon,
|
401 | showPreviewIcon: showPreviewIcon
|
402 | }) : null;
|
403 |
|
404 | if (type === 'drag') {
|
405 | var _classNames;
|
406 |
|
407 | var dragCls = (0, _classnames2["default"])(clsPrefix, (_classNames = {}, _defineProperty(_classNames, clsPrefix + '-drag', true), _defineProperty(_classNames, clsPrefix + '-drag-uploading', this.state.fileList.some(function (file) {
|
408 | return file.status === 'uploading';
|
409 | })), _defineProperty(_classNames, clsPrefix + '-drag-hover', this.state.dragState === 'dragover'), _defineProperty(_classNames, clsPrefix + '-disabled', disabled), _classNames));
|
410 | return _react2["default"].createElement(
|
411 | 'span',
|
412 | { className: className },
|
413 | _react2["default"].createElement(
|
414 | 'div',
|
415 | {
|
416 | className: dragCls,
|
417 | onDrop: this.onFileDrop,
|
418 | onDragOver: this.onFileDrop,
|
419 | onDragLeave: this.onFileDrop
|
420 | },
|
421 | _react2["default"].createElement(
|
422 | _Upload2["default"],
|
423 | _extends({}, rcUploadProps, { ref: 'upload', className: clsPrefix + '-btn' }),
|
424 | _react2["default"].createElement(
|
425 | 'div',
|
426 | { className: clsPrefix + '-drag-container' },
|
427 | children
|
428 | )
|
429 | )
|
430 | ),
|
431 | uploadList
|
432 | );
|
433 | }
|
434 |
|
435 | var uploadButtonCls = (0, _classnames2["default"])(clsPrefix, (_classNames2 = {}, _defineProperty(_classNames2, clsPrefix + '-select', true), _defineProperty(_classNames2, clsPrefix + '-select-' + listType, true), _defineProperty(_classNames2, clsPrefix + '-disabled', disabled), _classNames2));
|
436 |
|
437 | var uploadButton = _react2["default"].createElement(
|
438 | 'div',
|
439 | { className: uploadButtonCls, style: { display: children ? '' : 'none' } },
|
440 | _react2["default"].createElement(_Upload2["default"], _extends({}, rcUploadProps, { ref: 'upload' }))
|
441 | );
|
442 |
|
443 | if (listType === 'picture-card') {
|
444 | return _react2["default"].createElement(
|
445 | 'div',
|
446 | null,
|
447 | _react2["default"].createElement(
|
448 | 'span',
|
449 | { className: className },
|
450 | uploadList,
|
451 | uploadButton
|
452 | ),
|
453 | _react2["default"].createElement(
|
454 | _beeModal2["default"],
|
455 | { show: previewVisible, onHide: this.handleCancel },
|
456 | _react2["default"].createElement(
|
457 | _beeModal2["default"].Header,
|
458 | { closeButton: true },
|
459 | _react2["default"].createElement(
|
460 | _beeModal2["default"].Title,
|
461 | null,
|
462 | '\u5927\u56FE\u9884\u89C8'
|
463 | )
|
464 | ),
|
465 | _react2["default"].createElement(
|
466 | _beeModal2["default"].Body,
|
467 | null,
|
468 | _react2["default"].createElement('img', { alt: 'example', style: { width: '100%' }, src: previewImage })
|
469 | )
|
470 | )
|
471 | );
|
472 | }
|
473 | return _react2["default"].createElement(
|
474 | 'span',
|
475 | { className: className },
|
476 | uploadButton,
|
477 | uploadList
|
478 | );
|
479 | };
|
480 |
|
481 | return Upload;
|
482 | }(_react.Component);
|
483 |
|
484 | Upload.propTypes = propTypes;
|
485 | Upload.defaultProps = defaultProps;
|
486 | Upload.Dragger = Dragger;
|
487 | exports["default"] = Upload;
|
488 | module.exports = exports['default']; |
\ | No newline at end of file |