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; return false;
}, },
disableSubmit: function() { disableSubmit: Ember.computed.or("model.loading", "view.isUploading"),
return this.get('model.loading');
}.property('model.loading'),
save(force) { save(force) {
const composer = this.get('model'); const composer = this.get('model');

View File

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