autocomplete: move artist/pool/wiki autocompletion to autocomplete.js.

* Move all autocomplete code to autocomplete.js.

* Mark autocompleted fields with `data-autocomplete="<type>"` attributes,
  instead of hardcoding input field IDs in the javascript.
This commit is contained in:
evazion
2018-02-18 16:19:32 -06:00
parent a058a77c07
commit 962842815d
12 changed files with 118 additions and 137 deletions

View File

@@ -5,10 +5,6 @@
if ($("#c-artists").length) {
Danbooru.Artist.initialize_check_name();
Danbooru.Artist.initialize_shortcuts();
if (Danbooru.meta("enable-auto-complete") === "true") {
Danbooru.Artist.initialize_autocomplete();
}
}
}
@@ -43,46 +39,8 @@
});
}
};
Danbooru.Artist.initialize_autocomplete = function() {
var $fields = $("#search_name,#quick_search_name");
$fields.autocomplete({
minLength: 1,
source: function(req, resp) {
$.ajax({
url: "/artists.json",
data: {
"search[name]": req.term + "*",
"search[is_active]": true,
"search[order]": "post_count",
"limit": 10
},
method: "get",
success: function(data) {
resp($.map(data, function(artist) {
return {
label: artist.name.replace(/_/g, " "),
value: artist.name
};
}));
}
});
}
});
var render_artist = function(list, artist) {
var $link = $("<a/>").addClass("tag-type-1").text(artist.label);
return $("<li/>").data("item.autocomplete", artist).append($link).appendTo(list);
};
$fields.each(function(i, field) {
$(field).data("uiAutocomplete")._renderItem = render_artist;
});
}
})();
$(document).ready(function() {
Danbooru.Artist.initialize_all();
});

View File

@@ -13,7 +13,10 @@
if (Danbooru.meta("enable-auto-complete") === "true") {
Danbooru.Autocomplete.enable_local_storage = this.test_local_storage();
this.initialize_tag_autocomplete();
this.initialize_mention_autocomplete();
this.initialize_mention_autocomplete($(".autocomplete-mentions textarea"));
this.initialize_artist_autocomplete($('[data-autocomplete="artist"]'));
this.initialize_pool_autocomplete($('[data-autocomplete="pool"]'));
this.initialize_wiki_autocomplete($('[data-autocomplete="wiki-page"]'));
this.prune_local_storage();
}
}
@@ -43,9 +46,7 @@
}
}
Danbooru.Autocomplete.initialize_mention_autocomplete = function() {
var $fields = $(".autocomplete-mentions textarea");
Danbooru.Autocomplete.initialize_mention_autocomplete = function($fields) {
$fields.on("keydown.danbooru.autocomplete.tab", null, "tab", Danbooru.Autocomplete.on_tab);
$fields.autocomplete({
delay: 500,
@@ -194,6 +195,111 @@
});
}
Danbooru.Autocomplete.initialize_artist_autocomplete = function($fields) {
$fields.autocomplete({
minLength: 1,
source: function(req, resp) {
$.ajax({
url: "/artists.json",
data: {
"search[name]": req.term + "*",
"search[is_active]": true,
"search[order]": "post_count",
"limit": 10
},
method: "get",
success: function(data) {
resp($.map(data, function(artist) {
return {
label: artist.name.replace(/_/g, " "),
value: artist.name
};
}));
}
});
}
});
var render_artist = function(list, artist) {
var $link = $("<a/>").addClass("tag-type-1").text(artist.label);
return $("<li/>").data("item.autocomplete", artist).append($link).appendTo(list);
};
$fields.each(function(i, field) {
$(field).data("uiAutocomplete")._renderItem = render_artist;
});
};
Danbooru.Autocomplete.initialize_pool_autocomplete = function($fields) {
$fields.autocomplete({
minLength: 1,
source: function(req, resp) {
$.ajax({
url: "/pools.json",
data: {
"search[name_matches]": req.term,
"limit": 10
},
method: "get",
success: function(data) {
resp($.map(data, function(pool) {
return {
label: pool.name.replace(/_/g, " "),
value: pool.name,
category: pool.category
};
}));
}
});
}
});
var render_pool = function(list, pool) {
var $link = $("<a/>").addClass("pool-category-" + pool.category).text(pool.label);
return $("<li/>").data("item.autocomplete", pool).append($link).appendTo(list);
};
$fields.each(function(i, field) {
$(field).data("uiAutocomplete")._renderItem = render_pool;
});
};
Danbooru.Autocomplete.initialize_wiki_autocomplete = function($fields) {
$fields.autocomplete({
minLength: 1,
source: function(req, resp) {
$.ajax({
url: "/wiki_pages.json",
data: {
"search[title]": req.term + "*",
"search[hide_deleted]": "Yes",
"search[order]": "post_count",
"limit": 10
},
method: "get",
success: function(data) {
resp($.map(data, function(wiki_page) {
return {
label: wiki_page.title.replace(/_/g, " "),
value: wiki_page.title,
category: wiki_page.category_name
};
}));
}
});
}
});
var render_wiki_page = function(list, wiki_page) {
var $link = $("<a/>").addClass("tag-type-" + wiki_page.category).text(wiki_page.label);
return $("<li/>").data("item.autocomplete", wiki_page).append($link).appendTo(list);
};
$fields.each(function(i, field) {
$(field).data("uiAutocomplete")._renderItem = render_wiki_page;
});
};
Danbooru.Autocomplete.normal_source = function(term, resp) {
var key = "ac-" + term.replace(/\./g,'\uFFFF');
if (this.enable_local_storage) {

View File

@@ -4,9 +4,6 @@
Danbooru.Pool.initialize_all = function() {
if ($("#c-pools").length) {
this.initialize_shortcuts();
if (Danbooru.meta("enable-auto-complete") === "true") {
this.initialize_autocomplete_for("#search_name_matches,#quick_search_name_matches");
}
}
if ($("#c-posts").length && $("#a-show").length) {
@@ -18,47 +15,9 @@
}
}
Danbooru.Pool.initialize_autocomplete_for = function(selector) {
var $fields = $(selector);
$fields.autocomplete({
minLength: 1,
source: function(req, resp) {
$.ajax({
url: "/pools.json",
data: {
"search[name_matches]": req.term,
"limit": 10
},
method: "get",
success: function(data) {
resp($.map(data, function(pool) {
return {
label: pool.name.replace(/_/g, " "),
value: pool.name,
category: pool.category
};
}));
}
});
}
});
var render_pool = function(list, pool) {
var $link = $("<a/>").addClass("pool-category-" + pool.category).text(pool.label);
return $("<li/>").data("item.autocomplete", pool).append($link).appendTo(list);
};
$fields.each(function(i, field) {
$(field).data("uiAutocomplete")._renderItem = render_pool;
});
}
Danbooru.Pool.initialize_add_to_pool_link = function() {
$("#add-to-pool-dialog").dialog({autoOpen: false});
this.initialize_autocomplete_for("#add-to-pool-dialog input[type=text]");
$("#pool").click(function(e) {
e.preventDefault();
$("#add-to-pool-dialog").dialog("open");

View File

@@ -3,52 +3,10 @@
Danbooru.WikiPage.initialize_all = function() {
if ($("#c-wiki-pages,#c-wiki-page-versions").length) {
if (Danbooru.meta("enable-auto-complete") === "true") {
this.initialize_autocomplete();
}
this.initialize_shortcuts();
}
}
Danbooru.WikiPage.initialize_autocomplete = function() {
var $fields = $("#search_title,#quick_search_title");
$fields.autocomplete({
minLength: 1,
source: function(req, resp) {
$.ajax({
url: "/wiki_pages.json",
data: {
"search[title]": req.term + "*",
"search[hide_deleted]": "Yes",
"search[order]": "post_count",
"limit": 10
},
method: "get",
success: function(data) {
resp($.map(data, function(wiki_page) {
return {
label: wiki_page.title.replace(/_/g, " "),
value: wiki_page.title,
category: wiki_page.category_name
};
}));
}
});
}
});
var render_wiki_page = function(list, wiki_page) {
var $link = $("<a/>").addClass("tag-type-" + wiki_page.category).text(wiki_page.label);
return $("<li/>").data("item.autocomplete", wiki_page).append($link).appendTo(list);
};
$fields.each(function(i, field) {
$(field).data("uiAutocomplete")._renderItem = render_wiki_page;
});
}
Danbooru.WikiPage.initialize_shortcuts = function() {
if ($("#a-show").length) {
Danbooru.keydown("e", "edit", function(e) {

View File

@@ -5,7 +5,7 @@
<div id="search">
<%= form_tag(artist_versions_path, :method => :get, :class => "simple_form") do %>
<%= search_field "updater_name", :label => "User" %>
<%= search_field "name", :label => "Name" %>
<%= search_field "name", :label => "Name", :data => { autocomplete: "artist" } %>
<%= submit_tag "Search" %>
<% end %>
</div>

View File

@@ -1,3 +1,3 @@
<%= form_tag(artists_path, :method => :get) do %>
<%= text_field "search", "name", :id => "quick_search_name", :placeholder => "Search artists" %>
<%= text_field "search", "name", id: "quick_search_name", placeholder: "Search artists", data: { autocomplete: "artist" } %>
<% end %>

View File

@@ -1,5 +1,5 @@
<%= simple_form_for(:search, url: artists_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %>
<%= f.input :name, label: "Name", hint: "Use * for wildcard", input_html: { value: params[:search][:name] } %>
<%= f.input :name, label: "Name", hint: "Use * for wildcard", input_html: { value: params[:search][:name], data: { autocomplete: "artist" }} %>
<%= f.input :url_matches, label: "URL", as: "string", input_html: { value: params[:search][:url_matches] } %>
<%= f.input :creator_name, label: "Creator", input_html: { value: params[:search][:creator_name] } %>
<%= f.input :is_active, label: "Active?", collection: [["Yes", true], ["No", false]], include_blank: true, selected: params[:search][:is_active] %>

View File

@@ -3,7 +3,7 @@
<div class="input">
<label>Pool Name</label>
<span id="pool-name-container"><%= text_field_tag "pool_name", "", :size => 20 %></span>
<span id="pool-name-container"><%= text_field_tag "pool_name", "", :size => 20, :data => { autocomplete: "pool" } %></span>
<span id="pool-name-hint" class="hint">Search for a pool containing (<%= link_to "see full list", all_select_pool_element_path, :remote => true %>)</span>
</div>

View File

@@ -1,3 +1,3 @@
<%= form_tag(pools_path, :method => :get) do %>
<%= text_field "search", "name_matches", :id => "quick_search_name_matches", :placeholder => "Search pools" %>
<%= text_field "search", "name_matches", id: "quick_search_name_matches", placeholder: "Search pools", data: { autocomplete: "pool" } %>
<% end %>

View File

@@ -5,7 +5,7 @@
<th><label for="search_name_matches">Name</label></th>
<td>
<div class="input">
<%= text_field "search", "name_matches", :value => params[:search][:name_matches] %>
<%= text_field "search", "name_matches", :value => params[:search][:name_matches], :data => { autocomplete: "pool" } %>
</div>
</td>
</tr>

View File

@@ -1,3 +1,3 @@
<%= form_tag(wiki_pages_path, :method => :get) do %>
<%= text_field "search", "title", :id => "quick_search_title", :placeholder => "Search wiki pages" %>
<%= text_field "search", "title", id: "quick_search_title", placeholder: "Search wiki pages", data: { autocomplete: "wiki-page" } %>
<% end %>

View File

@@ -1,7 +1,7 @@
<div id="c-wiki-pages">
<div id="a-search">
<%= form_tag(wiki_pages_path, :method => :get, :class => "simple_form") do %>
<%= search_field "title", :hint => "Use * for wildcard searches" %>
<%= search_field "title", :hint => "Use * for wildcard searches", :data => { :autocomplete => "wiki-page" } %>
<%= search_field "creator_name" %>
<%= search_field "body_matches", :label => "Body" %>
<%= search_field "other_names_match", :label => "Other names", :hint => "Use * for wildcard searches" %>