comments.js: clean up comment quoting.

Replace Comment.quote with a remote new.js.erb call.
This commit is contained in:
evazion
2018-09-29 12:16:29 -05:00
parent 75232c0cd4
commit 9ac59aeadd
4 changed files with 19 additions and 30 deletions

View File

@@ -1,5 +1,6 @@
class CommentsController < ApplicationController
respond_to :html, :xml, :json
respond_to :js, only: [:new, :destroy, :undelete]
before_action :member_only, :except => [:index, :search, :show]
skip_before_action :api_check
@@ -17,7 +18,9 @@ class CommentsController < ApplicationController
end
def new
redirect_to comments_path
@comment = Comment.new(comment_params(:create))
@comment.body = Comment.find(params[:id]).quoted_response if params[:id]
respond_with(@comment)
end
def update
@@ -45,25 +48,21 @@ class CommentsController < ApplicationController
def show
@comment = Comment.find(params[:id])
respond_with(@comment, methods: [:quoted_response])
respond_with(@comment)
end
def destroy
@comment = Comment.find(params[:id])
check_privilege(@comment)
@comment.delete!
respond_with(@comment) do |format|
format.js
end
respond_with(@comment)
end
def undelete
@comment = Comment.find(params[:id])
check_privilege(@comment)
@comment.undelete!
respond_with(@comment) do |format|
format.js
end
respond_with(@comment)
end
private

View File

@@ -2,31 +2,11 @@ let Comment = {};
Comment.initialize_all = function() {
if ($("#c-posts").length || $("#c-comments").length) {
$(document).on("click.danbooru.comment", ".reply-link", Comment.quote);
$(document).on("click.danbooru.comment", ".edit_comment_link", Comment.show_edit_form);
$(document).on("click.danbooru.comment", ".expand-comment-response", Comment.show_new_comment_form);
}
}
Comment.quote = function(e) {
$.get(
"/comments/" + $(e.target).data('comment-id') + ".json",
function(data) {
var $link = $(e.target);
var $div = $link.closest("div.comments-for-post").find(".new-comment");
var $textarea = $div.find("textarea");
var msg = data.quoted_response;
if ($textarea.val().length > 0) {
msg = $textarea.val() + "\n\n" + msg;
}
$textarea.val(msg);
$div.find("a.expand-comment-response").trigger("click");
$textarea.selectEnd();
}
);
e.preventDefault();
}
Comment.show_new_comment_form = function(e) {
$(e.target).hide();
var $form = $(e.target).closest("div.new-comment").find("form");
@@ -45,4 +25,3 @@ $(document).ready(function() {
});
export default Comment

View File

@@ -0,0 +1,11 @@
var $div = $('div.comments-for-post[data-post-id="<%= @comment.post_id %>"] div.new-comment');
$div.find(".expand-comment-response").click();
var $textarea = $div.find("textarea");
var msg = "<%= j @comment.body %>";
if ($textarea.val().length > 0) {
msg = $textarea.val() + "\n\n" + msg;
}
$textarea.val(msg);
$textarea.selectEnd();

View File

@@ -24,7 +24,7 @@
<% if CurrentUser.is_member? %>
<menu>
<% if @post || @posts %>
<li><%= link_to "Reply", new_comment_path(:post_id => comment.post_id), :class => "reply-link", "data-comment-id" => comment.id %></li>
<li><%= link_to "Reply", new_comment_path(id: comment, comment: { post_id: comment.post_id }), class: "reply-link", remote: true %></li>
<% if comment.editable_by?(CurrentUser.user) %>
<% if comment.is_deleted? %>
<li><%= link_to "Undelete", undelete_comment_path(comment.id), :method => :post, :remote => true %></li>