From 4e6d09a774d48e5bf678e4104307b96972ba7081 Mon Sep 17 00:00:00 2001 From: isqua Date: Sun, 8 Feb 2015 21:01:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BC=D0=B5=D0=BD=D1=8F=D1=82?= =?UTF-8?q?=D1=8C=20url=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B5=D0=BA=D0=BE?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B4=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8=D1=8F;=20Fixes?= =?UTF-8?q?=20#87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chrome_point_plus/modules/ajax-comments.js | 37 +++++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/chrome_point_plus/modules/ajax-comments.js b/chrome_point_plus/modules/ajax-comments.js index c4c940c..d0bddbd 100644 --- a/chrome_point_plus/modules/ajax-comments.js +++ b/chrome_point_plus/modules/ajax-comments.js @@ -48,7 +48,7 @@ AjaxComments.prototype.onSubmit = function(event) { }; /** - * Проверяет, выбран ли файл. Если да — отправляет форму с перезагрузкой, иначе — аяксом  + * Проверяет, выбран ли файл. Если да — отправляет форму с перезагрузкой, иначе — аяксом * @param {jQuery} $form Элемент формы */ AjaxComments.prototype.submit = function($form) { @@ -67,8 +67,7 @@ AjaxComments.prototype.submit = function($form) { */ AjaxComments.prototype.onKeypress = function(event) { var $form; - var proc; - + if (this.isProperKeys(event)) { event.preventDefault(); event.stopPropagation(); @@ -116,12 +115,19 @@ function AjaxCommentProcessor($form) { this._text = this._$textarea.val(); this._CSRF = $form.get(0).elements.csrf_token.value; + this._actionUrl = this._$form.attr('action'); this._postId = this._$post.data('id'); this._commentId = this._$post.data('comment-id'); this.sendComment(); } +/** + * Регулярка для урла, проверяюшая, не рекомендация ли это + * @type {RegExp} + */ +AjaxCommentProcessor.recommendationReg = /^\/[^/]*\/r$/; + /** * Отправляет комментарий */ @@ -130,7 +136,7 @@ AjaxCommentProcessor.prototype.sendComment = function() { $.ajax({ type: 'POST', - url: '/api/post' + this._$form.attr('action'), + url: this.getUrl(), data: { text: this._text, comment_id: this._commentId @@ -141,6 +147,25 @@ AjaxCommentProcessor.prototype.sendComment = function() { }); }; +/** + * @return {Boolean} true — это коммент-рекомендация, fasle — обычный коммент + */ +AjaxCommentProcessor.prototype.isRecommendation = function() { + return this._isRec || (this._isRec = this.constructor.recommendationReg.test(this._actionUrl)); +}; + +/** + * @return {String} Адрес, на который слать запрос + */ +AjaxCommentProcessor.prototype.getUrl = function() { + // Если это рекомендация комментария + if (this.isRecommendation() && this._commentId) { + return '/api/post/' + this._postId + '/' + this._commentId + '/r'; + } else { + return '/api/post' + this._actionUrl; + } +}; + /** * Подкладывает CSRF-токен в заголовки запроса * @param {XMLHttpRequest} xhr Объект запроса @@ -181,6 +206,7 @@ AjaxCommentProcessor.prototype.onSuccess = function(data, textStatus) { }; AjaxCommentProcessor.prototype.createComment = function(data) { + /* global create_comment_elements */ create_comment_elements({ id: data.comment_id, toId: this._commentId || null, @@ -236,6 +262,7 @@ AjaxCommentProcessor.prototype.showComment = function($comment) { * @param {String} error */ AjaxCommentProcessor.prototype.onError = function(req, status, error) { + /* global alert */ alert(chrome.i18n.getMessage('msg_comment_send_failed') + '\n' + error); this.setProgress(false); @@ -243,7 +270,7 @@ AjaxCommentProcessor.prototype.onError = function(req, status, error) { /** * Устанавливает прогресс - * @param {Boolean} isProgress true — включить прогресс, false — отключить + * @param {Boolean} isProgress true — включить прогресс, false — отключить */ AjaxCommentProcessor.prototype.setProgress = function(isProgress) { this._$textarea.prop('disabled', isProgress);