diff --git a/app/assets/javascripts/application.js.erb.coffee b/app/assets/javascripts/application.js.erb.coffee index 3846c6df..bde5b1cf 100644 --- a/app/assets/javascripts/application.js.erb.coffee +++ b/app/assets/javascripts/application.js.erb.coffee @@ -168,6 +168,53 @@ $(document).on "click", "button[name=ab-comments]", -> commentBox.slideUp() btn[0].dataset.state = 'hidden' +$(document).on "keyup", "input[name=ab-comment-new]", (evt) -> + input = $(this) + aid = input[0].dataset.aId + ctr = $("span#ab-comment-charcount-#{aid}") + cbox = $("div[name=ab-comment-new-group][data-a-id=#{aid}]") + + if evt.which == 13 # return key + evt.preventDefault() + return cbox.addClass "has-error" if input.val().length > 160 || input.val().trim().length == 0 + input.attr 'disabled', 'disabled' + + $.ajax + url: '/ajax/create_comment' + type: 'POST' + data: + answer: aid + comment: input.val() + dataType: 'json' # jQuery can't guess the datatype correctly here... + success: (data, status, jqxhr) -> + console.log data + if data.success + $(".panel-footer#ab-comments-#{aid}").html data.render + input.val '' + ctr.html 160 + $("span#ab-comment-count-#{aid}").html data.count + showNotification data.message, data.success + error: (jqxhr, status, error) -> + console.log jqxhr, status, error + showNotification "An error occurred, a developer should check the console for details", false + complete: (jqxhr, status) -> + input.removeAttr 'disabled' + +$(document).on "input", "input[name=ab-comment-new]", (evt) -> + input = $(this) + aid = input[0].dataset.aId + ctr = $("span#ab-comment-charcount-#{aid}") + + cbox = $("div[name=ab-comment-new-group][data-a-id=#{aid}]") + cbox.removeClass "has-error" if cbox.hasClass "has-error" + + ctr.html 160 - input.val().length + if Number(ctr.html()) < 0 + ctr.removeClass 'text-muted' + ctr.addClass 'text-danger' + else + ctr.removeClass 'text-danger' + ctr.addClass 'text-muted' $(document).on "click", "button[name=user-action]", -> btn = $(this) diff --git a/app/controllers/ajax/comment_controller.rb b/app/controllers/ajax/comment_controller.rb index 72e8d438..8fec4701 100644 --- a/app/controllers/ajax/comment_controller.rb +++ b/app/controllers/ajax/comment_controller.rb @@ -18,5 +18,6 @@ class Ajax::CommentController < ApplicationController @message = "Comment posted successfully." @success = true @render = render_to_string(partial: 'shared/comments', locals: { a: answer }) + @count = answer.comment_count end end diff --git a/app/views/ajax/comment/create.json.jbuilder b/app/views/ajax/comment/create.json.jbuilder index e63a494b..46fa7695 100644 --- a/app/views/ajax/comment/create.json.jbuilder +++ b/app/views/ajax/comment/create.json.jbuilder @@ -1,2 +1,3 @@ json.partial! 'ajax/shared/status' -json.render @render if @render \ No newline at end of file +json.render @render if @render +json.count @count if @count \ No newline at end of file diff --git a/app/views/shared/_comments.html.haml b/app/views/shared/_comments.html.haml index 9680d5cf..de2ac9c1 100644 --- a/app/views/shared/_comments.html.haml +++ b/app/views/shared/_comments.html.haml @@ -7,6 +7,6 @@ #{user_screen_name comment.user}: = comment.content - if user_signed_in? - .form-group.has-feedback - %input.form-control{type: :text, placeholder: 'Comment...', id: "ab-comments-#{a.id}-new"} - %span.text-muted.form-control-feedback.comment-count{id: "ab-comments-#{a.id}-charcount"} 160 \ No newline at end of file + .form-group.has-feedback{name: 'ab-comment-new-group', 'data-a-id' => a.id} + %input.form-control{type: :text, placeholder: 'Comment...', name: 'ab-comment-new', 'data-a-id' => a.id} + %span.text-muted.form-control-feedback.comment-count{id: "ab-comment-charcount-#{a.id}"} 160 \ No newline at end of file