comments.js: clean up comment quoting.
Replace Comment.quote with a remote new.js.erb call.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
class CommentsController < ApplicationController
|
class CommentsController < ApplicationController
|
||||||
respond_to :html, :xml, :json
|
respond_to :html, :xml, :json
|
||||||
|
respond_to :js, only: [:new, :destroy, :undelete]
|
||||||
before_action :member_only, :except => [:index, :search, :show]
|
before_action :member_only, :except => [:index, :search, :show]
|
||||||
skip_before_action :api_check
|
skip_before_action :api_check
|
||||||
|
|
||||||
@@ -17,7 +18,9 @@ class CommentsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def new
|
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
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
@@ -45,25 +48,21 @@ class CommentsController < ApplicationController
|
|||||||
|
|
||||||
def show
|
def show
|
||||||
@comment = Comment.find(params[:id])
|
@comment = Comment.find(params[:id])
|
||||||
respond_with(@comment, methods: [:quoted_response])
|
respond_with(@comment)
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@comment = Comment.find(params[:id])
|
@comment = Comment.find(params[:id])
|
||||||
check_privilege(@comment)
|
check_privilege(@comment)
|
||||||
@comment.delete!
|
@comment.delete!
|
||||||
respond_with(@comment) do |format|
|
respond_with(@comment)
|
||||||
format.js
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def undelete
|
def undelete
|
||||||
@comment = Comment.find(params[:id])
|
@comment = Comment.find(params[:id])
|
||||||
check_privilege(@comment)
|
check_privilege(@comment)
|
||||||
@comment.undelete!
|
@comment.undelete!
|
||||||
respond_with(@comment) do |format|
|
respond_with(@comment)
|
||||||
format.js
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -2,31 +2,11 @@ let Comment = {};
|
|||||||
|
|
||||||
Comment.initialize_all = function() {
|
Comment.initialize_all = function() {
|
||||||
if ($("#c-posts").length || $("#c-comments").length) {
|
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", ".edit_comment_link", Comment.show_edit_form);
|
||||||
$(document).on("click.danbooru.comment", ".expand-comment-response", Comment.show_new_comment_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) {
|
Comment.show_new_comment_form = function(e) {
|
||||||
$(e.target).hide();
|
$(e.target).hide();
|
||||||
var $form = $(e.target).closest("div.new-comment").find("form");
|
var $form = $(e.target).closest("div.new-comment").find("form");
|
||||||
@@ -45,4 +25,3 @@ $(document).ready(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
export default Comment
|
export default Comment
|
||||||
|
|
||||||
|
|||||||
11
app/views/comments/new.js.erb
Normal file
11
app/views/comments/new.js.erb
Normal 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();
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
<% if CurrentUser.is_member? %>
|
<% if CurrentUser.is_member? %>
|
||||||
<menu>
|
<menu>
|
||||||
<% if @post || @posts %>
|
<% 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.editable_by?(CurrentUser.user) %>
|
||||||
<% if comment.is_deleted? %>
|
<% if comment.is_deleted? %>
|
||||||
<li><%= link_to "Undelete", undelete_comment_path(comment.id), :method => :post, :remote => true %></li>
|
<li><%= link_to "Undelete", undelete_comment_path(comment.id), :method => :post, :remote => true %></li>
|
||||||
|
|||||||
Reference in New Issue
Block a user