diff --git a/app/assets/javascripts/artist_commentaries.js b/app/assets/javascripts/artist_commentaries.js index 44845c697..6e3b0a5ee 100644 --- a/app/assets/javascripts/artist_commentaries.js +++ b/app/assets/javascripts/artist_commentaries.js @@ -53,6 +53,91 @@ e.preventDefault(); $("#add-commentary-dialog").dialog("open"); }); + + $('#fetch-commentary select[name="commentary_source_type"]').change(function() { + $("#fetch-commentary input").toggle(); + }); + + $('#fetch-commentary button[type="submit"]').click(Danbooru.ArtistCommentary.fetch_commentary); + } + + Danbooru.ArtistCommentary.fetch_commentary = function() { + Danbooru.notice("Fetching artist commentary..."); + + var type = $('#fetch-commentary select[name="commentary_source_type"]').val(); + if (type === "Source") { + var source = $('#fetch-commentary input[name="commentary_source"]').val(); + var commentary = Danbooru.ArtistCommentary.from_source(source); + } else if (type === "Post") { + var id = $('#fetch-commentary input[name="commentary_post_id"]').val(); + var commentary = Danbooru.ArtistCommentary.from_post_id(id); + } + + commentary.then(Danbooru.ArtistCommentary.fill_commentary).done(function (success) { + var message = success ? "Artist commentary copied." : "Artist commentary copied; conflicting fields ignored."; + Danbooru.notice(message); + }).fail(function () { + Danbooru.notice("Fetching artist commentary failed."); + }); + + return false; + }; + + Danbooru.ArtistCommentary.from_source = function(source) { + return $.get("/source.json?url=" + encodeURIComponent(source)).then(function(data) { + return { + original_title: data.artist_commentary.title, + original_description: data.artist_commentary.description, + source: source, + }; + }); + }; + + Danbooru.ArtistCommentary.from_post_id = function(post_id) { + return $.get("/posts/" + encodeURIComponent(post_id) + "/artist_commentary.json").then(function(commentary) { + commentary.source = "post #" + post_id; + return commentary; + }); + }; + + Danbooru.ArtistCommentary.fill_commentary = function(commentary) { + var description = Danbooru.ArtistCommentary.merge_commentaries($("#artist_commentary_original_description").val().trim(), commentary); + $("#artist_commentary_original_description").val(description); + + // Update the other fields if they're blank. Return success if none conflict. + return [ + Danbooru.ArtistCommentary.update_field($("#artist_commentary_original_title"), commentary.original_title), + Danbooru.ArtistCommentary.update_field($("#artist_commentary_translated_title"), commentary.translated_title), + Danbooru.ArtistCommentary.update_field($("#artist_commentary_translated_description"), commentary.translated_description), + ].every(function (i) { return i; }); + }; + + // If the new description conflicts with the current description, merge them + // by appending the new description onto the old one. + Danbooru.ArtistCommentary.merge_commentaries = function(description, commentary) { + if ((commentary.original_description && description) && + (commentary.original_description != description)) { + return description + + "\n\n[tn]\nSource: " + $("#post_source").val() + "\n[/tn]" + + "\n\nh6. " + (commentary.original_title || "Untitled") + + "\n\n" + commentary.original_description + + "\n\n[tn]\nSource: " + commentary.source + "\n[/tn]"; + } else { + return commentary.original_description || description; + } + }; + + // Update commentary field if it's blank, signal an error if there's a conflict. + Danbooru.ArtistCommentary.update_field = function($field, value) { + if ($field.val().trim() === "") { + $field.val(value); + return true; + } else if ($field.val().trim() !== value) { + $field.effect("shake", { direction: "up", distance: 5 }); + return false; + } else { + return true; + } } })(); diff --git a/app/controllers/artist_commentaries_controller.rb b/app/controllers/artist_commentaries_controller.rb index 51d338f3f..a5a7e53bd 100644 --- a/app/controllers/artist_commentaries_controller.rb +++ b/app/controllers/artist_commentaries_controller.rb @@ -1,6 +1,6 @@ class ArtistCommentariesController < ApplicationController respond_to :html, :xml, :json, :js - before_filter :member_only, :except => [:index] + before_filter :member_only, :except => [:index, :show] def index @commentaries = ArtistCommentary.search(params[:search]).order("artist_commentaries.id desc").paginate(params[:page], :limit => params[:limit]) @@ -11,6 +11,18 @@ class ArtistCommentariesController < ApplicationController end end + def show + if params[:id] + @commentary = ArtistCommentary.find(params[:id]) + else + @commentary = ArtistCommentary.find_by_post_id!(params[:post_id]) + end + + respond_with(@commentary) do |format| + format.html { redirect_to post_path(@commentary.post) } + end + end + def create_or_update @artist_commentary = ArtistCommentary.find_by_post_id(params[:artist_commentary][:post_id]) diff --git a/app/logical/sources/site.rb b/app/logical/sources/site.rb index 8c76c1b95..6c1838253 100644 --- a/app/logical/sources/site.rb +++ b/app/logical/sources/site.rb @@ -65,7 +65,11 @@ module Sources :danbooru_name => artist_record.try(:first).try(:name), :danbooru_id => artist_record.try(:first).try(:id), :unique_id => unique_id, - :page_count => page_count + :page_count => page_count, + :artist_commentary => { + :title => artist_commentary_title, + :description => artist_commentary_desc, + } }.to_json end diff --git a/app/views/artist_commentaries/_form.html.erb b/app/views/artist_commentaries/_form.html.erb index b173e342e..0c7fd6e02 100644 --- a/app/views/artist_commentaries/_form.html.erb +++ b/app/views/artist_commentaries/_form.html.erb @@ -1,5 +1,15 @@

If the artist of this image posted some interesting additional information about this work, you can copy it here. <%= link_to "View help.", wiki_pages_path(:search => {:title => "help:artist_commentary"}) %>

+
+
+ + <%= 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;" %> + <%= button_tag "Fetch" %> +
+
+ <%= form_tag(create_or_update_artist_commentaries_path(:format => :js), :remote => true, :class => "simple_form", :method => :put) do %> <%= hidden_field :artist_commentary, :post_id, :value => @post.id %> diff --git a/app/views/artist_commentaries/_quick_search.html.erb b/app/views/artist_commentaries/_quick_search.html.erb index f654021ae..5c24eb488 100644 --- a/app/views/artist_commentaries/_quick_search.html.erb +++ b/app/views/artist_commentaries/_quick_search.html.erb @@ -1,3 +1,3 @@ <%= form_tag(artist_commentaries_path, :method => :get) do %> - <%= text_field "search", "name", :id => "quick_search_name", :placeholder => "Search commentary" %> + <%= text_field "search", "text_matches", :id => "quick_search_text_matches", :placeholder => "Search commentary" %> <% end %> diff --git a/app/views/artist_commentaries/_secondary_links.html.erb b/app/views/artist_commentaries/_secondary_links.html.erb index 514bd1895..b2df444b6 100644 --- a/app/views/artist_commentaries/_secondary_links.html.erb +++ b/app/views/artist_commentaries/_secondary_links.html.erb @@ -5,5 +5,6 @@
  • <%= link_to "Listing", artist_commentaries_path %>
  • <%= link_to "Recent changes", artist_commentary_versions_path %>
  • <%= link_to "Translation requests", artist_commentaries_path(:search => {:post_tags_match => "commentary_request"}) %>
  • +
  • <%= link_to "Help", wiki_pages_path(:title => "help:artist_commentary") %>
  • <% end %> diff --git a/app/views/artist_commentaries/search.html.erb b/app/views/artist_commentaries/search.html.erb index 43ecf05f7..4e18c7e00 100644 --- a/app/views/artist_commentaries/search.html.erb +++ b/app/views/artist_commentaries/search.html.erb @@ -3,6 +3,7 @@

    Search Artist Commentary

    <%= form_tag(artist_commentaries_path, :method => :get, :class => "simple_form") do %> <%= search_field "text_matches", :label => "Text" %> + <%= search_field "post_tags_match", :label => "Tags" %>
    @@ -14,7 +15,6 @@ <%= select "search", "translated_present", ["yes", "no"], :include_blank => true %>
    - <%= search_field "post_tags_match", :label => "Tags" %> <%= submit_tag "Search" %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index e4b2805dd..8874ad087 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -193,6 +193,10 @@ Rails.application.routes.draw do resources :pool_versions, :only => [:index] resources :posts do resources :events, :only => [:index], :controller => "post_events" + resource :artist_commentary, :only => [:index, :show] do + collection { put :create_or_update } + member { put :revert } + end resources :votes, :controller => "post_votes", :only => [:create, :destroy] collection do get :home @@ -216,7 +220,7 @@ Rails.application.routes.draw do get :search end end - resources :artist_commentaries do + resources :artist_commentaries, :only => [:index, :show] do collection do put :create_or_update get :search