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 _classnames = require('classnames');
|
14 |
|
15 | var _classnames2 = _interopRequireDefault(_classnames);
|
16 |
|
17 | var _request = require('./request');
|
18 |
|
19 | var _request2 = _interopRequireDefault(_request);
|
20 |
|
21 | var _uid = require('./uid');
|
22 |
|
23 | var _uid2 = _interopRequireDefault(_uid);
|
24 |
|
25 | var _propTypes = require('prop-types');
|
26 |
|
27 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
30 |
|
31 | 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; }
|
32 |
|
33 | 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; }
|
34 |
|
35 | function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
|
36 |
|
37 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
38 |
|
39 | 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; }
|
40 |
|
41 | 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); } |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | var propTypes = {
|
48 | component: _propTypes2["default"].string,
|
49 | style: _propTypes2["default"].object,
|
50 | clsPrefix: _propTypes2["default"].string,
|
51 | className: _propTypes2["default"].string,
|
52 | multiple: _propTypes2["default"].bool,
|
53 | disabled: _propTypes2["default"].bool,
|
54 | accept: _propTypes2["default"].string,
|
55 | children: _propTypes2["default"].any,
|
56 | onStart: _propTypes2["default"].func,
|
57 | data: _propTypes2["default"].oneOfType([_propTypes2["default"].object, _propTypes2["default"].func]),
|
58 | headers: _propTypes2["default"].object,
|
59 | beforeUpload: _propTypes2["default"].func,
|
60 | customRequest: _propTypes2["default"].func,
|
61 | withCredentials: _propTypes2["default"].bool
|
62 | };
|
63 |
|
64 | var AjaxUploader = function (_Component) {
|
65 | _inherits(AjaxUploader, _Component);
|
66 |
|
67 | function AjaxUploader(props) {
|
68 | _classCallCheck(this, AjaxUploader);
|
69 |
|
70 | var _this = _possibleConstructorReturn(this, _Component.call(this, props));
|
71 |
|
72 | _this.reqs = {};
|
73 | _this.state = {
|
74 | uid: (0, _uid2["default"])()
|
75 | };
|
76 |
|
77 | _this.onChange = _this.onChange.bind(_this);
|
78 | _this.onClick = _this.onClick.bind(_this);
|
79 | _this.onKeyDown = _this.onKeyDown.bind(_this);
|
80 | _this.onFileDrop = _this.onFileDrop.bind(_this);
|
81 | _this.uploadFiles = _this.uploadFiles.bind(_this);
|
82 |
|
83 | _this.upload = _this.upload.bind(_this);
|
84 | _this.post = _this.post.bind(_this);
|
85 | _this.reset = _this.reset.bind(_this);
|
86 | _this.abort = _this.abort.bind(_this);
|
87 | return _this;
|
88 | }
|
89 |
|
90 | AjaxUploader.prototype.componentWillUnmount = function componentWillUnmount() {
|
91 | this.mounted = false;
|
92 | this.abort();
|
93 | };
|
94 |
|
95 | AjaxUploader.prototype.componentDidMount = function componentDidMount() {
|
96 | this.mounted = true;
|
97 | };
|
98 |
|
99 | AjaxUploader.prototype.onChange = function onChange(e) {
|
100 | var files = e.target.files;
|
101 | this.uploadFiles(files);
|
102 | this.reset();
|
103 | };
|
104 |
|
105 | AjaxUploader.prototype.onClick = function onClick() {
|
106 | var el = this.refs.file;
|
107 | if (!el) {
|
108 | return;
|
109 | }
|
110 | el.click();
|
111 | };
|
112 |
|
113 | AjaxUploader.prototype.onKeyDown = function onKeyDown(e) {
|
114 | if (e.key === 'Enter') {
|
115 | this.onClick();
|
116 | }
|
117 | };
|
118 |
|
119 | AjaxUploader.prototype.onFileDrop = function onFileDrop(e) {
|
120 | if (e.type === 'dragover') {
|
121 | e.preventDefault();
|
122 | return;
|
123 | }
|
124 |
|
125 | var files = e.dataTransfer.files;
|
126 | this.uploadFiles(files);
|
127 |
|
128 | e.preventDefault();
|
129 | };
|
130 |
|
131 | AjaxUploader.prototype.uploadFiles = function uploadFiles(files) {
|
132 | var postFiles = Array.prototype.slice.call(files);
|
133 | var len = postFiles.length;
|
134 | var mergeFiles = this.props.mergeFiles;
|
135 |
|
136 | if (mergeFiles) {
|
137 | var file = { filesArray: postFiles, uid: (0, _uid2["default"])() };
|
138 | this.upload(file, postFiles);
|
139 | } else {
|
140 | for (var i = 0; i < len; i++) {
|
141 | var _file = postFiles[i];
|
142 | _file.uid = (0, _uid2["default"])();
|
143 | this.upload(_file, postFiles);
|
144 | }
|
145 | }
|
146 | };
|
147 |
|
148 | AjaxUploader.prototype.upload = function upload(file, fileList) {
|
149 | var _this2 = this;
|
150 |
|
151 | var props = this.props;
|
152 |
|
153 | if (!props.beforeUpload) {
|
154 |
|
155 | return setTimeout(function () {
|
156 | return _this2.post(file);
|
157 | }, 0);
|
158 | }
|
159 |
|
160 | var before = props.beforeUpload(file, fileList);
|
161 | if (before && before.then) {
|
162 | before.then(function (processedFile) {
|
163 | var processedFileType = Object.prototype.toString.call(processedFile);
|
164 | if (processedFileType === '[object File]' || processedFileType === '[object Blob]') {
|
165 | _this2.post(processedFile);
|
166 | } else {
|
167 | _this2.post(file);
|
168 | }
|
169 | });
|
170 | } else if (before !== false) {
|
171 | setTimeout(function () {
|
172 | return _this2.post(file);
|
173 | }, 0);
|
174 | }
|
175 | };
|
176 |
|
177 | AjaxUploader.prototype.post = function post(file) {
|
178 | var _this3 = this;
|
179 |
|
180 | if (!this.mounted) {
|
181 | return;
|
182 | }
|
183 | var props = this.props;
|
184 | var data = props.data;
|
185 | var onStart = props.onStart;
|
186 |
|
187 | if (typeof data === 'function') {
|
188 | data = data(file);
|
189 | }
|
190 | var uid = file.uid;
|
191 |
|
192 | var request = props.customRequest || _request2["default"];
|
193 | this.reqs[uid] = request({
|
194 | action: props.action,
|
195 | filename: props.name,
|
196 | file: file.filesArray || file,
|
197 | data: data,
|
198 | headers: props.headers,
|
199 | withCredentials: props.withCredentials,
|
200 | onProgress: function onProgress(e) {
|
201 | props.onProgress(e, file);
|
202 | },
|
203 | onSuccess: function onSuccess(ret) {
|
204 | delete _this3.reqs[uid];
|
205 | props.onSuccess(ret, file);
|
206 | },
|
207 | onError: function onError(err, ret) {
|
208 | delete _this3.reqs[uid];
|
209 | props.onError(err, ret, file);
|
210 | }
|
211 | });
|
212 | onStart(file);
|
213 | };
|
214 |
|
215 | AjaxUploader.prototype.reset = function reset() {
|
216 | this.setState({
|
217 | uid: (0, _uid2["default"])()
|
218 | });
|
219 | };
|
220 |
|
221 | AjaxUploader.prototype.abort = function abort(file) {
|
222 | var reqs = this.reqs;
|
223 |
|
224 | if (file) {
|
225 | var uid = file;
|
226 | if (file && file.uid) {
|
227 | uid = file.uid;
|
228 | }
|
229 | if (reqs[uid] && reqs[uid].abort) {
|
230 | reqs[uid].abort();
|
231 | }
|
232 | delete reqs[uid];
|
233 | } else {
|
234 | Object.keys(reqs).forEach(function (uid) {
|
235 | if (reqs[uid] && reqs[uid].abort) {
|
236 | reqs[uid].abort();
|
237 | }
|
238 | delete reqs[uid];
|
239 | });
|
240 | }
|
241 | };
|
242 |
|
243 | AjaxUploader.prototype.render = function render() {
|
244 | var _classNames;
|
245 |
|
246 | var _props = this.props,
|
247 | Tag = _props.component,
|
248 | clsPrefix = _props.clsPrefix,
|
249 | className = _props.className,
|
250 | disabled = _props.disabled,
|
251 | style = _props.style,
|
252 | multiple = _props.multiple,
|
253 | accept = _props.accept,
|
254 | children = _props.children,
|
255 | name = _props.name,
|
256 | action = _props.action,
|
257 | headers = _props.headers,
|
258 | data = _props.data,
|
259 | type = _props.type,
|
260 | listType = _props.listType,
|
261 | fileList = _props.fileList,
|
262 | defaultFileList = _props.defaultFileList,
|
263 | size = _props.size,
|
264 | beforeUpload = _props.beforeUpload,
|
265 | showUploadList = _props.showUploadList,
|
266 | supportServerRender = _props.supportServerRender,
|
267 | onStart = _props.onStart,
|
268 | onSuccess = _props.onSuccess,
|
269 | multipart = _props.multipart,
|
270 | onReady = _props.onReady,
|
271 | customRequest = _props.customRequest,
|
272 | withCredentials = _props.withCredentials,
|
273 | onChange = _props.onChange,
|
274 | others = _objectWithoutProperties(_props, ['component', 'clsPrefix', 'className', 'disabled', 'style', 'multiple', 'accept', 'children', 'name', 'action', 'headers', 'data', 'type', 'listType', 'fileList', 'defaultFileList', 'size', 'beforeUpload', 'showUploadList', 'supportServerRender', 'onStart', 'onSuccess', 'multipart', 'onReady', 'customRequest', 'withCredentials', 'onChange']);
|
275 |
|
276 | var cls = (0, _classnames2["default"])((_classNames = {}, _defineProperty(_classNames, clsPrefix, true), _defineProperty(_classNames, clsPrefix + '-disabled', disabled), _defineProperty(_classNames, className, className), _classNames));
|
277 | var events = disabled ? {} : {
|
278 | onClick: this.onClick,
|
279 | onKeyDown: this.onKeyDown,
|
280 | onDrop: this.onFileDrop,
|
281 | onDragOver: this.onFileDrop,
|
282 | tabIndex: '0'
|
283 | };
|
284 | return _react2["default"].createElement(
|
285 | Tag,
|
286 | _extends({}, events, {
|
287 | className: cls,
|
288 | role: 'button',
|
289 | style: style
|
290 | }, others),
|
291 | _react2["default"].createElement('input', {
|
292 | type: 'file',
|
293 | ref: 'file',
|
294 | key: this.state.uid,
|
295 | style: { display: 'none' },
|
296 | accept: accept,
|
297 | multiple: multiple,
|
298 | onChange: this.onChange
|
299 | }),
|
300 | children
|
301 | );
|
302 | };
|
303 |
|
304 | return AjaxUploader;
|
305 | }(_react.Component);
|
306 |
|
307 | ;
|
308 | AjaxUploader.propTypes = propTypes;
|
309 | exports["default"] = AjaxUploader;
|
310 | module.exports = exports['default']; |
\ | No newline at end of file |