related tags: refactor /related_tags.js to avoid updating unchanged columns.
Refactor the /related_tags.js call so that when the "Related tags" button is clicked, it only replaces the columns that actually changed, not the entire related tags section. This avoids rebuilding the Recent / Frequent / Translated Tags / Artist columns every time the "Related tags" button is clicked.
This commit is contained in:
@@ -3,7 +3,7 @@ class RelatedTagsController < ApplicationController
|
|||||||
before_action :require_reportbooru_key, only: [:update]
|
before_action :require_reportbooru_key, only: [:update]
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@query = RelatedTagQuery.new(params[:query], category: params[:category], translated_tags: params[:translated_tags], artists: params[:artists], user: CurrentUser.user)
|
@query = RelatedTagQuery.new(query: params[:query], category: params[:category], user: CurrentUser.user)
|
||||||
respond_with(@query) do |format|
|
respond_with(@query) do |format|
|
||||||
format.json do
|
format.json do
|
||||||
render :json => @query.to_json
|
render :json => @query.to_json
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ import Utility from './utility';
|
|||||||
let RelatedTag = {};
|
let RelatedTag = {};
|
||||||
|
|
||||||
RelatedTag.initialize_all = function() {
|
RelatedTag.initialize_all = function() {
|
||||||
RelatedTag.artists = [];
|
|
||||||
RelatedTag.translated_tags = [];
|
|
||||||
$(document).on("click.danbooru", ".related-tags-button", RelatedTag.on_click_related_tags_button);
|
$(document).on("click.danbooru", ".related-tags-button", RelatedTag.on_click_related_tags_button);
|
||||||
$(document).on("click.danbooru", ".related-tags a.search-tag", RelatedTag.toggle_tag);
|
$(document).on("click.danbooru", ".related-tags a.search-tag", RelatedTag.toggle_tag);
|
||||||
$(document).on("click.danbooru", "#show-related-tags-link, #hide-related-tags-link", RelatedTag.toggle);
|
$(document).on("click.danbooru", "#show-related-tags-link, #hide-related-tags-link", RelatedTag.toggle);
|
||||||
@@ -20,15 +18,6 @@ RelatedTag.tags_include = function(name) {
|
|||||||
return $.inArray(name.toLowerCase(), current) > -1;
|
return $.inArray(name.toLowerCase(), current) > -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
RelatedTag.update_related_tags = function (category = "") {
|
|
||||||
$.get("/related_tag.js", {
|
|
||||||
"query": RelatedTag.current_tag(),
|
|
||||||
"category": category,
|
|
||||||
"translated_tags": RelatedTag.translated_tags.join(" "),
|
|
||||||
"artists": RelatedTag.artists.join(" "),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
RelatedTag.on_open_post_edit_dialog = function(event) {
|
RelatedTag.on_open_post_edit_dialog = function(event) {
|
||||||
$("#related-tags-container").removeClass("visible").addClass("hidden");
|
$("#related-tags-container").removeClass("visible").addClass("hidden");
|
||||||
}
|
}
|
||||||
@@ -38,20 +27,18 @@ RelatedTag.on_close_post_edit_dialog = function(event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RelatedTag.on_show_post_edit_form = function(event) {
|
RelatedTag.on_show_post_edit_form = function(event) {
|
||||||
RelatedTag.update_related_tags();
|
$.get("/related_tag.js", { user_tags: true });
|
||||||
$("#fetch-data-manual").click();
|
$("#fetch-data-manual").click();
|
||||||
}
|
}
|
||||||
|
|
||||||
RelatedTag.on_click_related_tags_button = function (event) {
|
RelatedTag.on_click_related_tags_button = function (event) {
|
||||||
const category = $(event.target).data("category");
|
$.get("/related_tag.js", { query: RelatedTag.current_tag(), category: $(event.target).data("category") });
|
||||||
RelatedTag.update_related_tags(category);
|
RelatedTag.show();
|
||||||
$("#related-tags-container").removeClass("hidden").addClass("visible");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RelatedTag.on_update_source_data = function (event, source) {
|
RelatedTag.on_update_source_data = function (event, { source, related_tags_html }) {
|
||||||
Danbooru.RelatedTag.artists = source.artists.map(artist => artist.name);
|
$(".source-related-tags-columns").replaceWith(related_tags_html);
|
||||||
Danbooru.RelatedTag.translated_tags = source.translated_tags.map(tag => tag[0]);
|
RelatedTag.update_selected();
|
||||||
RelatedTag.update_related_tags();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RelatedTag.current_tag = function() {
|
RelatedTag.current_tag = function() {
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
class RelatedTagQuery
|
class RelatedTagQuery
|
||||||
attr_reader :query, :category, :translated_tags, :artists, :user
|
attr_reader :query, :category, :user
|
||||||
|
|
||||||
def initialize(query, category: nil, translated_tags: nil, artists: nil, user: nil)
|
def initialize(query: nil, category: nil, user: nil)
|
||||||
@user = user
|
@user = user
|
||||||
@query = TagAlias.to_aliased(query.to_s.downcase.strip).join(" ")
|
@query = TagAlias.to_aliased(query.to_s.downcase.strip).join(" ")
|
||||||
@category = category
|
@category = category
|
||||||
@translated_tags = translated_tags.to_s.split
|
|
||||||
@artists = Artist.where(name: artists.to_s.split)
|
|
||||||
@artists = [Artist.find_by(name: "banned_artist")] + @artists if @artists.any?(&:is_banned?)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def pretty_name
|
def pretty_name
|
||||||
@@ -31,7 +28,7 @@ class RelatedTagQuery
|
|||||||
return [] unless user.present? && PostArchive.enabled?
|
return [] unless user.present? && PostArchive.enabled?
|
||||||
|
|
||||||
versions = PostArchive.where(updater_id: user.id).where("updated_at > ?", since).order(id: :desc).limit(max_edits)
|
versions = PostArchive.where(updater_id: user.id).where("updated_at > ?", since).order(id: :desc).limit(max_edits)
|
||||||
tags = versions.flat_map { |v| v.diff[:added_tags] }
|
tags = versions.flat_map(&:added_tags)
|
||||||
tags = tags.reject { |tag| Tag.is_metatag?(tag) }
|
tags = tags.reject { |tag| Tag.is_metatag?(tag) }
|
||||||
tags = tags.group_by(&:itself).transform_values(&:size).sort_by { |tag, count| [-count, tag] }.map(&:first)
|
tags = tags.group_by(&:itself).transform_values(&:size).sort_by { |tag, count| [-count, tag] }.map(&:first)
|
||||||
tags.take(max_tags)
|
tags.take(max_tags)
|
||||||
|
|||||||
@@ -89,5 +89,5 @@
|
|||||||
<%= submit_tag "Submit", :class => "ui-button ui-widget ui-corner-all" %>
|
<%= submit_tag "Submit", :class => "ui-button ui-widget ui-corner-all" %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%= render "related_tags/container" %>
|
<%= render "related_tags/container", related_tags: nil %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -5,5 +5,9 @@
|
|||||||
<a href="#" id="hide-related-tags-link">«</a>
|
<a href="#" id="hide-related-tags-link">«</a>
|
||||||
</h3>
|
</h3>
|
||||||
|
|
||||||
<%= render "related_tags/related_tags" %>
|
<div id="related-tags" class="related-tags">
|
||||||
|
<%= render "related_tags/user_tags", related_tags: nil %>
|
||||||
|
<%= render "related_tags/current_tags", related_tags: nil %>
|
||||||
|
<%= render "related_tags/source_tags", source: nil %>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
10
app/views/related_tags/_current_tags.html.erb
Normal file
10
app/views/related_tags/_current_tags.html.erb
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<div class="current-related-tags-columns">
|
||||||
|
<% if related_tags.present? %>
|
||||||
|
<%= render "related_tags/tag_column", tags: related_tags.tags, class: "general-related-tags-column", title: related_tags.pretty_name %>
|
||||||
|
<%= render "related_tags/tag_column", tags: related_tags.wiki_page_tags, class: "wiki-related-tags-column", title: link_to("wiki:#{related_tags.pretty_name}", show_or_new_wiki_pages_path(title: related_tags.query)) %>
|
||||||
|
|
||||||
|
<% related_tags.other_wiki_category_tags.each do |wiki| %>
|
||||||
|
<%= render "related_tags/tag_column", tags: wiki.tags, class: "other-wiki-related-tags-column", title: link_to("wiki:#{wiki.pretty_title}", show_or_new_wiki_pages_path(title: wiki.title)) %>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
<%# related_tags %>
|
|
||||||
|
|
||||||
<div id="related-tags" class="related-tags">
|
|
||||||
<% if local_assigns[:related_tags] %>
|
|
||||||
<%= render "tag_column", tags: related_tags.recent_tags, title: "Recent" %>
|
|
||||||
<%= render "tag_column", tags: related_tags.favorite_tags, title: "Frequent" %>
|
|
||||||
<%= render "tag_column", tags: related_tags.tags, class: "general-related-tags-column", title: related_tags.pretty_name %>
|
|
||||||
<%= render "tag_column", tags: related_tags.translated_tags, title: "Translated Tags" %>
|
|
||||||
<%= render "tag_column", tags: related_tags.wiki_page_tags, class: "wiki-related-tags-column", title: link_to("wiki:#{related_tags.pretty_name}", show_or_new_wiki_pages_path(title: related_tags.query)) %>
|
|
||||||
|
|
||||||
<% related_tags.other_wiki_category_tags.each do |wiki| %>
|
|
||||||
<%= render "tag_column", tags: wiki.tags, class: "other-wiki-related-tags-column", title: link_to("wiki:#{wiki.pretty_title}", show_or_new_wiki_pages_path(title: wiki.title)) %>
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
<div class="tag-column wide-column artist-related-tags-column">
|
|
||||||
<h6>Artist</h6>
|
|
||||||
|
|
||||||
<% if related_tags.artists.size == 0 %>
|
|
||||||
none
|
|
||||||
<% else %>
|
|
||||||
<% related_tags.artists.each do |artist| %>
|
|
||||||
<%= TagSetPresenter.new([artist.name]).tag_list_html(name_only: true) %>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<% artist.sorted_urls.each do |url| %>
|
|
||||||
<li>
|
|
||||||
<% if url.is_active %>
|
|
||||||
<%= external_link_to url.url, truncate: 30 %>
|
|
||||||
<%= link_to tag.i(class: "fas fa-minus-circle"), artist_url_path(id: url.id, artist_url: { is_active: false }), remote: true, method: :put, "data-confirm": "This will mark the URL as inactive. Continue?" %>
|
|
||||||
<% else %>
|
|
||||||
<del><%= truncate(url.url, length: 30) %></del>
|
|
||||||
<% end %>
|
|
||||||
</li>
|
|
||||||
<% end %>
|
|
||||||
</ul>
|
|
||||||
<% end %>
|
|
||||||
<% end %>
|
|
||||||
</div>
|
|
||||||
<% end %>
|
|
||||||
</div>
|
|
||||||
34
app/views/related_tags/_source_tags.html.erb
Normal file
34
app/views/related_tags/_source_tags.html.erb
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<div class="source-related-tags-columns">
|
||||||
|
<% if source.present? %>
|
||||||
|
<%= render "related_tags/tag_column", tags: source.translated_tags.map(&:first), title: "Translated Tags" %>
|
||||||
|
|
||||||
|
<div class="tag-column wide-column artist-related-tags-column">
|
||||||
|
<h6>Artist</h6>
|
||||||
|
|
||||||
|
<% if source.artists.any?(&:is_banned?) %>
|
||||||
|
<%= TagSetPresenter.new(["banned_artist"]).tag_list_html(name_only: true) %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<% if source.artists.size == 0 %>
|
||||||
|
none
|
||||||
|
<% else %>
|
||||||
|
<% source.artists.each do |artist| %>
|
||||||
|
<%= TagSetPresenter.new([artist.name]).tag_list_html(name_only: true) %>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<% artist.sorted_urls.each do |url| %>
|
||||||
|
<li>
|
||||||
|
<% if url.is_active %>
|
||||||
|
<%= external_link_to url.url, truncate: 30 %>
|
||||||
|
<%= link_to tag.i(class: "fas fa-minus-circle"), artist_url_path(id: url.id, artist_url: { is_active: false }), remote: true, method: :put, "data-confirm": "This will mark the URL as inactive. Continue?" %>
|
||||||
|
<% else %>
|
||||||
|
<del><%= truncate(url.url, length: 30) %></del>
|
||||||
|
<% end %>
|
||||||
|
</li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<%# tags, title, class (optional) %>
|
<%# tags, title, class (optional) %>
|
||||||
|
|
||||||
<% if tags.present? %>
|
<div class="tag-column <%= local_assigns[:class] || "#{title.parameterize}-related-tags-column" %>">
|
||||||
<div class="tag-column <%= local_assigns[:class] || "#{title.parameterize}-related-tags-column" %>">
|
<% if tags.present? %>
|
||||||
<h6><%= title %></h6>
|
<h6><%= title %></h6>
|
||||||
<%= TagSetPresenter.new(tags).tag_list_html(name_only: true) %>
|
<%= TagSetPresenter.new(tags).tag_list_html(name_only: true) %>
|
||||||
</div>
|
<% end %>
|
||||||
<% end %>
|
</div>
|
||||||
|
|||||||
6
app/views/related_tags/_user_tags.html.erb
Normal file
6
app/views/related_tags/_user_tags.html.erb
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<div class="user-related-tags-columns">
|
||||||
|
<% if related_tags.present? %>
|
||||||
|
<%= render "related_tags/tag_column", tags: related_tags.recent_tags, title: "Recent" %>
|
||||||
|
<%= render "related_tags/tag_column", tags: related_tags.favorite_tags, title: "Frequent" %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
@@ -1,2 +1,7 @@
|
|||||||
$("div#related-tags").replaceWith("<%= j render "related_tags", related_tags: @query %>");
|
<% if params[:user_tags] %>
|
||||||
|
$(".user-related-tags-columns").replaceWith("<%= j render "user_tags", related_tags: @query %>");
|
||||||
|
<% else %>
|
||||||
|
$(".current-related-tags-columns").replaceWith("<%= j render "current_tags", related_tags: @query %>");
|
||||||
|
<% end %>
|
||||||
|
|
||||||
Danbooru.RelatedTag.update_selected();
|
Danbooru.RelatedTag.update_selected();
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
$("#source-info").replaceWith("<%= j render "info", source: @source %>");
|
$("#source-info").replaceWith("<%= j render "info", source: @source %>");
|
||||||
|
|
||||||
$(document).trigger("danbooru:update-source-data", <%= raw @source.to_json %>);
|
$(document).trigger("danbooru:update-source-data", {
|
||||||
|
source: <%= raw @source.to_json %>,
|
||||||
|
related_tags_html: "<%= j render "related_tags/source_tags", source: @source %>",
|
||||||
|
});
|
||||||
|
|
||||||
if ($("#c-uploads #a-new").length) {
|
if ($("#c-uploads #a-new").length) {
|
||||||
$("#upload_artist_commentary_title").val(<%= raw @source.dtext_artist_commentary_title.to_json %>);
|
$("#upload_artist_commentary_title").val(<%= raw @source.dtext_artist_commentary_title.to_json %>);
|
||||||
|
|||||||
@@ -104,7 +104,7 @@
|
|||||||
<%= submit_tag "Submit", :id => "submit-button", :class => "large ui-button ui-widget ui-corner-all gradient", data: {disable_with: false} %>
|
<%= submit_tag "Submit", :id => "submit-button", :class => "large ui-button ui-widget ui-corner-all gradient", data: {disable_with: false} %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%= render "related_tags/container" %>
|
<%= render "related_tags/container", related_tags: nil %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<h2 style="margin-bottom: 1em;">You <%= CurrentUser.user.upload_limited_reason %></h2>
|
<h2 style="margin-bottom: 1em;">You <%= CurrentUser.user.upload_limited_reason %></h2>
|
||||||
|
|||||||
Reference in New Issue
Block a user