Merge pull request #614 from ZogStriP/implemented-cancel-image-upload
users can now cancel image upload
This commit is contained in:
commit
f2f82ea83b
|
@ -248,7 +248,6 @@ Discourse.Composer = Discourse.Model.extend({
|
||||||
if (opts.postId) {
|
if (opts.postId) {
|
||||||
this.set('loading', true);
|
this.set('loading', true);
|
||||||
Discourse.Post.load(opts.postId).then(function(result) {
|
Discourse.Post.load(opts.postId).then(function(result) {
|
||||||
console.log(result);
|
|
||||||
composer.set('post', result);
|
composer.set('post', result);
|
||||||
composer.set('loading', false);
|
composer.set('loading', false);
|
||||||
});
|
});
|
||||||
|
|
|
@ -53,7 +53,7 @@
|
||||||
<div class='saving-draft'></div>
|
<div class='saving-draft'></div>
|
||||||
{{#if view.loadingImage}}
|
{{#if view.loadingImage}}
|
||||||
<div id="image-uploading">
|
<div id="image-uploading">
|
||||||
{{i18n image_selector.uploading_image}} {{view.uploadProgress}}% <a {{action cancelUpload target="view"}}>{{i18n cancel}}</a>
|
{{i18n image_selector.uploading_image}} {{view.uploadProgress}}% <a id="cancel-image-upload">{{i18n cancel}}</a>
|
||||||
</div>
|
</div>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
{{/if}}
|
{{/if}}
|
||||||
|
|
|
@ -158,10 +158,6 @@ Discourse.ComposerView = Discourse.View.extend({
|
||||||
});
|
});
|
||||||
}, 100),
|
}, 100),
|
||||||
|
|
||||||
cancelUpload: function() {
|
|
||||||
// TODO
|
|
||||||
},
|
|
||||||
|
|
||||||
initEditor: function() {
|
initEditor: function() {
|
||||||
// not quite right, need a callback to pass in, meaning this gets called once,
|
// not quite right, need a callback to pass in, meaning this gets called once,
|
||||||
// but if you start replying to another topic it will get the avatars wrong
|
// but if you start replying to another topic it will get the avatars wrong
|
||||||
|
@ -281,7 +277,6 @@ Discourse.ComposerView = Discourse.View.extend({
|
||||||
});
|
});
|
||||||
|
|
||||||
var addImages = function (e, data) {
|
var addImages = function (e, data) {
|
||||||
console.log('addImages');
|
|
||||||
// can only upload one image at a time
|
// can only upload one image at a time
|
||||||
if (data.files.length > 1) {
|
if (data.files.length > 1) {
|
||||||
bootbox.alert(Em.String.i18n('post.errors.upload_too_many_images'));
|
bootbox.alert(Em.String.i18n('post.errors.upload_too_many_images'));
|
||||||
|
@ -311,6 +306,23 @@ Discourse.ComposerView = Discourse.View.extend({
|
||||||
// drop
|
// drop
|
||||||
$uploadTarget.on('fileuploaddrop', addImages);
|
$uploadTarget.on('fileuploaddrop', addImages);
|
||||||
|
|
||||||
|
// send
|
||||||
|
$uploadTarget.on('fileuploadsend', function (e, data) {
|
||||||
|
// cf. https://github.com/blueimp/jQuery-File-Upload/wiki/API#how-to-cancel-an-upload
|
||||||
|
var jqXHR = data.xhr();
|
||||||
|
// need to wait for the link to show up in the DOM
|
||||||
|
Em.run.next(function() {
|
||||||
|
// bind on the click event on the cancel link
|
||||||
|
$('#cancel-image-upload').on('click', function() {
|
||||||
|
// cancel the upload
|
||||||
|
// NOTE: this will trigger a 'fileuploadfail' event with status = 0
|
||||||
|
if (jqXHR) jqXHR.abort();
|
||||||
|
// unbind
|
||||||
|
$(this).off('click');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// progress all
|
// progress all
|
||||||
$uploadTarget.on('fileuploadprogressall', function (e, data) {
|
$uploadTarget.on('fileuploadprogressall', function (e, data) {
|
||||||
var progress = parseInt(data.loaded / data.total * 100, 10);
|
var progress = parseInt(data.loaded / data.total * 100, 10);
|
||||||
|
@ -327,20 +339,27 @@ Discourse.ComposerView = Discourse.View.extend({
|
||||||
|
|
||||||
// fail
|
// fail
|
||||||
$uploadTarget.on('fileuploadfail', function (e, data) {
|
$uploadTarget.on('fileuploadfail', function (e, data) {
|
||||||
|
// hide upload status
|
||||||
_this.set('loadingImage', false);
|
_this.set('loadingImage', false);
|
||||||
// 413 == entity too large, returned usually from nginx
|
// deal with meaningful errors first
|
||||||
if(data.jqXHR && data.jqXHR.status === 413) {
|
if (data.jqXHR) {
|
||||||
bootbox.alert(Em.String.i18n('post.errors.upload_too_large', {max_size_kb: Discourse.SiteSettings.max_upload_size_kb}));
|
switch (data.jqXHR.status) {
|
||||||
} else {
|
// 0 == cancel from the user
|
||||||
bootbox.alert(Em.String.i18n('post.errors.upload'));
|
case 0: return;
|
||||||
|
// 413 == entity too large, returned usually from nginx
|
||||||
|
case 413:
|
||||||
|
bootbox.alert(Em.String.i18n('post.errors.upload_too_large', {max_size_kb: Discourse.SiteSettings.max_upload_size_kb}));
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// otherwise, display a generic error message
|
||||||
|
bootbox.alert(Em.String.i18n('post.errors.upload'));
|
||||||
});
|
});
|
||||||
|
|
||||||
// I hate to use Em.run.later, but I don't think there's a way of waiting for a CSS transition
|
// I hate to use Em.run.later, but I don't think there's a way of waiting for a CSS transition
|
||||||
// to finish.
|
// to finish.
|
||||||
return Em.run.later(jQuery, (function() {
|
return Em.run.later(jQuery, (function() {
|
||||||
var replyTitle;
|
var replyTitle = $('#reply-title');
|
||||||
replyTitle = $('#reply-title');
|
|
||||||
_this.resize();
|
_this.resize();
|
||||||
if (replyTitle.length) {
|
if (replyTitle.length) {
|
||||||
return replyTitle.putCursorAtEnd();
|
return replyTitle.putCursorAtEnd();
|
||||||
|
@ -351,11 +370,9 @@ Discourse.ComposerView = Discourse.View.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
addMarkdown: function(text) {
|
addMarkdown: function(text) {
|
||||||
var caretPosition, ctrl, current,
|
var ctrl = $('#wmd-input').get(0),
|
||||||
_this = this;
|
caretPosition = Discourse.Utilities.caretPosition(ctrl),
|
||||||
ctrl = $('#wmd-input').get(0);
|
current = this.get('content.reply');
|
||||||
caretPosition = Discourse.Utilities.caretPosition(ctrl);
|
|
||||||
current = this.get('content.reply');
|
|
||||||
this.set('content.reply', current.substring(0, caretPosition) + text + current.substring(caretPosition, current.length));
|
this.set('content.reply', current.substring(0, caretPosition) + text + current.substring(caretPosition, current.length));
|
||||||
return Em.run.next(function() {
|
return Em.run.next(function() {
|
||||||
return Discourse.Utilities.setCaretPosition(ctrl, caretPosition + text.length);
|
return Discourse.Utilities.setCaretPosition(ctrl, caretPosition + text.length);
|
||||||
|
@ -364,11 +381,9 @@ Discourse.ComposerView = Discourse.View.extend({
|
||||||
|
|
||||||
// Uses javascript to get the image sizes from the preview, if present
|
// Uses javascript to get the image sizes from the preview, if present
|
||||||
imageSizes: function() {
|
imageSizes: function() {
|
||||||
var result;
|
var result = {};
|
||||||
result = {};
|
|
||||||
$('#wmd-preview img').each(function(i, e) {
|
$('#wmd-preview img').each(function(i, e) {
|
||||||
var $img;
|
var $img = $(e);
|
||||||
$img = $(e);
|
|
||||||
result[$img.prop('src')] = {
|
result[$img.prop('src')] = {
|
||||||
width: $img.width(),
|
width: $img.width(),
|
||||||
height: $img.height()
|
height: $img.height()
|
||||||
|
@ -384,9 +399,10 @@ Discourse.ComposerView = Discourse.View.extend({
|
||||||
|
|
||||||
// not sure if this is the right way, keeping here for now, we could use a mixin perhaps
|
// not sure if this is the right way, keeping here for now, we could use a mixin perhaps
|
||||||
Discourse.NotifyingTextArea = Ember.TextArea.extend({
|
Discourse.NotifyingTextArea = Ember.TextArea.extend({
|
||||||
placeholder: (function() {
|
placeholder: function() {
|
||||||
return Em.String.i18n(this.get('placeholderKey'));
|
return Em.String.i18n(this.get('placeholderKey'));
|
||||||
}).property('placeholderKey'),
|
}.property('placeholderKey'),
|
||||||
|
|
||||||
didInsertElement: function() {
|
didInsertElement: function() {
|
||||||
return this.get('parent').childDidInsertElement(this);
|
return this.get('parent').childDidInsertElement(this);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue