1
0
mirror of https://github.com/ascribe/onion.git synced 2025-02-14 21:10:27 +01:00

implement cancel functionality

This commit is contained in:
Tim Daubenschütz 2015-06-29 10:01:54 +02:00
parent 12da6c1426
commit 66fff1c50a
7 changed files with 76 additions and 27 deletions

View File

@ -17,6 +17,7 @@ var FileDragAndDrop = React.createClass({
onDragEnd: React.PropTypes.func, onDragEnd: React.PropTypes.func,
filesToUpload: React.PropTypes.array, filesToUpload: React.PropTypes.array,
handleDeleteFile: React.PropTypes.func, handleDeleteFile: React.PropTypes.func,
handleCancelFile: React.PropTypes.func,
multiple: React.PropTypes.bool, multiple: React.PropTypes.bool,
dropzoneInactive: React.PropTypes.bool dropzoneInactive: React.PropTypes.bool
}, },
@ -85,6 +86,13 @@ var FileDragAndDrop = React.createClass({
this.props.handleDeleteFile(fileId); this.props.handleDeleteFile(fileId);
}, },
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);
},
handleOnClick() { handleOnClick() {
// when multiple is set to false and the user already uploaded a piece, // when multiple is set to false and the user already uploaded a piece,
// do not propagate event // do not propagate event
@ -117,7 +125,8 @@ var FileDragAndDrop = React.createClass({
{hasFiles ? null : this.props.multiple ? <span>Click or drag to add files</span> : <span>Click or drag to add a file</span>} {hasFiles ? null : this.props.multiple ? <span>Click or drag to add files</span> : <span>Click or drag to add a file</span>}
<FileDragAndDropPreviewIterator <FileDragAndDropPreviewIterator
files={this.props.filesToUpload} files={this.props.filesToUpload}
handleDeleteFile={this.handleDeleteFile}/> handleDeleteFile={this.handleDeleteFile}
handleCancelFile={this.handleCancelFile}/>
<input <input
multiple={this.props.multiple} multiple={this.props.multiple}
ref="fileinput" ref="fileinput"

View File

@ -13,7 +13,8 @@ let FileDragAndDropPreview = React.createClass({
url: React.PropTypes.string, url: React.PropTypes.string,
type: React.PropTypes.string type: React.PropTypes.string
}).isRequired, }).isRequired,
handleDeleteFile: React.PropTypes.func handleDeleteFile: React.PropTypes.func,
handleCancelFile: React.PropTypes.func
}, },
handleDeleteFile(event) { handleDeleteFile(event) {
@ -22,8 +23,12 @@ let FileDragAndDropPreview = React.createClass({
// handleDeleteFile is optional, so if its not submitted, // handleDeleteFile is optional, so if its not submitted,
// don't run it // don't run it
if(this.props.handleDeleteFile) { // On the other hand, if the files progress is not yet at a 100%,
// just run fineuploader.cancel
if(this.props.handleDeleteFile && this.props.file.progress === 100) {
this.props.handleDeleteFile(this.props.file.id); this.props.handleDeleteFile(this.props.file.id);
} else if(this.props.handleCancelFile && this.props.file.progress !== 100) {
this.props.handleCancelFile(this.props.file.id);
} }
}, },

View File

@ -18,11 +18,15 @@ let FileDragAndDropPreviewImage = React.createClass({
return ( return (
<div <div
onClick={this.props.onClick}
className="file-drag-and-drop-preview-image" className="file-drag-and-drop-preview-image"
style={imageStyle}> style={imageStyle}>
<ProgressBar completed={this.props.progress} color="black"/> <ProgressBar completed={this.props.progress} color="black"/>
{this.props.progress === 100 ? <span className="glyphicon glyphicon-remove delete-file" aria-hidden="true" title="Delete"></span> : null} <span
className="glyphicon glyphicon-remove delete-file"
aria-hidden="true"
title="Delete"
onClick={this.props.onClick}>
</span>
</div> </div>
); );
} }

View File

@ -7,7 +7,8 @@ import FileDragAndDropPreview from './file_drag_and_drop_preview';
let FileDragAndDropPreviewIterator = React.createClass({ let FileDragAndDropPreviewIterator = React.createClass({
propTypes: { propTypes: {
files: React.PropTypes.array, files: React.PropTypes.array,
handleDeleteFile: React.PropTypes.func handleDeleteFile: React.PropTypes.func,
handleCancelFile: React.PropTypes.func
}, },
render() { render() {
@ -19,7 +20,8 @@ let FileDragAndDropPreviewIterator = React.createClass({
<FileDragAndDropPreview <FileDragAndDropPreview
key={i} key={i}
file={file} file={file}
handleDeleteFile={this.props.handleDeleteFile}/> handleDeleteFile={this.props.handleDeleteFile}
handleCancelFile={this.props.handleCancelFile}/>
); );
})} })}
</div> </div>

View File

@ -13,12 +13,17 @@ let FileDragAndDropPreviewOther = React.createClass({
render() { render() {
return ( return (
<div <div
onClick={this.props.onClick}
className="file-drag-and-drop-preview"> className="file-drag-and-drop-preview">
<ProgressBar completed={this.props.progress} color="black"/> <ProgressBar completed={this.props.progress} color="black"/>
<div className="file-drag-and-drop-preview-table-wrapper"> <div className="file-drag-and-drop-preview-table-wrapper">
<div className="file-drag-and-drop-preview-other"> <div className="file-drag-and-drop-preview-other">
{this.props.progress === 100 ? <span className="glyphicon glyphicon-remove delete-file" aria-hidden="true" title="Delete"></span> : null} <span
className="glyphicon glyphicon-remove delete-file"
aria-hidden="true"
title="Delete"
onClick={this.props.onClick}>
</span>
<span>{'.' + this.props.type}</span> <span>{'.' + this.props.type}</span>
</div> </div>
</div> </div>

View File

@ -59,7 +59,8 @@ var ReactS3FineUploader = React.createClass({
deleteFile: React.PropTypes.shape({ deleteFile: React.PropTypes.shape({
enabled: React.PropTypes.bool, enabled: React.PropTypes.bool,
method: React.PropTypes.string, method: React.PropTypes.string,
endpoint: React.PropTypes.string endpoint: React.PropTypes.string,
customHeaders: React.PropTypes.object
}), }),
session: React.PropTypes.shape({ session: React.PropTypes.shape({
endpoint: React.PropTypes.bool endpoint: React.PropTypes.bool
@ -79,6 +80,7 @@ var ReactS3FineUploader = React.createClass({
}, },
getInitialState() { getInitialState() {
console.log(this.props);
return { return {
filesToUpload: [], filesToUpload: [],
uploader: new fineUploader.s3.FineUploaderBasic(this.propsToConfig()) uploader: new fineUploader.s3.FineUploaderBasic(this.propsToConfig())
@ -109,6 +111,7 @@ var ReactS3FineUploader = React.createClass({
callbacks: { callbacks: {
onSubmit: this.onSubmit, onSubmit: this.onSubmit,
onComplete: this.onComplete, onComplete: this.onComplete,
onCancel: this.onCancel,
onDelete: this.onDelete, onDelete: this.onDelete,
onSessionRequestComplete: this.onSessionRequestComplete, onSessionRequestComplete: this.onSessionRequestComplete,
onProgress: this.onProgress, onProgress: this.onProgress,
@ -219,9 +222,11 @@ var ReactS3FineUploader = React.createClass({
console.log('delete'); console.log('delete');
}, },
onCancel() { onCancel(id) {
console.log('cancel'); this.removeFileWithIdFromFilesToUpload(id);
// handle file removal here, for this.state.filesToUpload (same as in onDeleteComplete)
let notification = new GlobalNotificationModel('File upload canceled', 'success', 10000);
GlobalNotificationActions.appendGlobalNotification(notification);
}, },
onSessionRequestComplete() { onSessionRequestComplete() {
@ -233,17 +238,7 @@ var ReactS3FineUploader = React.createClass({
let notification = new GlobalNotificationModel('Couldn\'t delete file', 'danger', 10000); let notification = new GlobalNotificationModel('Couldn\'t delete file', 'danger', 10000);
GlobalNotificationActions.appendGlobalNotification(notification); GlobalNotificationActions.appendGlobalNotification(notification);
} else { } else {
// also, sync files from state with the ones from fineuploader this.removeFileWithIdFromFilesToUpload(id);
let filesToUpload = JSON.parse(JSON.stringify(this.state.filesToUpload));
// splice because I can
filesToUpload.splice(id, 1);
// set state
let newState = React.addons.update(this.state, {
filesToUpload: { $set: filesToUpload }
});
this.setState(newState);
let notification = new GlobalNotificationModel('File deleted', 'success', 10000); let notification = new GlobalNotificationModel('File deleted', 'success', 10000);
GlobalNotificationActions.appendGlobalNotification(notification); GlobalNotificationActions.appendGlobalNotification(notification);
@ -267,6 +262,10 @@ var ReactS3FineUploader = React.createClass({
// promise // promise
}, },
handleCancelFile(fileId) {
this.state.uploader.cancel(fileId);
},
handleUploadFile(files) { handleUploadFile(files) {
// If multiple set and user already uploaded its work, // If multiple set and user already uploaded its work,
@ -320,12 +319,27 @@ var ReactS3FineUploader = React.createClass({
this.setState(newState); this.setState(newState);
}, },
removeFileWithIdFromFilesToUpload(fileId) {
// also, sync files from state with the ones from fineuploader
let filesToUpload = JSON.parse(JSON.stringify(this.state.filesToUpload));
// splice because I can
filesToUpload.splice(fileId, 1);
// set state
let newState = React.addons.update(this.state, {
filesToUpload: { $set: filesToUpload }
});
this.setState(newState);
},
render() { render() {
return ( return (
<FileDragAndDrop <FileDragAndDrop
onDrop={this.handleUploadFile} onDrop={this.handleUploadFile}
filesToUpload={this.state.filesToUpload} filesToUpload={this.state.filesToUpload}
handleDeleteFile={this.handleDeleteFile} handleDeleteFile={this.handleDeleteFile}
handleCancelFile={this.handleCancelFile}
multiple={this.props.multiple} multiple={this.props.multiple}
dropzoneInactive={!this.props.multiple && this.state.filesToUpload.length > 0} /> dropzoneInactive={!this.props.multiple && this.state.filesToUpload.length > 0} />
); );

View File

@ -116,6 +116,13 @@ let RegisterPiece = React.createClass( {
let FileUploader = React.createClass({ let FileUploader = React.createClass({
getCookie(name) {
let value = '; ' + document.cookie;
let parts = value.split('; ' + name + '=');
if (parts.length === 2) {
return parts.pop().split(';').shift();
}
},
render() { render() {
return ( return (
<ReactS3FineUploader <ReactS3FineUploader
@ -161,7 +168,10 @@ let FileUploader = React.createClass( {
deleteFile={{ deleteFile={{
enabled: true, enabled: true,
method: 'DELETE', method: 'DELETE',
endpoint: AppConstants.serverUrl + 's3/delete' endpoint: AppConstants.serverUrl + 's3/delete',
customHeaders: {
'X-CSRFToken': 'asdasd'
}
}} }}
validation={{ validation={{
itemLimit: 100000, itemLimit: 100000,