1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 09:35:10 +01:00
onion/js/components/ascribe_uploader/file_drag_and_drop.js

221 lines
8.1 KiB
JavaScript
Raw Normal View History

'use strict';
2015-06-23 10:16:53 +02:00
import React from 'react';
2015-07-24 15:13:20 +02:00
import ProgressBar from 'react-progressbar';
2015-07-07 18:07:12 +02:00
import FileDragAndDropDialog from './file_drag_and_drop_dialog';
2015-06-23 10:16:53 +02:00
import FileDragAndDropPreviewIterator from './file_drag_and_drop_preview_iterator';
import { getLangText } from '../../utils/lang_utils';
2015-06-23 10:16:53 +02:00
// Taken from: https://github.com/fedosejev/react-file-drag-and-drop
2015-06-30 11:56:38 +02:00
let FileDragAndDrop = React.createClass({
2015-06-23 10:16:53 +02:00
propTypes: {
2015-06-29 13:41:37 +02:00
className: React.PropTypes.string,
2015-06-23 10:16:53 +02:00
onDragStart: React.PropTypes.func,
onDrop: React.PropTypes.func.isRequired,
onDrag: React.PropTypes.func,
2015-06-23 10:16:53 +02:00
onDragEnter: React.PropTypes.func,
onLeave: React.PropTypes.func,
onDragLeave: React.PropTypes.func,
2015-06-23 10:16:53 +02:00
onDragOver: React.PropTypes.func,
onDragEnd: React.PropTypes.func,
onInactive: React.PropTypes.func,
2015-06-23 10:16:53 +02:00
filesToUpload: React.PropTypes.array,
handleDeleteFile: React.PropTypes.func,
2015-06-29 10:01:54 +02:00
handleCancelFile: React.PropTypes.func,
2015-06-29 17:37:14 +02:00
handlePauseFile: React.PropTypes.func,
handleResumeFile: React.PropTypes.func,
multiple: React.PropTypes.bool,
2015-06-30 13:53:02 +02:00
dropzoneInactive: React.PropTypes.bool,
areAssetsDownloadable: React.PropTypes.bool,
areAssetsEditable: React.PropTypes.bool,
enableLocalHashing: React.PropTypes.bool,
// triggers a FileDragAndDrop-global spinner
2015-07-24 16:17:27 +02:00
hashingProgress: React.PropTypes.number,
// sets the value of this.state.hashingProgress in reactfineuploader
// to -1 which is code for: aborted
handleCancelHashing: React.PropTypes.func
2015-06-23 10:16:53 +02:00
},
handleDragStart(event) {
if (typeof this.props.onDragStart === 'function') {
this.props.onDragStart(event);
}
},
handleDrag(event) {
if (typeof this.props.onDrag === 'function') {
this.props.onDrag(event);
}
},
handleDragEnd(event) {
if (typeof this.props.onDragEnd === 'function') {
this.props.onDragEnd(event);
}
},
handleDragEnter(event) {
if (typeof this.props.onDragEnter === 'function') {
this.props.onDragEnter(event);
}
},
handleDragLeave(event) {
if (typeof this.props.onDragLeave === 'function') {
this.props.onDragLeave(event);
}
},
handleDragOver(event) {
event.preventDefault();
if (typeof this.props.onDragOver === 'function') {
this.props.onDragOver(event);
}
},
handleDrop(event) {
event.preventDefault();
event.stopPropagation();
let files;
if(this.props.dropzoneInactive) {
// if there is a handle function for doing stuff
// when the dropzone is inactive, then call it
if(this.props.onInactive) {
this.props.onInactive();
}
return;
}
2015-06-23 10:16:53 +02:00
// handle Drag and Drop
if(event.dataTransfer && event.dataTransfer.files.length > 0) {
files = event.dataTransfer.files;
} else if(event.target.files) { // handle input type file
files = event.target.files;
}
if(typeof this.props.onDrop === 'function' && files) {
this.props.onDrop(files);
}
},
handleDeleteFile(fileId) {
// input's value is not change the second time someone
// inputs the same file again, therefore we need to reset its value
this.refs.fileinput.getDOMNode().value = '';
this.props.handleDeleteFile(fileId);
},
2015-06-29 10:01:54 +02:00
handleCancelFile(fileId) {
// input's value is not change the second time someone
// inputs the same file again, therefore we need to reset its value
this.refs.fileinput.getDOMNode().value = '';
this.props.handleCancelFile(fileId);
},
2015-06-29 17:37:14 +02:00
handlePauseFile(fileId) {
// input's value is not change the second time someone
// inputs the same file again, therefore we need to reset its value
this.refs.fileinput.getDOMNode().value = '';
this.props.handlePauseFile(fileId);
},
handleResumeFile(fileId) {
// input's value is not change the second time someone
// inputs the same file again, therefore we need to reset its value
this.refs.fileinput.getDOMNode().value = '';
this.props.handleResumeFile(fileId);
},
handleOnClick() {
// when multiple is set to false and the user already uploaded a piece,
// do not propagate event
if(this.props.dropzoneInactive) {
// if there is a handle function for doing stuff
// when the dropzone is inactive, then call it
if(this.props.onInactive) {
this.props.onInactive();
}
return;
}
2015-06-30 11:56:38 +02: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
2015-06-30 13:32:41 +02:00
var evt = new MouseEvent('click', {
2015-06-30 11:56:38 +02:00
view: window,
bubbles: true,
2015-06-30 13:32:41 +02:00
cancelable: true
2015-06-30 11:56:38 +02:00
});
2015-06-30 11:56:38 +02:00
evt.stopPropagation();
this.refs.fileinput.getDOMNode().dispatchEvent(evt);
2015-06-23 10:16:53 +02:00
},
render: function () {
2015-06-29 11:44:16 +02:00
// has files only is true if there are files that do not have the status deleted or canceled
2015-06-30 17:57:20 +02:00
let hasFiles = this.props.filesToUpload.filter((file) => file.status !== 'deleted' && file.status !== 'canceled' && file.size !== -1).length > 0;
2015-06-29 14:36:55 +02:00
let className = hasFiles ? 'has-files ' : '';
className += this.props.dropzoneInactive ? 'inactive-dropzone' : 'active-dropzone';
2015-06-29 14:36:55 +02:00
className += this.props.className ? ' ' + this.props.className : '';
2015-06-23 10:16:53 +02:00
2015-07-24 16:17:27 +02:00
// if !== -2: triggers a FileDragAndDrop-global spinner
if(this.props.hashingProgress !== -2) {
return (
<div className={className}>
2015-07-24 16:17:27 +02:00
<p>{getLangText('Computing hash(es)... This may take a few minutes.')}</p>
<p>
<span>{Math.ceil(this.props.hashingProgress)}%</span>
<a onClick={this.props.handleCancelHashing}> {getLangText('Cancel hashing')}</a>
2015-07-24 16:17:27 +02:00
</p>
2015-07-24 15:13:20 +02:00
<ProgressBar completed={this.props.hashingProgress} color="#48DACB"/>
</div>
);
} else {
return (
<div
className={className}
onDragStart={this.handleDragStart}
onDrag={this.handleDrop}
onDragEnter={this.handleDragEnter}
onDragLeave={this.handleDragLeave}
onDragOver={this.handleDragOver}
onDrop={this.handleDrop}
onDragEnd={this.handleDragEnd}>
<FileDragAndDropDialog
multipleFiles={this.props.multiple}
hasFiles={hasFiles}
onClick={this.handleOnClick}
enableLocalHashing={this.props.enableLocalHashing}/>
<FileDragAndDropPreviewIterator
files={this.props.filesToUpload}
handleDeleteFile={this.handleDeleteFile}
handleCancelFile={this.handleCancelFile}
handlePauseFile={this.handlePauseFile}
handleResumeFile={this.handleResumeFile}
areAssetsDownloadable={this.props.areAssetsDownloadable}
areAssetsEditable={this.props.areAssetsEditable}/>
<input
multiple={this.props.multiple}
ref="fileinput"
type="file"
style={{
display: 'none',
height: 0,
width: 0
}}
onChange={this.handleDrop} />
</div>
);
}
2015-06-23 10:16:53 +02:00
}
});
2015-07-03 19:08:56 +02:00
export default FileDragAndDrop;