Merge pull request #3215 from evazion/fix-ss-autocomplete

Fix #3214: Add autocomplete for saved searches.
This commit is contained in:
Albert Yi
2017-07-11 17:45:11 -07:00
committed by GitHub
5 changed files with 52 additions and 25 deletions

View File

@@ -93,7 +93,7 @@
var prefixes = "-|~|general:|gen:|artist:|art:|copyright:|copy:|co:|character:|char:|ch:";
var metatags = "order|-status|status|-rating|rating|-locked|locked|child|filetype|-filetype|" +
"-user|user|-approver|approver|commenter|comm|noter|noteupdater|artcomm|-fav|fav|ordfav|" +
"sub|-pool|pool|ordpool|favgroup";
"sub|-pool|pool|ordpool|favgroup|-search|search";
$fields_multiple.autocomplete({
delay: 100,
@@ -183,6 +183,10 @@
case "-favgroup":
Danbooru.Autocomplete.favorite_group_source(term, resp, metatag);
break;
case "search":
case "-search":
Danbooru.Autocomplete.saved_search_source(term, resp);
break;
default:
Danbooru.Autocomplete.normal_source(term, resp);
break;
@@ -420,6 +424,17 @@
}
});
}
Danbooru.Autocomplete.saved_search_source = function(term, resp) {
return Danbooru.SavedSearch.labels(term).success(function(labels) {
resp(labels.map(function(label) {
return {
label: label.replace(/_/g, " "),
value: "search:" + label,
};
}));
});
}
})();
$(document).ready(function() {

View File

@@ -548,23 +548,15 @@
Danbooru.Post.initialize_saved_searches = function() {
$("#saved_search_labels").autocomplete({
minLength: 2,
source: function(req, resp) {
$.ajax({
url: "/saved_searches/labels.json",
data: {
label: req.term
},
method: "get",
success: function(data) {
resp($.map(data, function(saved_search) {
return {
label: saved_search.replace(/_/g, " "),
value: saved_search
};
}));
}
})
Danbooru.SavedSearch.labels(req.term).success(function(labels) {
resp(labels.map(function(label) {
return {
label: label.replace(/_/g, " "),
value: label
};
}));
});
}
});

View File

@@ -1,5 +1,16 @@
$(document).ready(function() {
if ($("#c-saved-searches".length)) {
Danbooru.SavedSearch = {};
Danbooru.SavedSearch.initialize_all = function() {
if ($("#c-saved-searches").length) {
Danbooru.sorttable($("#c-saved-searches table"));
}
});
}
Danbooru.SavedSearch.labels = function(term) {
return $.getJSON("/saved_searches/labels", {
"search[label]": term + "*",
"limit": 10
});
}
$(Danbooru.SavedSearch.initialize_all);

View File

@@ -17,11 +17,7 @@ class SavedSearchesController < ApplicationController
end
def labels
@labels = SavedSearch.labels_for(CurrentUser.user.id)
if params[:label]
regexp = Regexp.compile(Regexp.escape(params[:label]))
@labels = @labels.grep(regexp)
end
@labels = SavedSearch.search_labels(CurrentUser.id, params[:search])
respond_with(@labels)
end

View File

@@ -62,6 +62,19 @@ class SavedSearch < ApplicationRecord
label.to_s.strip.downcase.gsub(/[[:space:]]/, "_")
end
def self.search_labels(user_id, params)
labels = labels_for(user_id)
if params[:label].present?
query = Regexp.escape(params[:label]).gsub("\\*", ".*")
query = ".*#{query}.*" unless query.include?("*")
query = /\A#{query}\z/
labels = labels.grep(query)
end
labels
end
def self.labels_for(user_id)
Cache.get(cache_key(user_id)) do
SavedSearch.where(user_id: user_id).order("label").pluck("distinct unnest(labels) as label")