-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously the file upload dialog was PDF- and textbook-specific. The changes are adding parameters to the FileUpload model for the file type, and adding an onSuccess callback to the UploadDialog view. Also moved upload-specific SASS into its own file.
- Loading branch information
Peter Fogg
committed
Aug 15, 2013
1 parent
f4fe6de
commit e8f40bc
Showing
11 changed files
with
363 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
CMS.Models.FileUpload = Backbone.Model.extend({ | ||
defaults: { | ||
"title": "", | ||
"message": "", | ||
"selectedFile": null, | ||
"uploading": false, | ||
"uploadedBytes": 0, | ||
"totalBytes": 0, | ||
"finished": false, | ||
"mimeType": "application/pdf", | ||
"fileType": "PDF" | ||
}, | ||
// NOTE: validation functions should return non-internationalized error | ||
// messages. The messages will be passed through gettext in the template. | ||
validate: function(attrs, options) { | ||
if(attrs.selectedFile && attrs.selectedFile.type !== this.attributes.mimeType) { | ||
return { | ||
message: "Only " + this.attributes.fileType + " files can be uploaded. Please select a file ending in ." + this.attributes.fileType.toLowerCase() + " to upload.", | ||
attributes: {selectedFile: true} | ||
}; | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
CMS.Views.UploadDialog = Backbone.View.extend({ | ||
options: { | ||
shown: true, | ||
successMessageTimeout: 2000 // 2 seconds | ||
}, | ||
initialize: function() { | ||
this.template = _.template($("#upload-dialog-tpl").text()); | ||
this.listenTo(this.model, "change", this.render); | ||
}, | ||
render: function() { | ||
var isValid = this.model.isValid(); | ||
var selectedFile = this.model.get('selectedFile'); | ||
var oldInput = this.$("input[type=file]").get(0); | ||
this.$el.html(this.template({ | ||
shown: this.options.shown, | ||
url: CMS.URL.UPLOAD_ASSET, | ||
title: this.model.escape('title'), | ||
message: this.model.escape('message'), | ||
selectedFile: selectedFile, | ||
uploading: this.model.get('uploading'), | ||
uploadedBytes: this.model.get('uploadedBytes'), | ||
totalBytes: this.model.get('totalBytes'), | ||
finished: this.model.get('finished'), | ||
error: this.model.validationError | ||
})); | ||
// Ideally, we'd like to tell the browser to pre-populate the | ||
// <input type="file"> with the selectedFile if we have one -- but | ||
// browser security prohibits that. So instead, we'll swap out the | ||
// new input (that has no file selected) with the old input (that | ||
// already has the selectedFile selected). However, we only want to do | ||
// this if the selected file is valid: if it isn't, we want to render | ||
// a blank input to prompt the user to upload a different (valid) file. | ||
if (selectedFile && isValid) { | ||
$(oldInput).removeClass("error"); | ||
this.$('input[type=file]').replaceWith(oldInput); | ||
} | ||
return this; | ||
}, | ||
events: { | ||
"change input[type=file]": "selectFile", | ||
"click .action-cancel": "hideAndRemove", | ||
"click .action-upload": "upload" | ||
}, | ||
selectFile: function(e) { | ||
this.model.set({ | ||
selectedFile: e.target.files[0] || null | ||
}); | ||
}, | ||
show: function(e) { | ||
if(e && e.preventDefault) { e.preventDefault(); } | ||
this.options.shown = true; | ||
$body.addClass('dialog-is-shown'); | ||
return this.render(); | ||
}, | ||
hide: function(e) { | ||
if(e && e.preventDefault) { e.preventDefault(); } | ||
this.options.shown = false; | ||
$body.removeClass('dialog-is-shown'); | ||
return this.render(); | ||
}, | ||
hideAndRemove: function(e) { | ||
if(e && e.preventDefault) { e.preventDefault(); } | ||
return this.hide().remove(); | ||
}, | ||
upload: function(e) { | ||
if(e && e.preventDefault) { e.preventDefault(); } | ||
this.model.set('uploading', true); | ||
this.$("form").ajaxSubmit({ | ||
success: _.bind(this.success, this), | ||
error: _.bind(this.error, this), | ||
uploadProgress: _.bind(this.progress, this), | ||
data: { | ||
// don't show the generic error notification; we're in a modal, | ||
// and we're better off modifying it instead. | ||
notifyOnError: false | ||
} | ||
}); | ||
}, | ||
progress: function(event, position, total, percentComplete) { | ||
this.model.set({ | ||
"uploadedBytes": position, | ||
"totalBytes": total | ||
}); | ||
}, | ||
success: function(response, statusText, xhr, form) { | ||
this.model.set({ | ||
uploading: false, | ||
finished: true | ||
}); | ||
if(this.options.onSuccess) { | ||
this.options.onSuccess(response, statusText, xhr, form); | ||
} | ||
var that = this; | ||
this.removalTimeout = setTimeout(function() { | ||
that.hide().remove(); | ||
}, this.options.successMessageTimeout); | ||
}, | ||
error: function() { | ||
this.model.set({ | ||
"uploading": false, | ||
"uploadedBytes": 0, | ||
"title": gettext("We're sorry, there was an error") | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.