work on related tag js
This commit is contained in:
96
app/assets/javascripts/related_tag.js
Normal file
96
app/assets/javascripts/related_tag.js
Normal file
@@ -0,0 +1,96 @@
|
||||
(function() {
|
||||
Danbooru.RelatedTag = {};
|
||||
|
||||
Danbooru.RelatedTag.initialize_all = function() {
|
||||
this.initialize_buttons();
|
||||
}
|
||||
|
||||
Danbooru.RelatedTag.initialize_buttons = function() {
|
||||
this.common_bind("#related-tags-button", "");
|
||||
this.common_bind("#related-artists-button", "artist");
|
||||
this.common_bind("#related-characters-button", "character");
|
||||
this.common_bind("#related-copyrights-button", "copyright");
|
||||
}
|
||||
|
||||
Danbooru.RelatedTag.common_bind = function(button_name, category) {
|
||||
$(button_name).click(function(e) {
|
||||
$.get("/related_tag.json", {
|
||||
"query": Danbooru.RelatedTag.current_tag(),
|
||||
"category": category
|
||||
}).success(Danbooru.RelatedTag.process_response);
|
||||
e.preventDefault();
|
||||
});
|
||||
}
|
||||
|
||||
Danbooru.RelatedTag.current_tag = function() {
|
||||
var $field = $("#upload_tag_string,#post_tag_string");
|
||||
var string = $field.val();
|
||||
var text_length = string.length;
|
||||
var a = $field[0].selectionStart;
|
||||
var b = $field[0].selectionStart;
|
||||
|
||||
while ((a > 0) && string[a] != " ") {
|
||||
a -= 1;
|
||||
}
|
||||
|
||||
if (string[a] == " ") {
|
||||
a += 1;
|
||||
}
|
||||
|
||||
while ((b < text_length) && string[b] != " ") {
|
||||
b += 1;
|
||||
}
|
||||
|
||||
return string.slice(a, b);
|
||||
}
|
||||
|
||||
Danbooru.RelatedTag.process_response = function(data) {
|
||||
var query = data.query;
|
||||
var related_tags = data.tags;
|
||||
var wiki_page_tags = data.wiki_page_tags;
|
||||
var $dest = $("#related-tags");
|
||||
|
||||
$dest.append(Danbooru.RelatedTag.build_html(query, related_tags));
|
||||
if (wiki_page_tags.length > 0) {
|
||||
$dest.append(Danbooru.RelatedTag.build_html("wiki:" + query), wiki_page_tags);
|
||||
}
|
||||
}
|
||||
|
||||
Danbooru.RelatedTag.build_html = function(query, related_tags) {
|
||||
if (query === null || query === "") {
|
||||
return "";
|
||||
}
|
||||
|
||||
var current = $("#upload_tag_string,#post_tag_string").val().match(/\S+/g) || [];
|
||||
var $div = $("<div/>").addClass("tag-column");
|
||||
var $ul = $("<ul/>");
|
||||
$ul.append(
|
||||
$("<li/>").append(
|
||||
$("<em/>").html(
|
||||
query.replace(/_/g, " ")
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$.each(related_tags, function(i, tag) {
|
||||
var $link = $("<a/>");
|
||||
$link.html(tag[0].replace(/_/g, " "));
|
||||
$link.addClass("tag-type-" + tag[1]);
|
||||
$link.attr("href", "/posts?tags=" + encodeURIComponent(tag[0]));
|
||||
$link.click(Danbooru.RelatedTag.toggle_tag);
|
||||
if ($.inArray(tag, current)) {
|
||||
$link.addClass("active");
|
||||
}
|
||||
$ul.append(
|
||||
$("<li/>").append($link)
|
||||
);
|
||||
});
|
||||
|
||||
$div.append($ul);
|
||||
return $div;
|
||||
}
|
||||
})();
|
||||
|
||||
$(function() {
|
||||
Danbooru.RelatedTag.initialize_all();
|
||||
});
|
||||
12
app/controllers/related_tags_controller.rb
Normal file
12
app/controllers/related_tags_controller.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
class RelatedTagsController < ApplicationController
|
||||
respond_to :json
|
||||
|
||||
def show
|
||||
@query = RelatedTagQuery.new(params[:query], params[:category])
|
||||
respond_with(@query) do |format|
|
||||
format.json do
|
||||
render :json => @query.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
2
app/helpers/related_tags_helper.rb
Normal file
2
app/helpers/related_tags_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module RelatedTagsHelper
|
||||
end
|
||||
@@ -20,8 +20,16 @@ class RelatedTagQuery
|
||||
wiki_page.try(:tags) || []
|
||||
end
|
||||
|
||||
def to_json
|
||||
{:query => query, :category => category, :tags => map_with_category_data(tags), :wiki_page_tags => map_with_category_data(wiki_page_tags)}.to_json
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def map_with_category_data(list_of_tag_names)
|
||||
Tag.categories_for(list_of_tag_names).to_a
|
||||
end
|
||||
|
||||
def pattern_matching_tags
|
||||
Tag.name_matches(query).order("post_count desc").limit(50).sort_by {|x| x.name}.map(&:name)
|
||||
end
|
||||
|
||||
@@ -38,9 +38,18 @@
|
||||
</div>
|
||||
|
||||
<div class="input">
|
||||
<%= f.label :tag_string, "Tags" %>
|
||||
<%= f.text_area :tag_string, :size => "60x4" %>
|
||||
<div>
|
||||
<%= f.label :tag_string, "Tags" %>
|
||||
<%= f.text_area :tag_string, :size => "60x4" %>
|
||||
</div>
|
||||
|
||||
<%= button_tag "Related tags", :id => "related-tags-button" %>
|
||||
<%= button_tag "Artists", :id => "related-artists-button" %>
|
||||
<%= button_tag "Characters", :id => "related-characters-button" %>
|
||||
<%= button_tag "Copyrights", :id => "related-copyrights-button" %>
|
||||
</div>
|
||||
|
||||
<div id="related-tags"></div>
|
||||
|
||||
<div class="input">
|
||||
<%= submit_tag "Submit" %>
|
||||
|
||||
@@ -33,6 +33,7 @@ Danbooru::Application.routes.draw do
|
||||
resources :hits, :controller => "advertisement_hits", :only => [:create]
|
||||
end
|
||||
resource :source, :only => [:show]
|
||||
resource :related_tag, :only => [:show]
|
||||
resources :artists do
|
||||
member do
|
||||
put :revert
|
||||
|
||||
7
test/functional/related_tags_controller_test.rb
Normal file
7
test/functional/related_tags_controller_test.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class RelatedTagsControllerTest < ActionController::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
@@ -24,6 +24,10 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
|
||||
should "work" do
|
||||
assert_equal(["aaa", "bbb", "ccc"], @query.tags)
|
||||
end
|
||||
|
||||
should "render the json" do
|
||||
assert_equal("{\"query\":\"aaa\",\"category\":\"\",\"tags\":[[\"aaa\",0],[\"bbb\",0],[\"ccc\",0]],\"wiki_page_tags\":[]}", @query.to_json)
|
||||
end
|
||||
end
|
||||
|
||||
context "for a tag that doesn't exist" do
|
||||
|
||||
Reference in New Issue
Block a user