From f886f2fdb3dd8176766bf64912e99a720de70941 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 26 Sep 2018 17:17:46 -0500 Subject: [PATCH 1/4] posts/show: load flag/appeal/replacement dialogs via ajax (#3922). --- app/controllers/post_appeals_controller.rb | 2 +- app/controllers/post_flags_controller.rb | 2 +- .../post_replacements_controller.rb | 5 ++- app/controllers/posts_controller.rb | 4 +- .../src/javascripts/post_appeals.js | 35 ----------------- app/javascript/src/javascripts/post_flags.js | 39 ------------------- app/javascript/src/javascripts/posts.js.erb | 27 ------------- app/javascript/src/javascripts/utility.js | 25 ++++++++++++ app/views/artist_commentaries/_form.html.erb | 2 +- app/views/post_appeals/_new.html.erb | 20 ++++++---- app/views/post_appeals/new.js.erb | 1 + app/views/post_flags/_new.html.erb | 20 ++++++---- app/views/post_flags/new.js.erb | 1 + app/views/post_replacements/_new.html.erb | 18 +++++---- app/views/post_replacements/new.html.erb | 2 +- app/views/post_replacements/new.js.erb | 1 + .../posts/partials/show/_options.html.erb | 6 +-- app/views/posts/show.html.erb | 12 ------ 18 files changed, 74 insertions(+), 148 deletions(-) delete mode 100644 app/javascript/src/javascripts/post_appeals.js delete mode 100644 app/javascript/src/javascripts/post_flags.js create mode 100644 app/views/post_appeals/new.js.erb create mode 100644 app/views/post_flags/new.js.erb create mode 100644 app/views/post_replacements/new.js.erb diff --git a/app/controllers/post_appeals_controller.rb b/app/controllers/post_appeals_controller.rb index 18a14494b..116c94030 100644 --- a/app/controllers/post_appeals_controller.rb +++ b/app/controllers/post_appeals_controller.rb @@ -3,7 +3,7 @@ class PostAppealsController < ApplicationController respond_to :html, :xml, :json, :js def new - @post_appeal = PostAppeal.new + @post_appeal = PostAppeal.new(post_appeal_params) respond_with(@post_appeal) end diff --git a/app/controllers/post_flags_controller.rb b/app/controllers/post_flags_controller.rb index b80bad83b..b64a729e5 100644 --- a/app/controllers/post_flags_controller.rb +++ b/app/controllers/post_flags_controller.rb @@ -3,7 +3,7 @@ class PostFlagsController < ApplicationController respond_to :html, :xml, :json, :js def new - @post_flag = PostFlag.new + @post_flag = PostFlag.new(post_flag_params) respond_with(@post_flag) end diff --git a/app/controllers/post_replacements_controller.rb b/app/controllers/post_replacements_controller.rb index c83fd7c8b..8327b2a5e 100644 --- a/app/controllers/post_replacements_controller.rb +++ b/app/controllers/post_replacements_controller.rb @@ -1,9 +1,10 @@ class PostReplacementsController < ApplicationController - respond_to :html, :xml, :json + respond_to :html, :xml, :json, :js before_action :moderator_only, except: [:index] def new - @post = Post.find(params[:post_id]) + @post_replacement = Post.find(params[:post_id]).replacements.new + respond_with(@post_replacement) end def create diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 01ef84c76..3e9fd8e62 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -23,9 +23,7 @@ class PostsController < ApplicationController def show @post = Post.find(params[:id]) - @post_flag = PostFlag.new(:post_id => @post.id) - @post_appeal = PostAppeal.new(:post_id => @post.id) - + include_deleted = @post.is_deleted? || (@post.parent_id.present? && @post.parent.is_deleted?) || CurrentUser.user.show_deleted_children? @parent_post_set = PostSets::PostRelationship.new(@post.parent_id, :include_deleted => include_deleted) @children_post_set = PostSets::PostRelationship.new(@post.id, :include_deleted => include_deleted) diff --git a/app/javascript/src/javascripts/post_appeals.js b/app/javascript/src/javascripts/post_appeals.js deleted file mode 100644 index 814c18343..000000000 --- a/app/javascript/src/javascripts/post_appeals.js +++ /dev/null @@ -1,35 +0,0 @@ -let PostAppeal = {}; - -PostAppeal.initialize_all = function() { - if ($("#c-posts").length && $("#a-show").length) { - this.initialize_appeal(); - } -} - -PostAppeal.initialize_appeal = function() { - $("#appeal-dialog").dialog({ - autoOpen: false, - width: 700, - modal: true, - buttons: { - "Submit": function() { - $("#appeal-dialog form").submit(); - $(this).dialog("close"); - }, - "Cancel": function() { - $(this).dialog("close"); - } - } - }); - - $("#appeal").on("click.danbooru", function(e) { - e.preventDefault(); - $("#appeal-dialog").dialog("open"); - }); -} - -$(document).ready(function() { - PostAppeal.initialize_all(); -}); - -export default PostAppeal diff --git a/app/javascript/src/javascripts/post_flags.js b/app/javascript/src/javascripts/post_flags.js deleted file mode 100644 index 27f4d56d3..000000000 --- a/app/javascript/src/javascripts/post_flags.js +++ /dev/null @@ -1,39 +0,0 @@ -let PostFlag = {}; - -PostFlag.initialize_all = function() { - if ($("#c-posts").length && $("#a-show").length) { - this.initialize_flag(); - } -} - -PostFlag.initialize_flag = function() { - $("#flag-dialog").dialog({ - autoOpen: false, - width: 700, - modal: true, - buttons: { - "Submit": function() { - $("#flag-dialog form").submit(); - $(this).dialog("close"); - }, - "Cancel": function() { - $(this).dialog("close"); - } - } - }); - - $('#flag-dialog form').submit(function() { - $('#flag-dialog').dialog('close'); - }); - - $("#flag").on("click.danbooru", function(e) { - e.preventDefault(); - $("#flag-dialog").dialog("open"); - }); -} - -$(function() { - PostFlag.initialize_all(); -}); - -export default PostFlag diff --git a/app/javascript/src/javascripts/posts.js.erb b/app/javascript/src/javascripts/posts.js.erb index 9a1eb4b96..795c23e87 100644 --- a/app/javascript/src/javascripts/posts.js.erb +++ b/app/javascript/src/javascripts/posts.js.erb @@ -31,7 +31,6 @@ Post.initialize_all = function() { this.initialize_post_image_resize_links(); this.initialize_post_image_resize_to_window_link(); this.initialize_similar(); - this.initialize_replace_image_dialog(); this.initialize_gestures(); if ((Utility.meta("always-resize-images") === "true") || (Utility.meta("viewport") && (window.screen.width <= 660))) { @@ -616,32 +615,6 @@ Post.initialize_saved_searches = function() { }); } -Post.initialize_replace_image_dialog = function() { - $("#replace-image-dialog").dialog({ - autoOpen: false, - width: 700, - modal: true, - buttons: { - "Submit": function() { - $("#replace-image-dialog form").submit(); - $(this).dialog("close"); - }, - "Cancel": function() { - $(this).dialog("close"); - } - } - }); - - $('#replace-image-dialog form').submit(function() { - $('#replace-image-dialog').dialog('close'); - }); - - $("#replace-image").on("click.danbooru", function(e) { - e.preventDefault(); - $("#replace-image-dialog").dialog("open"); - }); -}; - $(document).ready(function() { Post.initialize_all(); }); diff --git a/app/javascript/src/javascripts/utility.js b/app/javascript/src/javascripts/utility.js index 35ade85ec..802855e17 100644 --- a/app/javascript/src/javascripts/utility.js +++ b/app/javascript/src/javascripts/utility.js @@ -36,6 +36,31 @@ Utility.error = function(msg) { } } +Utility.dialog = function(title, html) { + const $dialog = $(html).dialog({ + title: title, + width: 700, + modal: true, + close: function() { + // Defer removing the dialog to avoid detaching the
tag before the + // form is submitted (which would prevent the submission from going through). + $(() => $dialog.dialog("destroy")); + }, + buttons: { + "Submit": function() { + $dialog.find("form").submit(); + }, + "Cancel": function() { + $dialog.dialog("close"); + } + } + }); + + $dialog.find("form").on("submit.danbooru", function() { + $dialog.dialog("close"); + }); +} + Utility.keydown = function(keys, namespace, handler) { if (Utility.meta("enable-js-navigation") === "true") { $(document).on("keydown.danbooru." + namespace, null, keys, handler); diff --git a/app/views/artist_commentaries/_form.html.erb b/app/views/artist_commentaries/_form.html.erb index 537e42b1a..dee5c5a0a 100644 --- a/app/views/artist_commentaries/_form.html.erb +++ b/app/views/artist_commentaries/_form.html.erb @@ -5,7 +5,7 @@ <%= select_tag :commentary_source_type, options_for_select(%w[Source Post]) %> <%= text_field_tag :commentary_source, post.source %> - <%= text_field_tag :commentary_post_id, (post.parent.try(&:id) || post.children.first.try(&:id)), :style => "display: none;" %> + <%= text_field_tag :commentary_post_id, post.parent_id, :style => "display: none;" %> <%= button_tag "Fetch" %>
diff --git a/app/views/post_appeals/_new.html.erb b/app/views/post_appeals/_new.html.erb index c06e3705f..b652c1d08 100644 --- a/app/views/post_appeals/_new.html.erb +++ b/app/views/post_appeals/_new.html.erb @@ -1,9 +1,13 @@ -<%= format_text(WikiPage.titled(Danbooru.config.appeal_notice_wiki_page).first.try(&:body)) %> +
+
+ <%= format_text(WikiPage.titled(Danbooru.config.appeal_notice_wiki_page).first.try(&:body)) %> +
-<%# XXX dtext_field expects there to be a `post_appeal` instance variable. %> -<% @post_appeal = post_appeal %> -<%= simple_form_for(@post_appeal, format: :js, remote: true) do |f| %> - <%= f.hidden_field :post_id %> - <%= dtext_field "post_appeal", "reason", preview_id: "dtext-preview-for-post-appeal" %> - <%= dtext_preview_button "post_appeal", "reason", preview_id: "dtext-preview-for-post-appeal" %> -<% end %> + <%# XXX dtext_field expects there to be a `post_appeal` instance variable. %> + <% @post_appeal = post_appeal %> + <%= simple_form_for(@post_appeal, format: :js, remote: true) do |f| %> + <%= f.hidden_field :post_id %> + <%= dtext_field "post_appeal", "reason", preview_id: "dtext-preview-for-post-appeal", type: "string" %> + <%= dtext_preview_button "post_appeal", "reason", preview_id: "dtext-preview-for-post-appeal" %> + <% end %> +
diff --git a/app/views/post_appeals/new.js.erb b/app/views/post_appeals/new.js.erb new file mode 100644 index 000000000..f03b42d35 --- /dev/null +++ b/app/views/post_appeals/new.js.erb @@ -0,0 +1 @@ +Danbooru.Utility.dialog("Appeal post", "<%= j render "post_appeals/new", post_appeal: @post_appeal %>"); diff --git a/app/views/post_flags/_new.html.erb b/app/views/post_flags/_new.html.erb index da735b3b7..280be9949 100644 --- a/app/views/post_flags/_new.html.erb +++ b/app/views/post_flags/_new.html.erb @@ -1,9 +1,13 @@ -<%= format_text(WikiPage.titled(Danbooru.config.flag_notice_wiki_page).first.try(&:body)) %> +
+
+ <%= format_text(WikiPage.titled(Danbooru.config.flag_notice_wiki_page).first.try(&:body)) %> +
-<%# XXX dtext_field expects there to be a `post_flag` instance variable. %> -<% @post_flag = post_flag %> -<%= simple_form_for(@post_flag, format: :js, remote: true) do |f| %> - <%= f.hidden_field :post_id %> - <%= dtext_field "post_flag", "reason", preview_id: "dtext-preview-for-post-flag", type: "string" %> - <%= dtext_preview_button "post_flag", "reason", preview_id: "dtext-preview-for-post-flag" %> -<% end %> + <%# XXX dtext_field expects there to be a `post_flag` instance variable. %> + <% @post_flag = post_flag %> + <%= simple_form_for(@post_flag, format: :js, remote: true) do |f| %> + <%= f.hidden_field :post_id %> + <%= dtext_field "post_flag", "reason", preview_id: "dtext-preview-for-post-flag", type: "string" %> + <%= dtext_preview_button "post_flag", "reason", preview_id: "dtext-preview-for-post-flag" %> + <% end %> +
diff --git a/app/views/post_flags/new.js.erb b/app/views/post_flags/new.js.erb new file mode 100644 index 000000000..ec1517cf3 --- /dev/null +++ b/app/views/post_flags/new.js.erb @@ -0,0 +1 @@ +Danbooru.Utility.dialog("Flag post", "<%= j render "post_flags/new", post_flag: @post_flag %>"); diff --git a/app/views/post_replacements/_new.html.erb b/app/views/post_replacements/_new.html.erb index dce3540cb..6cd800187 100644 --- a/app/views/post_replacements/_new.html.erb +++ b/app/views/post_replacements/_new.html.erb @@ -1,8 +1,12 @@ -<%= format_text(WikiPage.titled(Danbooru.config.replacement_notice_wiki_page).first.try(&:body)) %> +
+
+ <%= format_text(WikiPage.titled(Danbooru.config.replacement_notice_wiki_page).first.try(&:body)) %> +
-<%= simple_form_for(post_replacement, url: post_replacements_path(post_id: post_replacement.post_id), method: :post) do |f| %> - <%= f.input :replacement_file, label: "File", as: :file %> - <%= f.input :replacement_url, label: "Replacement URL", hint: "The source URL to download the replacement from.", as: :string, input_html: { value: post_replacement.post.normalized_source } %> - <%= f.input :final_source, label: "Final Source", hint: "If present, the source field will be changed to this after replacement.", as: :string, input_html: { value: post_replacement.post.source } %> - <%= f.input :tags, label: "Tags", as: :text, input_html: { value: post_replacement.suggested_tags_for_removal, data: { autocomplete: "tag-edit" } } %> -<% end %> + <%= simple_form_for(post_replacement, url: post_replacements_path(post_id: post_replacement.post_id), method: :post) do |f| %> + <%= f.input :replacement_file, label: "File", as: :file %> + <%= f.input :replacement_url, label: "Replacement URL", hint: "The source URL to download the replacement from.", as: :string, input_html: { value: post_replacement.post.normalized_source } %> + <%= f.input :final_source, label: "Final Source", hint: "If present, the source field will be changed to this after replacement.", as: :string, input_html: { value: post_replacement.post.source } %> + <%= f.input :tags, label: "Tags", as: :string, input_html: { value: post_replacement.suggested_tags_for_removal, data: { autocomplete: "tag-edit" } } %> + <% end %> +
diff --git a/app/views/post_replacements/new.html.erb b/app/views/post_replacements/new.html.erb index 6286252b2..dace2eaa4 100644 --- a/app/views/post_replacements/new.html.erb +++ b/app/views/post_replacements/new.html.erb @@ -1,5 +1,5 @@
- <%= render "new", post_replacement: @post.replacements.new %> + <%= render "new", post_replacement: @post_replacement %>
diff --git a/app/views/post_replacements/new.js.erb b/app/views/post_replacements/new.js.erb new file mode 100644 index 000000000..6a6943ff1 --- /dev/null +++ b/app/views/post_replacements/new.js.erb @@ -0,0 +1 @@ +Danbooru.Utility.dialog("Replace image", "<%= j render "post_replacements/new", post_replacement: @post_replacement %>"); diff --git a/app/views/posts/partials/show/_options.html.erb b/app/views/posts/partials/show/_options.html.erb index 59545f93b..15352e1c0 100644 --- a/app/views/posts/partials/show/_options.html.erb +++ b/app/views/posts/partials/show/_options.html.erb @@ -22,9 +22,9 @@
  • Status locked
  • <% else %> <% if !post.is_deleted? && !post.is_pending? && !post.is_flagged? %> -
  • <%= link_to "Flag", new_post_flag_path(:post_id => post.id), :id => "flag" %>
  • +
  • <%= link_to "Flag", new_post_flag_path(post_flag: { post_id: post.id }), id: "flag", remote: true %>
  • <% elsif post.is_flagged? || post.is_deleted? %> -
  • <%= link_to "Appeal", new_post_appeal_path(:post_id => post.id), :id => "appeal" %>
  • +
  • <%= link_to "Appeal", new_post_appeal_path(post_appeal: { post_id: post.id }), id: "appeal", remote: true %>
  • <% end %> <% if CurrentUser.can_approve_posts? %> @@ -53,7 +53,7 @@ <% end %> <% if CurrentUser.is_moderator? %> -
  • <%= link_to "Replace Image", new_post_replacement_path(:post_id => post.id), :id => "replace-image" %>
  • +
  • <%= link_to "Replace Image", new_post_replacement_path(post_id: post.id), id: "replace-image", remote: true %>
  • <%= link_to "Down vote report", reports_down_voting_post_path(post_id: post.id) %>
  • <% end %> <% end %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 1388db82f..a6b15d4a1 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -135,18 +135,6 @@ - - - - - - From 6aecbf4a7e763b9c8f105ceb90ecbf88291677bf Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 26 Sep 2018 02:17:07 -0500 Subject: [PATCH 2/4] posts/show: skip rendering dialogs for anon users. Anonymous users can't use any of the artist commentary / add to pool / add to favgroup dialogs, so rendering them is unnecessary. --- app/views/posts/show.html.erb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index a6b15d4a1..dddda0107 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -135,17 +135,19 @@ - + <% if CurrentUser.is_member? %> + - + - + + <% end %> <%= post_view_count_js %> From a5438aaf3703d43ac74a2a24dcddf07ffac8dddf Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 26 Sep 2018 19:49:56 -0500 Subject: [PATCH 3/4] Fix dtext preview button to use event delegation. Fixes the dtext preview button and dtext expandable to work with dynamically loaded dtext forms. --- app/helpers/application_helper.rb | 6 ++---- app/javascript/src/javascripts/comments.js | 1 - app/javascript/src/javascripts/dtext.js | 8 +++----- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1632d9766..49082424c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -181,10 +181,8 @@ module ApplicationHelper render "dtext/form", options end - def dtext_preview_button(object, name, options = {}) - options[:input_id] ||= "#{object}_#{name}" - options[:preview_id] ||= "dtext-preview" - submit_tag("Preview", "data-input-id" => options[:input_id], "data-preview-id" => options[:preview_id]) + def dtext_preview_button(object, name, input_id: "#{object}_#{name}", preview_id: "dtext-preview") + tag.input value: "Preview", type: "button", class: "dtext-preview-button", "data-input-id": input_id, "data-preview-id": preview_id end def search_field(method, label: method.titleize, hint: nil, value: nil, **attributes) diff --git a/app/javascript/src/javascripts/comments.js b/app/javascript/src/javascripts/comments.js index 7038cbf0b..9d67427b7 100644 --- a/app/javascript/src/javascripts/comments.js +++ b/app/javascript/src/javascripts/comments.js @@ -11,7 +11,6 @@ Comment.initialize_all = function() { $(window).on("danbooru:index_for_post", (_event, post_id, current_comment_section) => { $("#threshold-comments-notice-for-" + post_id).hide(); - Dtext.initialize_expandables(current_comment_section); }); } diff --git a/app/javascript/src/javascripts/dtext.js b/app/javascript/src/javascripts/dtext.js index e9a633f42..59121e65b 100644 --- a/app/javascript/src/javascripts/dtext.js +++ b/app/javascript/src/javascripts/dtext.js @@ -6,12 +6,11 @@ Dtext.initialize_all = function() { } Dtext.initialize_links = function() { - $(".simple_form input[value=Preview]").on("click.danbooru", Dtext.click_button); + $(document).on("click.danbooru", ".dtext-preview-button", Dtext.click_button); } -Dtext.initialize_expandables = function($parent) { - $parent = $parent || $(document); - $parent.find(".expandable-button").on("click.danbooru", function(e) { +Dtext.initialize_expandables = function() { + $(document).on("click.danbooru", ".expandable-button", function(e) { var button = $(this); button.parent().next().fadeToggle("fast"); if (button.val() === "Show") { @@ -34,7 +33,6 @@ Dtext.call_preview = function(e, $button, $input, $preview) { }, success: function(data) { $preview.html(data).fadeIn("fast"); - Dtext.initialize_expandables($preview); } }); } From 654d40875f09766c6be305dcfaaf4ee269a25ab5 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 26 Sep 2018 20:46:22 -0500 Subject: [PATCH 4/4] posts/show: only render detailed rejection dialog for approvers. --- app/views/posts/partials/show/_notices.html.erb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/views/posts/partials/show/_notices.html.erb b/app/views/posts/partials/show/_notices.html.erb index 86b9f795d..343a1d481 100644 --- a/app/views/posts/partials/show/_notices.html.erb +++ b/app/views/posts/partials/show/_notices.html.erb @@ -42,6 +42,7 @@ <% if CurrentUser.can_approve_posts? && !post.disapproved_by?(CurrentUser.user) %> <%= render "moderator/post/queues/quick_mod", post: post %> + <%= render "post_disapprovals/detailed_rejection_dialog" %> <% end %> <% end %> @@ -73,5 +74,3 @@ Loading... <% end %> - -<%= render "post_disapprovals/detailed_rejection_dialog" %>