Option for AJAX comments. A bit of JSDoc.

This commit is contained in:
Alexey Skobkin 2015-01-15 13:45:07 +04:00
parent 56bd3117ec
commit 6d6a9fa3d6
4 changed files with 100 additions and 86 deletions

View File

@ -113,8 +113,11 @@
"option_nsfw_blur_comments_entire": {
"message": "Blur entire comments in nsfw posts"
},
"option_ctrl_enter": {
"message": "Send post and comments by CTRL+Enter (deprecated)"
"option_ajax": {
"message": "AJAX"
},
"option_ajax_comments": {
"message": "Send comments via AJAX (CTRL+Enter)"
},
"option_fluid_layout": {
"message": "Fluid layout"

View File

@ -113,8 +113,11 @@
"option_nsfw_blur_comments_entire": {
"message": "Размытие комментариев целиком"
},
"option_ctrl_enter": {
"message": "Отправлять текст по CTRL+Enter (устарело)"
"option_ajax": {
"message": "AJAX"
},
"option_ajax_comments": {
"message": "Отправка комментариев через AJAX (CTRL+Enter)"
},
"option_fluid_layout": {
"message": ""Резиновая" вёрстка (растянуть сайт по горизонтали)"

View File

@ -544,88 +544,91 @@ $(document).ready(function() {
});
}
// @todo implement option
if (true || options.is('option_ajax_comments')) {
// Removing old bindings
// Dirty hack for page context
$('#comments').replaceWith($('#comments').clone());
if (options.is('option_ajax')) {
// Comments
if (options.is('option_ajax_comments')) {
// Removing old bindings
// Dirty hack for page context
$('#comments').replaceWith($('#comments').clone());
// Binding new
$('#comments').on('keypress.pp', '.reply-form textarea', function (evt) {
if ((evt.keyCode === 10 || evt.keyCode === 13) && (evt.ctrlKey || evt.metaKey)) {
evt.stopPropagation();
evt.preventDefault();
var $post = $(this).parents('.post:first');
var csRf = $(this).siblings('input[name="csrf_token"]').val();
console.log(csRf);
$.ajax({
type: 'POST',
url: '/api/post/' + $post.data('id'),
data: {
text: $(this).val(),
comment_id: $post.data('comment-id')
},
error: function(req, status, error) {
console.error('AJAX request error while sending the comment: %s', error);
console.log('Status: %s', status);
alert(chrome.i18n.getMessage('msg_comment_send_failed') + '\n' + error);
},
success: function(data, textStatus) {
console.log('data %O', data);
console.log('status %O', textStatus);
/*{
comment_id: 34,
id: 'ovrwcv'
};*/
if (textStatus === 'success') {
// Hiding form
$('#reply-' + $post.data('id') + '_' + $post.data('comment-id')).prop('checked', false);
// Creating the comment HTML
create_comment_elements({
id: data.comment_id,
toId: $post.data('comment-id') || null,
postId: $post.data('id'),
author: $('#name h1').text(),
text: $(this).val(),
fadeOut: false
}, function($comment) {
// If list mode or not addressed to other comment
if ($('#comments #tree-switch a').eq(0).hasClass('active') || ($post.data('comment-id') === undefined)) {
// Adding to the end of the list
$('.content-wrap #comments #post-reply').before($comment);
} else {
// Check for children
$parentCommentChildren = $post.next('.comments');
// @fixme Find a bug with lost indentation of new comment
// If child comment already exist
if ($parentCommentChildren.length) {
console.log('Child comments found. Appending...');
$parentCommentChildren.append($comment);
// Binding new
$('#comments').on('keypress.pp', '.reply-form textarea', function (evt) {
if ((evt.keyCode === 10 || evt.keyCode === 13) && (evt.ctrlKey || evt.metaKey)) {
evt.stopPropagation();
evt.preventDefault();
var $post = $(this).parents('.post:first');
var csRf = $(this).siblings('input[name="csrf_token"]').val();
console.log(csRf);
$.ajax({
type: 'POST',
url: '/api/post/' + $post.data('id'),
data: {
text: $(this).val(),
comment_id: $post.data('comment-id')
},
error: function(req, status, error) {
console.error('AJAX request error while sending the comment: %s', error);
console.log('Status: %s', status);
alert(chrome.i18n.getMessage('msg_comment_send_failed') + '\n' + error);
},
/**
* @param {object} data Response data
* @param {number} data.comment_id ID of the created comment
* @param {string} data.id ID of the post
* @param {string} textStatus Text of request status
*/
success: function(data, textStatus) {
console.log('data %O', data);
console.log('status %O', textStatus);
if (textStatus === 'success') {
// Hiding form
$('#reply-' + $post.data('id') + '_' + $post.data('comment-id')).prop('checked', false);
// Creating the comment HTML
create_comment_elements({
id: data.comment_id,
toId: $post.data('comment-id') || null,
postId: $post.data('id'),
author: $('#name h1').text(),
text: $(this).val(),
fadeOut: false
}, function($comment) {
// If list mode or not addressed to other comment
if ($('#comments #tree-switch a').eq(0).hasClass('active') || ($post.data('comment-id') === undefined)) {
// Adding to the end of the list
$('.content-wrap #comments #post-reply').before($comment);
} else {
console.log('No child comments found. Creating...');
$post.after($('<div>').addClass('comments').append($comment).css('margin-left', '0px'));
// Check for children
$parentCommentChildren = $post.next('.comments');
// @fixme Find a bug with lost indentation of new comment
// If child comment already exist
if ($parentCommentChildren.length) {
console.log('Child comments found. Appending...');
$parentCommentChildren.append($comment);
} else {
console.log('No child comments found. Creating...');
$post.after($('<div>').addClass('comments').append($comment).css('margin-left', '0px'));
}
}
}
});
// Cleaning textarea
$(this).val('');
});
// Cleaning textarea
$(this).val('');
}
}.bind(this),
beforeSend: function (xhr) {
xhr.setRequestHeader('X-CSRF', csRf);
}
}.bind(this),
beforeSend: function (xhr) {
xhr.setRequestHeader('X-CSRF', csRf);
}
});
}
});
});
}
});
}
}
// Hightlight post with new comments

View File

@ -165,10 +165,15 @@
</section>
<section class="tabs-content-item" id="other">
<label class="option-node">
<input type="checkbox" name="option-ctrl-enter" disabled="disabled">
<span data-i18n="option_ctrl_enter"></span>
</label>
<div class="option-node">
<input type="checkbox" name="option-ajax" id="option-ajax">
<label for="option-ajax" data-i18n="option_ajax"></label>
<label class="option-node">
<input type="checkbox" name="option-ajax-comments">
<span data-i18n="option_ajax_comments"></span>
</label>
</div>
<label class="option-node">
<input type="checkbox" name="option-fluid-layout">