2018-01-03 02:12:57 +01:00
|
|
|
import React from 'react';
|
2018-01-18 00:00:03 +01:00
|
|
|
import { validateFile } from 'utils/file';
|
|
|
|
import Preview from 'components/Preview';
|
2018-01-03 02:12:57 +01:00
|
|
|
|
2018-01-11 20:41:12 +01:00
|
|
|
class Dropzone extends React.Component {
|
2018-01-03 02:12:57 +01:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2018-01-11 01:30:17 +01:00
|
|
|
dragOver : false,
|
|
|
|
mouseOver : false,
|
|
|
|
dimPreview: false,
|
2018-01-13 00:02:42 +01:00
|
|
|
};
|
2018-01-11 01:30:17 +01:00
|
|
|
this.handleDrop = this.handleDrop.bind(this);
|
|
|
|
this.handleDragOver = this.handleDragOver.bind(this);
|
|
|
|
this.handleDragEnd = this.handleDragEnd.bind(this);
|
|
|
|
this.handleDragEnter = this.handleDragEnter.bind(this);
|
|
|
|
this.handleDragLeave = this.handleDragLeave.bind(this);
|
|
|
|
this.handleMouseEnter = this.handleMouseEnter.bind(this);
|
|
|
|
this.handleMouseLeave = this.handleMouseLeave.bind(this);
|
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.handleFileInput = this.handleFileInput.bind(this);
|
|
|
|
this.selectFile = this.selectFile.bind(this);
|
|
|
|
}
|
|
|
|
handleDrop (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.setState({dragOver: false});
|
|
|
|
// if dropped items aren't files, reject them
|
|
|
|
const dt = event.dataTransfer;
|
|
|
|
console.log('dt', dt);
|
|
|
|
if (dt.items) {
|
2018-02-06 03:14:12 +01:00
|
|
|
if (dt.items[0].kind === 'file') {
|
2018-01-11 01:30:17 +01:00
|
|
|
const droppedFile = dt.items[0].getAsFile();
|
|
|
|
this.selectFile(droppedFile);
|
|
|
|
}
|
2018-01-03 02:12:57 +01:00
|
|
|
}
|
2018-01-11 01:30:17 +01:00
|
|
|
}
|
|
|
|
handleDragOver (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
handleDragEnd (event) {
|
|
|
|
var dt = event.dataTransfer;
|
|
|
|
if (dt.items) {
|
|
|
|
for (var i = 0; i < dt.items.length; i++) {
|
|
|
|
dt.items.remove(i);
|
|
|
|
}
|
2018-01-03 02:12:57 +01:00
|
|
|
} else {
|
2018-01-11 01:30:17 +01:00
|
|
|
event.dataTransfer.clearData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handleDragEnter () {
|
|
|
|
this.setState({dragOver: true, dimPreview: true});
|
|
|
|
}
|
|
|
|
handleDragLeave () {
|
|
|
|
this.setState({dragOver: false, dimPreview: false});
|
|
|
|
}
|
|
|
|
handleMouseEnter () {
|
|
|
|
this.setState({mouseOver: true, dimPreview: true});
|
|
|
|
}
|
|
|
|
handleMouseLeave () {
|
|
|
|
this.setState({mouseOver: false, dimPreview: false});
|
|
|
|
}
|
|
|
|
handleClick (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
// trigger file input
|
|
|
|
document.getElementById('file_input').click();
|
|
|
|
}
|
|
|
|
handleFileInput (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const fileList = event.target.files;
|
|
|
|
this.selectFile(fileList[0]);
|
|
|
|
}
|
|
|
|
selectFile (file) {
|
|
|
|
if (file) {
|
|
|
|
try {
|
|
|
|
validateFile(file); // validate the file's name, type, and size
|
|
|
|
} catch (error) {
|
2018-02-26 02:47:22 +01:00
|
|
|
return this.props.setFileError(error.message);
|
2018-01-11 01:30:17 +01:00
|
|
|
}
|
|
|
|
// stage it so it will be ready when the publish button is clicked
|
2018-02-26 02:47:22 +01:00
|
|
|
this.props.clearFileError(null);
|
|
|
|
this.props.selectFile(file);
|
2018-01-03 02:12:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
render () {
|
|
|
|
return (
|
2018-02-26 02:38:14 +01:00
|
|
|
<div className='row row--tall flex-container--column'>
|
2018-01-11 01:30:17 +01:00
|
|
|
<form>
|
2018-02-26 02:38:14 +01:00
|
|
|
<input className='input-file' type='file' id='file_input' name='file_input' accept='video/*,image/*' onChange={this.handleFileInput} encType='multipart/form-data' />
|
2018-01-11 01:30:17 +01:00
|
|
|
</form>
|
2018-02-26 02:38:14 +01:00
|
|
|
<div id='preview-dropzone' className={'row row--padded row--tall dropzone' + (this.state.dragOver ? ' dropzone--drag-over' : '')} onDrop={this.handleDrop} onDragOver={this.handleDragOver} onDragEnd={this.handleDragEnd} onDragEnter={this.handleDragEnter} onDragLeave={this.handleDragLeave} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave} onClick={this.handleClick}>
|
2018-01-11 01:30:17 +01:00
|
|
|
{this.props.file ? (
|
|
|
|
<div>
|
2018-01-11 20:41:12 +01:00
|
|
|
<Preview
|
|
|
|
dimPreview={this.state.dimPreview}
|
|
|
|
file={this.props.file}
|
|
|
|
thumbnail={this.props.thumbnail}
|
|
|
|
/>
|
2018-02-26 02:38:14 +01:00
|
|
|
<div id='dropzone-text-holder' className={'flex-container--column flex-container--center-center'}>
|
|
|
|
{ this.state.dragOver ? (
|
|
|
|
<div id='dropzone-dragover'>
|
|
|
|
<p className='blue'>Drop it.</p>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
null
|
|
|
|
)}
|
|
|
|
{ this.state.mouseOver ? (
|
|
|
|
<div id='dropzone-instructions'>
|
|
|
|
<p className='info-message-placeholder info-message--failure' id='input-error-file-selection'>{this.props.fileError}</p>
|
|
|
|
<p>Drag & drop image or video here to publish</p>
|
|
|
|
<p className='fine-print'>OR</p>
|
|
|
|
<p className='blue--underlined'>CHOOSE FILE</p>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
null
|
|
|
|
)}
|
2018-01-11 01:30:17 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
2018-02-26 02:38:14 +01:00
|
|
|
<div id='dropzone-text-holder' className={'flex-container--column flex-container--center-center'}>
|
2018-01-11 01:30:17 +01:00
|
|
|
{ this.state.dragOver ? (
|
2018-02-26 02:38:14 +01:00
|
|
|
<div id='dropzone-dragover'>
|
|
|
|
<p className='blue'>Drop it.</p>
|
2018-01-11 01:30:17 +01:00
|
|
|
</div>
|
|
|
|
) : (
|
2018-02-26 02:38:14 +01:00
|
|
|
<div id='dropzone-instructions'>
|
|
|
|
<p className='info-message-placeholder info-message--failure' id='input-error-file-selection'>{this.props.fileError}</p>
|
2018-01-11 01:30:17 +01:00
|
|
|
<p>Drag & drop image or video here to publish</p>
|
2018-02-26 02:38:14 +01:00
|
|
|
<p className='fine-print'>OR</p>
|
|
|
|
<p className='blue--underlined'>CHOOSE FILE</p>
|
2018-01-11 01:30:17 +01:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
2018-01-03 02:12:57 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-17 19:49:57 +01:00
|
|
|
export default Dropzone;
|