From 487fe9fb5dbcaa36b802ef515c5d8818d471f843 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 14 Nov 2017 23:07:01 -0600 Subject: [PATCH] Fetch source data: list Danbooru artist tag; elim duplicated code. * In the `Fetch source data` box, list the Danbooru artist tag(s) that were found beside the artist name. * Unify `Fetch source data` code that was duplicated between the uploads page and the post show page. --- app/assets/javascripts/uploads.js | 55 ++++++++++++-------- app/assets/stylesheets/specific/posts.scss | 39 ++++++++------ app/assets/stylesheets/specific/uploads.scss | 17 ------ app/views/posts/partials/show/_edit.html.erb | 2 +- app/views/sources/_info.html.erb | 14 ++--- app/views/sources/_info_for_post.html.erb | 15 ------ 6 files changed, 60 insertions(+), 82 deletions(-) delete mode 100644 app/views/sources/_info_for_post.html.erb diff --git a/app/assets/javascripts/uploads.js b/app/assets/javascripts/uploads.js index 673c9c56e..cf8a6bee1 100644 --- a/app/assets/javascripts/uploads.js +++ b/app/assets/javascripts/uploads.js @@ -59,56 +59,65 @@ } Danbooru.Upload.initialize_info_bookmarklet = function() { - $("#source-info ul").hide(); - $("#fetch-data-bookmarklet").click(function(e) { - var xhr = $.get(e.target.href); - xhr.success(Danbooru.Upload.fill_source_info); - xhr.fail(function(data) { - $("#source-info span#loading-data").html("Error: " + data.responseJSON["message"]) - }); - e.preventDefault(); - }); - $("#fetch-data-bookmarklet").trigger("click"); + $("#fetch-data-manual").click(); } Danbooru.Upload.initialize_info_manual = function() { - $("#source-info ul").hide(); - $("#fetch-data-manual").click(function(e) { var source = $("#upload_source,#post_source").val(); + var referer = $("#upload_referer_url").val(); + if (!/\S/.test(source)) { Danbooru.error("Error: You must enter a URL into the source field to get its data"); } else if (!/^https?:\/\//.test(source)) { Danbooru.error("Error: Source is not a URL"); } else { $("#source-info span#loading-data").show(); - var xhr = $.get("/source.json?url=" + encodeURIComponent(source)); - xhr.success(Danbooru.Upload.fill_source_info); - xhr.fail(function(data) { - $("#source-info span#loading-data").html("Error: " + data.responseJSON["message"]) - }); + Danbooru.Upload.fetch_source_data(source, referer); } + e.preventDefault(); }); } + Danbooru.Upload.fetch_source_data = function(url, referer_url) { + var xhr = $.getJSON("/source.json", { url: url, ref: referer_url }); + + xhr.success(Danbooru.Upload.fill_source_info); + xhr.fail(function(data) { + $("#source-info span#loading-data").html("Error: " + data.responseJSON["message"]) + }); + + return xhr; + } + Danbooru.Upload.fill_source_info = function(data) { $("#source-tags").empty(); $.each(data.tags, function(i, v) { $("").attr("href", v[1]).text(v[0]).appendTo("#source-tags"); }); - $("#source-artist").html($("").attr("href", data.profile_url).text(data.artist_name)); + $("#source-artist-profile").attr("href", data.profile_url).text(data.artist_name); Danbooru.RelatedTag.translated_tags = data.translated_tags; Danbooru.RelatedTag.build_all(); - var new_artist_href = "/artists/new?other_names=" - + encodeURIComponent(data.artist_name) - + "&urls=" - + encodeURIComponent($.unique([data.profile_url, data.normalized_for_artist_finder_url]).join("\n")); + if (data.artists.length === 0) { + var new_artist_params = $.param({ + name: data.unique_id, + other_names: data.artist_name, + urls: $.unique([data.profile_url, data.normalized_for_artist_finder_url]).join("\n") + }); - $("#source-record").html($("").attr("href", new_artist_href).text("Create New")); + var link = $("").attr("href", "/artists/new?" + new_artist_params).text("Create new artist"); + $("#source-danbooru-artists").html(link); + } else { + var artistLinks = data.artists.map(function (artist) { + return $('').attr("href", "/artists/" + artist.id).text(artist.name); + }); + + $("#source-danbooru-artists").html(artistLinks) + } if (data.image_urls.length > 1) { $("#gallery-warning").show(); diff --git a/app/assets/stylesheets/specific/posts.scss b/app/assets/stylesheets/specific/posts.scss index 4c39fe95d..3620122b2 100644 --- a/app/assets/stylesheets/specific/posts.scss +++ b/app/assets/stylesheets/specific/posts.scss @@ -494,22 +494,6 @@ div#c-posts { color: gray; } } - - div#source-info { - margin: 1em 0; - padding: 1em; - border: 1px solid gray; - - p { - margin: 0; - } - - ul { - a { - margin-right: 1em; - } - } - } } div#quick-edit-div { @@ -529,6 +513,29 @@ div#c-post-versions, div#c-artist-versions { } } +div#c-posts, div#c-uploads { + /* Fetch source data box */ + div#source-info { + margin: 1em 0; + padding: 1em; + border: 1px solid gray; + + p { + margin: 0; + } + + > ul { + display: none; + + .source-tags { + a { + margin-right: 1em; + } + } + } + } +} + div#c-explore-posts { a.desc { font-weight: bold; diff --git a/app/assets/stylesheets/specific/uploads.scss b/app/assets/stylesheets/specific/uploads.scss index 4bca51a6b..de987fd53 100644 --- a/app/assets/stylesheets/specific/uploads.scss +++ b/app/assets/stylesheets/specific/uploads.scss @@ -26,23 +26,6 @@ div#c-uploads { display: block; } - div#source-info { - margin: 1em 0; - padding: 1em; - border: 1px solid gray; - min-height: 5em; - - p { - margin: 0; - } - - ul { - a { - margin-right: 1em; - } - } - } - div.field_with_errors { display: inline; } diff --git a/app/views/posts/partials/show/_edit.html.erb b/app/views/posts/partials/show/_edit.html.erb index 08db57ab6..d5b8d7747 100644 --- a/app/views/posts/partials/show/_edit.html.erb +++ b/app/views/posts/partials/show/_edit.html.erb @@ -4,7 +4,7 @@ <% end %> -<%= render "sources/info_for_post" %> +<%= render "sources/info" %> <%= form_for(post, :html => {:class => "simple_form", :id => "form"}) do |f| %> <%= hidden_field_tag :tags_query, params[:tags] %> diff --git a/app/views/sources/_info.html.erb b/app/views/sources/_info.html.erb index ddb7cd87e..3fab37564 100644 --- a/app/views/sources/_info.html.erb +++ b/app/views/sources/_info.html.erb @@ -3,19 +3,13 @@ -->
- <% if source.try(:available?) %> -

<%= link_to "Fetch source data", source_path(:format => "json", :url => source.referer_url), :id => "fetch-data-bookmarklet", :style => "display: none;" %>

-

<%= content_tag "span", "Loading #{source.site_name} data...", :id => "loading-data" %>

- <% else %> -

<%= link_to "Fetch source data", source_path(:format => "json"), :id => "fetch-data-manual" %>

-

<%= content_tag "span", "Loading source data...", :id => "loading-data", :style => "display: none;" %>

- <% end %> +

<%= link_to "Fetch source data", source_path(:format => "json"), :id => "fetch-data-manual" %>

+

<%= content_tag "span", "Loading source data...", :id => "loading-data", :style => "display: none;" %>

diff --git a/app/views/sources/_info_for_post.html.erb b/app/views/sources/_info_for_post.html.erb deleted file mode 100644 index 329377697..000000000 --- a/app/views/sources/_info_for_post.html.erb +++ /dev/null @@ -1,15 +0,0 @@ - - -
-

<%= link_to "Fetch source data", source_path(:format => "json"), :id => "fetch-data-manual" %>

-

<%= content_tag "span", "Loading source data...", :id => "loading-data", :style => "display: none;" %>

-

<%= content_tag "span", "Gallery. Tags may not apply to all images.", :id => "gallery-warning", :style => "display: none;" %>

- - -