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