2015-09-15 16:35:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
2015-11-09 16:58:35 +01:00
|
|
|
import Glyphicon from 'react-bootstrap/lib/Glyphicon';
|
|
|
|
|
2015-09-15 16:35:45 +02:00
|
|
|
import { displayValidProgressFilesFilter } from '../react_s3_fine_uploader_utils';
|
2015-09-16 09:47:22 +02:00
|
|
|
import { getLangText } from '../../../utils/lang_utils';
|
2015-11-09 16:58:35 +01:00
|
|
|
import { truncateTextAtCharIndex } from '../../../utils/general_utils';
|
2015-09-16 09:47:22 +02:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
const { func, array, bool, shape, string } = React.PropTypes;
|
2015-09-15 16:35:45 +02:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
let UploadButton = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
onDrop: func.isRequired,
|
|
|
|
filesToUpload: array,
|
|
|
|
multiple: bool,
|
2015-11-12 13:08:23 +01:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
// For simplification purposes we're just going to use this prop as a
|
|
|
|
// label for the upload button
|
|
|
|
fileClassToUpload: shape({
|
|
|
|
singular: string,
|
|
|
|
plural: string
|
|
|
|
}),
|
2015-11-12 13:08:23 +01:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
allowedExtensions: string,
|
2015-11-12 13:08:23 +01:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
handleCancelFile: func // provided by ReactS3FineUploader
|
|
|
|
},
|
2015-11-12 13:08:23 +01:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
handleDrop(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
let files = event.target.files;
|
2015-11-12 13:08:23 +01:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
if(typeof this.props.onDrop === 'function' && files) {
|
|
|
|
this.props.onDrop(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
2015-11-12 13:08:23 +01:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
getUploadingFiles() {
|
|
|
|
return this.props.filesToUpload.filter((file) => file.status === 'uploading');
|
|
|
|
},
|
2015-11-12 13:08:23 +01:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
getUploadedFile() {
|
|
|
|
return this.props.filesToUpload.filter((file) => file.status === 'upload successful')[0];
|
|
|
|
},
|
2015-11-12 13:08:23 +01:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
handleOnClick() {
|
|
|
|
const uploadingFiles = this.getUploadingFiles();
|
|
|
|
const uploadedFile = this.getUploadedFile();
|
2015-11-12 13:08:23 +01:00
|
|
|
|
2015-11-12 14:18:08 +01:00
|
|
|
if(uploadedFile) {
|
|
|
|
this.props.handleCancelFile(uploadedFile.id);
|
|
|
|
}
|
|
|
|
if(uploadingFiles.length === 0) {
|
2015-11-12 13:08:23 +01:00
|
|
|
// We only want the button to be clickable if there are no files currently uploading
|
2015-11-12 14:18:08 +01:00
|
|
|
|
|
|
|
// Firefox only recognizes the simulated mouse click if bubbles is set to true,
|
|
|
|
// but since Google Chrome propagates the event much further than needed, we
|
|
|
|
// need to stop propagation as soon as the event is created
|
|
|
|
var evt = new MouseEvent('click', {
|
|
|
|
view: window,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true
|
|
|
|
});
|
|
|
|
|
|
|
|
evt.stopPropagation();
|
|
|
|
this.refs.fileinput.getDOMNode().dispatchEvent(evt);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getButtonLabel() {
|
|
|
|
let { filesToUpload, fileClassToUpload } = this.props;
|
|
|
|
|
|
|
|
// filter invalid files that might have been deleted or canceled...
|
|
|
|
filesToUpload = filesToUpload.filter(displayValidProgressFilesFilter);
|
|
|
|
|
|
|
|
if(this.getUploadingFiles().length !== 0) {
|
|
|
|
return getLangText('Upload progress') + ': ' + Math.ceil(filesToUpload[0].progress) + '%';
|
|
|
|
} else {
|
|
|
|
return fileClassToUpload.singular;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getUploadedFileLabel() {
|
|
|
|
const uploadedFile = this.getUploadedFile();
|
|
|
|
|
|
|
|
if(uploadedFile) {
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
<Glyphicon glyph="ok" />
|
2015-11-13 09:48:22 +01:00
|
|
|
{' ' + truncateTextAtCharIndex(uploadedFile.name, 40)}
|
2015-11-12 14:18:08 +01:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
} else {
|
2015-11-09 16:58:35 +01:00
|
|
|
return (
|
2015-11-12 14:18:08 +01:00
|
|
|
<span>{getLangText('No file chosen')}</span>
|
2015-11-09 16:58:35 +01:00
|
|
|
);
|
2015-09-15 16:35:45 +02:00
|
|
|
}
|
2015-11-12 14:18:08 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let { multiple,
|
|
|
|
allowedExtensions } = this.props;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We do not want a button that submits here.
|
|
|
|
* As UploadButton could be used in forms that want to be submitted independent
|
|
|
|
* of clicking the selector.
|
|
|
|
* Therefore the wrapping component needs to be an `anchor` tag instead of a `button`
|
|
|
|
*/
|
|
|
|
return (
|
|
|
|
<div className="upload-button-wrapper">
|
|
|
|
<a
|
|
|
|
onClick={this.handleOnClick}
|
|
|
|
className="btn btn-default btn-sm margin-left-2px"
|
|
|
|
disabled={this.getUploadingFiles().length !== 0}>
|
|
|
|
{this.getButtonLabel()}
|
|
|
|
<input
|
|
|
|
multiple={multiple}
|
|
|
|
ref="fileinput"
|
|
|
|
type="file"
|
|
|
|
style={{
|
|
|
|
display: 'none',
|
|
|
|
height: 0,
|
|
|
|
width: 0
|
|
|
|
}}
|
|
|
|
onChange={this.handleDrop}
|
|
|
|
accept={allowedExtensions}/>
|
|
|
|
</a>
|
|
|
|
{this.getUploadedFileLabel()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default UploadButton;
|