From 6d6a9fa3d6ea1ffde8e030d9a1c0e04ef4e1fc70 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Thu, 15 Jan 2015 13:45:07 +0400 Subject: [PATCH] Option for AJAX comments. A bit of JSDoc. --- chrome_point_plus/_locales/en/messages.json | 7 +- chrome_point_plus/_locales/ru/messages.json | 7 +- chrome_point_plus/js/point-plus.js | 159 ++++++++++---------- chrome_point_plus/options.html | 13 +- 4 files changed, 100 insertions(+), 86 deletions(-) diff --git a/chrome_point_plus/_locales/en/messages.json b/chrome_point_plus/_locales/en/messages.json index c8d0c1d..0735059 100644 --- a/chrome_point_plus/_locales/en/messages.json +++ b/chrome_point_plus/_locales/en/messages.json @@ -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" diff --git a/chrome_point_plus/_locales/ru/messages.json b/chrome_point_plus/_locales/ru/messages.json index 0c15eb1..f005fb7 100644 --- a/chrome_point_plus/_locales/ru/messages.json +++ b/chrome_point_plus/_locales/ru/messages.json @@ -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": ""Резиновая" вёрстка (растянуть сайт по горизонтали)" diff --git a/chrome_point_plus/js/point-plus.js b/chrome_point_plus/js/point-plus.js index e14b7dc..6ff1507 100644 --- a/chrome_point_plus/js/point-plus.js +++ b/chrome_point_plus/js/point-plus.js @@ -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($('
').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($('
').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 diff --git a/chrome_point_plus/options.html b/chrome_point_plus/options.html index d109901..432694b 100644 --- a/chrome_point_plus/options.html +++ b/chrome_point_plus/options.html @@ -165,10 +165,15 @@
- +
+ + + + +