Move comment posting into its own function

This commit is contained in:
Pablo Vazquez 2017-09-22 19:12:13 +02:00
parent 58b8cea757
commit 7d740c74e3
2 changed files with 33 additions and 27 deletions

View File

@ -303,3 +303,33 @@ function save_comment(is_new_comment, $commentContainer)
return promise;
}
/* Used when clicking the .comment-action-submit button or by a shortcut */
function post_comment($submit_button){
save_comment(true)
.progress(function() {
$submit_button
.addClass('submitting')
.html('<i class="pi-spin spin"></i> Posting...');
})
.fail(function(xhr){
if (typeof xhr === 'string') {
show_comment_button_error(xhr);
} else {
// If it's not a string, we assume it's a jQuery XHR object.
if (console) console.log('Error saving comment:', xhr.responseText);
show_comment_button_error("Houston! Try again?");
}
toastr.error(xhr.responseText, 'Error saving comment');
})
.done(function(comment_node_id) {
$submit_button
.removeClass('submitting')
.html('Post Comment');
$('#comment_field').val('');
$('body').trigger('pillar:comment-posted', [comment_node_id]);
toastr.success('Comment posted!');
});
}

View File

@ -92,32 +92,8 @@ script.
convert = convert.makeHtml;
/* Submit new comment */
$('.comment-action-submit').click(function(e){
var $button = $(this);
save_comment(true)
.progress(function() {
$button
.addClass('submitting')
.html('<i class="pi-spin spin"></i> Posting...');
})
.fail(function(xhr){
if (typeof xhr === 'string') {
show_comment_button_error(xhr);
} else {
// If it's not a string, we assume it's a jQuery XHR object.
if (console) console.log('Error saving comment:', xhr.responseText);
show_comment_button_error("Houston! Try again?");
}
toastr.error(xhr.responseText, 'Error saving comment');
})
.done(function(comment_node_id) {
$button
.removeClass('submitting')
.html('Post Comment');
$('#comment_field').val('');
$('body').trigger('pillar:comment-posted', [comment_node_id]);
});
$(document).on('click', 'body .comment-action-submit', function(e){
post_comment($(this));
});
{% if show_comments %}
@ -152,7 +128,7 @@ script.
// Send on ctrl+enter
if ($textarea.is(":focus")) {
if ((e.keyCode == 10 || e.keyCode == 13) && e.ctrlKey){
$( ".comment-action-submit" ).trigger( "click" );
post_comment($container.find('.comment-action-submit'));
}
}
});