Merge pull request #3809 from evazion/fix-comments-js
Refactor comments.js
This commit is contained in:
@@ -70,7 +70,6 @@ private
|
|||||||
def index_for_post
|
def index_for_post
|
||||||
@post = Post.find(params[:post_id])
|
@post = Post.find(params[:post_id])
|
||||||
@comments = @post.comments
|
@comments = @post.comments
|
||||||
@comments = @comments.visible(CurrentUser.user) unless params[:include_below_threshold]
|
|
||||||
render :action => "index_for_post"
|
render :action => "index_for_post"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -5,29 +5,13 @@ let Comment = {};
|
|||||||
|
|
||||||
Comment.initialize_all = function() {
|
Comment.initialize_all = function() {
|
||||||
if ($("#c-posts").length || $("#c-comments").length) {
|
if ($("#c-posts").length || $("#c-comments").length) {
|
||||||
this.initialize_response_link();
|
$(document).on("click", ".reply-link", Comment.quote);
|
||||||
this.initialize_reply_links();
|
$(document).on("click", ".edit_comment_link", Comment.show_edit_form);
|
||||||
this.initialize_expand_links();
|
$(document).on("click", ".expand-comment-response", Comment.show_new_comment_form);
|
||||||
this.initialize_vote_links();
|
|
||||||
|
|
||||||
if (!$("#a-edit").length) {
|
|
||||||
this.initialize_edit_links();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($("#c-posts").length && $("#a-show").length) {
|
$(window).on("danbooru:index_for_post", (_event, post_id, current_comment_section) => {
|
||||||
Comment.highlight_threshold_comments(Utility.meta("post-id"));
|
$("#threshold-comments-notice-for-" + post_id).hide();
|
||||||
}
|
|
||||||
|
|
||||||
$(window).on("danbooru:index_for_post", (_event, post_id, current_comment_section, include_below_threshold) => {
|
|
||||||
if (include_below_threshold) {
|
|
||||||
$("#threshold-comments-notice-for-" + post_id).hide();
|
|
||||||
} else {
|
|
||||||
Comment.highlight_threshold_comments(post_id);
|
|
||||||
}
|
|
||||||
Comment.initialize_reply_links(current_comment_section);
|
|
||||||
Comment.initialize_edit_links(current_comment_section);
|
|
||||||
Comment.initialize_vote_links(current_comment_section);
|
|
||||||
Dtext.initialize_expandables(current_comment_section);
|
Dtext.initialize_expandables(current_comment_section);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -51,69 +35,17 @@ Comment.quote = function(e) {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
Comment.initialize_reply_links = function($parent) {
|
Comment.show_new_comment_form = function(e) {
|
||||||
$parent = $parent || $(document);
|
$(e.target).hide();
|
||||||
$parent.find(".reply-link").click(Comment.quote);
|
var $form = $(e.target).closest("div.new-comment").find("form");
|
||||||
|
$form.show();
|
||||||
|
Utility.scroll_to($form);
|
||||||
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
Comment.initialize_expand_links = function() {
|
Comment.show_edit_form = function(e) {
|
||||||
$(".comment-section form").hide();
|
$(this).closest(".comment").find(".edit_comment").show();
|
||||||
$(".comment-section input.expand-comment-response").click(function(e) {
|
e.preventDefault();
|
||||||
var post_id = $(this).closest(".comment-section").data("post-id");
|
|
||||||
$(this).hide();
|
|
||||||
$(".comment-section[data-post-id=" + post_id + "] form").slideDown("fast");
|
|
||||||
e.preventDefault();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Comment.initialize_response_link = function() {
|
|
||||||
$("a.expand-comment-response").click(function(e) {
|
|
||||||
$(e.target).hide();
|
|
||||||
var $form = $(e.target).closest("div.new-comment").find("form");
|
|
||||||
$form.show();
|
|
||||||
Utility.scroll_to($form);
|
|
||||||
e.preventDefault();
|
|
||||||
});
|
|
||||||
|
|
||||||
$("div.new-comment form").hide();
|
|
||||||
}
|
|
||||||
|
|
||||||
Comment.initialize_edit_links = function($parent) {
|
|
||||||
$parent = $parent || $(document);
|
|
||||||
$parent.find(".edit_comment").hide();
|
|
||||||
$parent.find(".edit_comment_link").click(function(e) {
|
|
||||||
var link_id = $(this).attr("id");
|
|
||||||
var comment_id = link_id.match(/^edit_comment_link_(\d+)$/)[1];
|
|
||||||
$("#edit_comment_" + comment_id).fadeToggle("fast");
|
|
||||||
e.preventDefault();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Comment.highlight_threshold_comments = function(post_id) {
|
|
||||||
var threshold = parseInt(Utility.meta("user-comment-threshold"));
|
|
||||||
var articles = $("article.comment[data-post-id=" + post_id + "]");
|
|
||||||
articles.each(function(i, v) {
|
|
||||||
var $comment = $(v);
|
|
||||||
if (parseInt($comment.data("score")) < threshold) {
|
|
||||||
$comment.addClass("below-threshold");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Comment.hide_threshold_comments = function(post_id) {
|
|
||||||
var threshold = parseInt(Utility.meta("user-comment-threshold"));
|
|
||||||
var articles = $("article.comment[data-post-id=" + post_id + "]");
|
|
||||||
articles.each(function(i, v) {
|
|
||||||
var $comment = $(v);
|
|
||||||
if (parseInt($comment.data("score")) < threshold) {
|
|
||||||
$comment.hide();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Comment.initialize_vote_links = function($parent) {
|
|
||||||
$parent = $parent || $(document);
|
|
||||||
$parent.find(".unvote-comment-link").hide();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
|
|||||||
@@ -189,6 +189,10 @@ class Comment < ApplicationRecord
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def below_threshold?(user = CurrentUser.user)
|
||||||
|
score < user.comment_threshold
|
||||||
|
end
|
||||||
|
|
||||||
def editable_by?(user)
|
def editable_by?(user)
|
||||||
creator_id == user.id || user.is_moderator?
|
creator_id == user.id || user.is_moderator?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<%= error_messages_for :comment %>
|
<%= error_messages_for :comment %>
|
||||||
|
|
||||||
<%= simple_form_for(comment, :html => {:class => "edit_comment"}) do |f| %>
|
<%= simple_form_for(comment, :html => {:style => ("display: none;" if local_assigns[:hidden]), :class => "edit_comment"}) do |f| %>
|
||||||
<%= f.hidden_field :post_id %>
|
<%= f.hidden_field :post_id %>
|
||||||
<%= dtext_field "comment", "body", :classes => "autocomplete-mentions", :value => comment.body, :input_id => "comment_body_for_#{comment.id}", :preview_id => "dtext-preview-for-#{comment.id}" %>
|
<%= dtext_field "comment", "body", :classes => "autocomplete-mentions", :value => comment.body, :input_id => "comment_body_for_#{comment.id}", :preview_id => "dtext-preview-for-#{comment.id}" %>
|
||||||
<%= f.button :submit, "Submit" %>
|
<%= f.button :submit, "Submit" %>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<div id="a-edit">
|
<div id="a-edit">
|
||||||
<h1>Edit Comment</h1>
|
<h1>Edit Comment</h1>
|
||||||
|
|
||||||
<%= render "comments/form", :post => @comment.post, :comment => @comment %>
|
<%= render "comments/form", comment: @comment %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -2,4 +2,4 @@ $("#hidden-comments-notice-for-<%= @post.id %>").hide();
|
|||||||
|
|
||||||
var current_comment_section = $("div.comments-for-post[data-post-id=<%= @post.id %>] div.list-of-comments");
|
var current_comment_section = $("div.comments-for-post[data-post-id=<%= @post.id %>] div.list-of-comments");
|
||||||
current_comment_section.html("<%= j(render(:partial => 'comments/partials/show/comment', :collection => @comments))%>");
|
current_comment_section.html("<%= j(render(:partial => 'comments/partials/show/comment', :collection => @comments))%>");
|
||||||
$(window).trigger("danbooru:index_for_post", [<%= @post.id %>, current_comment_section, <%= ActiveModel::Type::Boolean.new.cast(params[:include_below_threshold]) %>]);
|
$(window).trigger("danbooru:index_for_post", [<%= @post.id %>, current_comment_section]);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<div class="row notices">
|
<div class="row notices">
|
||||||
<% if post.comments.hidden(CurrentUser.user).count > 0 || (params[:controller] == "comments" && post.comments.count > 6) %>
|
<% if post.comments.hidden(CurrentUser.user).count > 0 || (params[:controller] == "comments" && post.comments.count > 6) %>
|
||||||
<span class="info" id="threshold-comments-notice-for-<%= post.id %>">
|
<span class="info" id="threshold-comments-notice-for-<%= post.id %>">
|
||||||
<%= link_to "Show all comments", comments_path(:post_id => post.id, :include_below_threshold => true), :remote => true %>
|
<%= link_to "Show all comments", comments_path(:post_id => post.id), :remote => true %>
|
||||||
</span>
|
</span>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
<% if CurrentUser.is_member? %>
|
<% if CurrentUser.is_member? %>
|
||||||
<div class="new-comment">
|
<div class="new-comment">
|
||||||
<p><%= link_to "Post comment", new_comment_path, :class => "expand-comment-response" %></p>
|
<p><%= link_to "Post comment", new_comment_path, :class => "expand-comment-response" %></p>
|
||||||
<%= render "comments/form", :post => post, :comment => post.comments.new %>
|
<%= render "comments/form", comment: post.comments.new, hidden: true %>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<% if CurrentUser.is_moderator? || (params[:search] && params[:search][:is_deleted] =~ /t/) || !comment.is_deleted? %>
|
<% if CurrentUser.is_moderator? || (params[:search] && params[:search][:is_deleted] =~ /t/) || !comment.is_deleted? %>
|
||||||
<a name="comment-<%= comment.id %>"></a>
|
<a name="comment-<%= comment.id %>"></a>
|
||||||
<article class="comment" data-post-id="<%= comment.post_id %>" data-comment-id="<%= comment.id %>" data-score="<%= comment.score %>" data-creator="<%= comment.creator_name %>" data-is-sticky="<%= comment.is_sticky %>">
|
<article class="comment <%= "below-threshold" if comment.below_threshold? %>" data-post-id="<%= comment.post_id %>" data-comment-id="<%= comment.id %>" data-score="<%= comment.score %>" data-creator="<%= comment.creator_name %>" data-is-sticky="<%= comment.is_sticky %>">
|
||||||
<div class="author">
|
<div class="author">
|
||||||
<h1>
|
<h1>
|
||||||
<%= link_to_user comment.creator %>
|
<%= link_to_user comment.creator %>
|
||||||
@@ -35,7 +35,7 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
<li id="comment-vote-up-link-for-<%= comment.id %>"><%= link_to "Vote up", comment_votes_path(:comment_id => comment.id, :score => "up"), :method => :post, :remote => true %></li>
|
<li id="comment-vote-up-link-for-<%= comment.id %>"><%= link_to "Vote up", comment_votes_path(:comment_id => comment.id, :score => "up"), :method => :post, :remote => true %></li>
|
||||||
<li id="comment-vote-down-link-for-<%= comment.id %>"><%= link_to "Vote down", comment_votes_path(:comment_id => comment.id, :score => "down"), :method => :post, :remote => true %></li>
|
<li id="comment-vote-down-link-for-<%= comment.id %>"><%= link_to "Vote down", comment_votes_path(:comment_id => comment.id, :score => "down"), :method => :post, :remote => true %></li>
|
||||||
<li id="comment-unvote-link-for-<%= comment.id %>" class="unvote-comment-link"><%= link_to "Unvote", comment_votes_path(:comment_id => comment.id), :method => :delete, :remote => true %></li>
|
<li style="display: none;" id="comment-unvote-link-for-<%= comment.id %>" class="unvote-comment-link"><%= link_to "Unvote", comment_votes_path(:comment_id => comment.id), :method => :delete, :remote => true %></li>
|
||||||
<% if CurrentUser.is_moderator? %>
|
<% if CurrentUser.is_moderator? %>
|
||||||
<li>|</li>
|
<li>|</li>
|
||||||
<li>
|
<li>
|
||||||
@@ -46,7 +46,7 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
</menu>
|
</menu>
|
||||||
<% if comment.editable_by?(CurrentUser.user) %>
|
<% if comment.editable_by?(CurrentUser.user) %>
|
||||||
<%= render "comments/form", :post => comment.post, :comment => comment %>
|
<%= render "comments/form", comment: comment, hidden: true %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user