/
AssetUploader.jsx
69 lines (63 loc) · 1.84 KB
/
AssetUploader.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* A component for managing hosted assets.
*/
module.exports = React.createClass({
propTypes: {
onUploadStart: React.PropTypes.func.isRequired,
onUploadDone: React.PropTypes.func.isRequired,
channelId: React.PropTypes.string.isRequired,
typeFilter: React.PropTypes.string,
uploadsEnabled: React.PropTypes.bool.isRequired
},
componentDidMount: function () {
var props = this.props;
$(this.refs.uploader).fileupload({
dataType: 'json',
url: '/v3/assets/' + props.channelId + '/',
// prevent fileupload from replacing the input DOM element, which
// React does not like
replaceFileInput: false,
add: function (e, data) {
props.onUploadStart();
data.submit();
},
done: function (e, data) {
props.onUploadDone(data.result);
},
error: function (e, data) {
props.onUploadError(e.status);
}
});
},
componentWillUnmount: function () {
$(this.refs.uploader).fileupload('destroy');
},
/**
* We've hidden the <input type="file"/> and replaced it with a big button.
* Forward clicks on the button to the hidden file input.
*/
fileUploadClicked: function () {
this.refs.uploader.click();
},
render: function () {
// NOTE: IE9 will ignore accept, which means on this browser we can end
// up uploading files that dont match typeFilter
return (
<span>
<input
ref="uploader"
type="file"
style={{display: 'none'}}
accept={(this.props.typeFilter || '*') + '/*'}/>
<button
onClick={this.fileUploadClicked}
className="share"
id="upload-asset"
disabled={!this.props.uploadsEnabled}>
<i className="fa fa-upload"></i>
Upload File
</button>
</span>
);
}
});