FIX: immediately remove upload placeholder when cancelling the upload

FIX: prevent post submit when something is uploading
This commit is contained in:
Régis Hanol 2015-09-22 18:16:53 +02:00
parent 6a4b9a3d38
commit e65ddc6c25
2 changed files with 20 additions and 10 deletions

View file

@ -225,9 +225,7 @@ export default Ember.Controller.extend({
return false;
},
disableSubmit: function() {
return this.get('model.loading');
}.property('model.loading'),
disableSubmit: Ember.computed.or("model.loading", "view.isUploading"),
save(force) {
const composer = this.get('model');

View file

@ -340,15 +340,18 @@ const ComposerView = Ember.View.extend(Ember.Evented, {
var cancelledByTheUser;
this.messageBus.subscribe("/uploads/composer", upload => {
// reset upload state
reset();
// replace upload placeholder
if (upload && upload.url) {
const old = Discourse.Utilities.getUploadPlaceholder(),
markdown = cancelledByTheUser ? "" : Discourse.Utilities.getUploadMarkdown(upload);
this.replaceMarkdown(old, markdown);
if (!cancelledByTheUser) {
const uploadPlaceholder = Discourse.Utilities.getUploadPlaceholder(),
markdown = Discourse.Utilities.getUploadMarkdown(upload);
this.replaceMarkdown(uploadPlaceholder, markdown);
}
} else {
Discourse.Utilities.displayErrorForUpload(upload);
}
// reset upload state
reset();
});
$uploadTarget.fileupload({
@ -370,8 +373,8 @@ const ComposerView = Ember.View.extend(Ember.Evented, {
// deal with cancellation
cancelledByTheUser = false;
// add upload placeholder
const markdown = Discourse.Utilities.getUploadPlaceholder();
this.addMarkdown(markdown);
const uploadPlaceholder = Discourse.Utilities.getUploadPlaceholder();
this.addMarkdown(uploadPlaceholder);
if (data["xhr"]) {
const jqHXR = data.xhr();
@ -381,7 +384,10 @@ const ComposerView = Ember.View.extend(Ember.Evented, {
const $cancel = $("#cancel-file-upload");
$cancel.on("click", () => {
if (jqHXR) {
// signal the upload was cancelled by the user
cancelledByTheUser = true;
// immediately remove upload placeholder
this.replaceMarkdown(uploadPlaceholder, "");
// might trigger a "fileuploadfail" event with status = 0
jqHXR.abort();
// make sure we always reset the uploading status
@ -401,8 +407,14 @@ const ComposerView = Ember.View.extend(Ember.Evented, {
});
$uploadTarget.on("fileuploadfail", (e, data) => {
// reset upload state
reset();
if (!cancelledByTheUser) {
// remove upload placeholder when there's a failure
const uploadPlaceholder = Discourse.Utilities.getUploadPlaceholder();
this.replaceMarkdown(uploadPlaceholder, "");
// display the error
Discourse.Utilities.displayErrorForUpload(data);
}
});