2015-09-10 10:51:47 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import ReactS3FineUploader from '../ascribe_uploader/react_s3_fine_uploader';
|
2015-11-10 17:35:57 +01:00
|
|
|
import FileDragAndDrop from '../ascribe_uploader/ascribe_file_drag_and_drop/file_drag_and_drop';
|
2015-09-10 10:51:47 +02:00
|
|
|
|
|
|
|
import AppConstants from '../../constants/application_constants';
|
|
|
|
|
|
|
|
import { getCookie } from '../../utils/fetch_api_utils';
|
|
|
|
|
2015-11-10 17:35:57 +01:00
|
|
|
|
2015-12-08 18:29:26 +01:00
|
|
|
const { func, bool, shape, string, number, element, oneOf, oneOfType, arrayOf } = React.PropTypes;
|
2015-11-10 17:35:57 +01:00
|
|
|
|
|
|
|
const InputFineUploader = React.createClass({
|
2015-09-10 10:51:47 +02:00
|
|
|
propTypes: {
|
|
|
|
// isFineUploaderActive is used to lock react fine uploader in case
|
|
|
|
// a user is actually not logged in already to prevent him from droping files
|
|
|
|
// before login in
|
2015-11-10 17:35:57 +01:00
|
|
|
isFineUploaderActive: bool,
|
2015-09-21 12:05:42 +02:00
|
|
|
|
2015-09-10 10:51:47 +02:00
|
|
|
// provided by Property
|
2015-11-10 17:35:57 +01:00
|
|
|
disabled: bool,
|
2015-12-08 20:55:13 +01:00
|
|
|
onChange: func,
|
2015-09-14 16:33:32 +02:00
|
|
|
|
2015-12-08 18:09:24 +01:00
|
|
|
// Props for ReactS3FineUploader
|
|
|
|
areAssetsDownloadable: bool,
|
2016-02-05 17:06:16 +01:00
|
|
|
createBlobRoutine: ReactS3FineUploader.propTypes.createBlobRoutine,
|
|
|
|
enableLocalHashing: bool,
|
|
|
|
fileClassToUpload: ReactS3FineUploader.propTypes.fileClassToUpload,
|
|
|
|
fileInputElement: ReactS3FineUploader.propTypes.fileInputElement,
|
|
|
|
isReadyForFormSubmission: func,
|
|
|
|
keyRoutine: ReactS3FineUploader.propTypes.keyRoutine,
|
2015-12-08 18:09:24 +01:00
|
|
|
handleChangedFile: func, // TODO: rename to onChangedFile
|
|
|
|
submitFile: func, // TODO: rename to onSubmitFile
|
2015-12-21 11:53:27 +01:00
|
|
|
onValidationFailed: func,
|
2015-12-08 18:09:24 +01:00
|
|
|
setIsUploadReady: func, //TODO: rename to setIsUploaderValidated
|
2016-02-05 17:06:16 +01:00
|
|
|
setWarning: func,
|
|
|
|
showErrorPrompt: bool,
|
2015-12-08 18:09:24 +01:00
|
|
|
uploadMethod: oneOf(['hash', 'upload']),
|
2016-02-05 17:06:16 +01:00
|
|
|
validation: ReactS3FineUploader.propTypes.validation,
|
2015-09-10 10:51:47 +02:00
|
|
|
},
|
|
|
|
|
2015-11-10 17:35:57 +01:00
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
fileInputElement: FileDragAndDrop
|
|
|
|
};
|
2015-09-10 10:51:47 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
2015-11-10 17:35:57 +01:00
|
|
|
value: null,
|
|
|
|
file: null
|
2015-09-10 10:51:47 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-11-10 17:35:57 +01:00
|
|
|
submitFile(file) {
|
2015-09-10 10:51:47 +02:00
|
|
|
this.setState({
|
2015-11-10 17:35:57 +01:00
|
|
|
file,
|
2015-09-14 14:46:03 +02:00
|
|
|
value: file.key
|
2015-09-10 10:51:47 +02:00
|
|
|
});
|
2015-09-14 16:15:01 +02:00
|
|
|
|
2016-01-18 14:46:39 +01:00
|
|
|
if (this.state.value && typeof this.props.onChange === 'function') {
|
2015-11-10 17:35:57 +01:00
|
|
|
this.props.onChange({ target: { value: this.state.value } });
|
|
|
|
}
|
|
|
|
|
2016-01-18 14:46:39 +01:00
|
|
|
if (typeof this.props.submitFile === 'function') {
|
2015-11-17 11:57:18 +01:00
|
|
|
this.props.submitFile(file);
|
2015-09-14 16:15:01 +02:00
|
|
|
}
|
2015-09-10 10:51:47 +02:00
|
|
|
},
|
|
|
|
|
2015-09-10 11:22:42 +02:00
|
|
|
reset() {
|
|
|
|
this.refs.fineuploader.reset();
|
|
|
|
},
|
2015-09-10 10:51:47 +02:00
|
|
|
|
2015-11-10 17:35:57 +01:00
|
|
|
createBlobRoutine() {
|
|
|
|
const { fineuploader } = this.refs;
|
|
|
|
const { file } = this.state;
|
|
|
|
|
|
|
|
fineuploader.createBlob(file);
|
|
|
|
},
|
|
|
|
|
2015-09-10 11:22:42 +02:00
|
|
|
render() {
|
2016-01-18 14:46:39 +01:00
|
|
|
const { areAssetsDownloadable,
|
|
|
|
createBlobRoutine,
|
2016-02-08 14:08:54 +01:00
|
|
|
enableLocalHashing,
|
2016-01-18 14:46:39 +01:00
|
|
|
disabled,
|
|
|
|
fileClassToUpload,
|
|
|
|
fileInputElement,
|
2016-02-08 14:08:54 +01:00
|
|
|
handleChangedFile,
|
2016-01-18 14:46:39 +01:00
|
|
|
isFineUploaderActive,
|
|
|
|
isReadyForFormSubmission,
|
|
|
|
keyRoutine,
|
|
|
|
onValidationFailed,
|
|
|
|
setIsUploadReady,
|
2016-02-08 14:08:54 +01:00
|
|
|
setWarning,
|
|
|
|
showErrorPrompt,
|
2016-01-18 14:46:39 +01:00
|
|
|
uploadMethod,
|
2016-02-08 14:08:54 +01:00
|
|
|
validation } = this.props;
|
2015-12-04 16:34:25 +01:00
|
|
|
let editable = isFineUploaderActive;
|
2015-09-10 10:51:47 +02:00
|
|
|
|
|
|
|
// if disabled is actually set by property, we want to override
|
|
|
|
// isFineUploaderActive
|
2016-01-18 14:46:39 +01:00
|
|
|
if (typeof disabled !== 'undefined') {
|
2015-12-02 12:37:21 +01:00
|
|
|
editable = !disabled;
|
2015-09-10 10:51:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ReactS3FineUploader
|
2015-09-10 11:22:42 +02:00
|
|
|
ref="fineuploader"
|
2015-11-10 17:35:57 +01:00
|
|
|
fileInputElement={fileInputElement}
|
|
|
|
keyRoutine={keyRoutine}
|
|
|
|
createBlobRoutine={createBlobRoutine}
|
|
|
|
validation={validation}
|
2015-09-14 14:46:03 +02:00
|
|
|
submitFile={this.submitFile}
|
2015-12-21 11:53:27 +01:00
|
|
|
onValidationFailed={onValidationFailed}
|
2015-11-10 17:35:57 +01:00
|
|
|
setIsUploadReady={setIsUploadReady}
|
|
|
|
isReadyForFormSubmission={isReadyForFormSubmission}
|
|
|
|
areAssetsDownloadable={areAssetsDownloadable}
|
2015-09-10 10:51:47 +02:00
|
|
|
areAssetsEditable={editable}
|
2015-12-08 18:23:02 +01:00
|
|
|
setWarning={setWarning}
|
2015-12-08 18:22:11 +01:00
|
|
|
showErrorPrompt={showErrorPrompt}
|
2015-09-10 10:51:47 +02:00
|
|
|
signature={{
|
|
|
|
endpoint: AppConstants.serverUrl + 's3/signature/',
|
|
|
|
customHeaders: {
|
|
|
|
'X-CSRFToken': getCookie(AppConstants.csrftoken)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
deleteFile={{
|
|
|
|
enabled: true,
|
|
|
|
method: 'DELETE',
|
|
|
|
endpoint: AppConstants.serverUrl + 's3/delete',
|
|
|
|
customHeaders: {
|
|
|
|
'X-CSRFToken': getCookie(AppConstants.csrftoken)
|
|
|
|
}
|
|
|
|
}}
|
2015-11-17 11:57:18 +01:00
|
|
|
enableLocalHashing={enableLocalHashing}
|
|
|
|
uploadMethod={uploadMethod}
|
2015-11-17 15:46:46 +01:00
|
|
|
fileClassToUpload={fileClassToUpload}
|
2015-12-08 18:09:24 +01:00
|
|
|
handleChangedFile={handleChangedFile} />
|
2015-09-10 10:51:47 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-30 17:43:20 +01:00
|
|
|
export default InputFineUploader;
|