Fix #3214: Add autocomplete for saved searches.

* Autocomplete the `search:<label>` metatag.

* Make label autocompletion do a prefix match instead of a substring
  match. Example: `search:ar` matches `artists`, not `characters`. This
  is how tags and most other things are autocompleted.
This commit is contained in:
evazion
2017-07-11 19:17:22 -05:00
parent dc079d7932
commit 51d0feb2c6
3 changed files with 37 additions and 18 deletions

View File

@@ -93,7 +93,7 @@
var prefixes = "-|~|general:|gen:|artist:|art:|copyright:|copy:|co:|character:|char:|ch:"; var prefixes = "-|~|general:|gen:|artist:|art:|copyright:|copy:|co:|character:|char:|ch:";
var metatags = "order|-status|status|-rating|rating|-locked|locked|child|filetype|-filetype|" + var metatags = "order|-status|status|-rating|rating|-locked|locked|child|filetype|-filetype|" +
"-user|user|-approver|approver|commenter|comm|noter|noteupdater|artcomm|-fav|fav|ordfav|" + "-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({ $fields_multiple.autocomplete({
delay: 100, delay: 100,
@@ -183,6 +183,10 @@
case "-favgroup": case "-favgroup":
Danbooru.Autocomplete.favorite_group_source(term, resp, metatag); Danbooru.Autocomplete.favorite_group_source(term, resp, metatag);
break; break;
case "search":
case "-search":
Danbooru.Autocomplete.saved_search_source(term, resp);
break;
default: default:
Danbooru.Autocomplete.normal_source(term, resp); Danbooru.Autocomplete.normal_source(term, resp);
break; 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() { $(document).ready(function() {

View File

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

View File

@@ -1,5 +1,16 @@
$(document).ready(function() { Danbooru.SavedSearch = {};
Danbooru.SavedSearch.initialize_all = function() {
if ($("#c-saved-searches".length)) { if ($("#c-saved-searches".length)) {
Danbooru.sorttable($("#c-saved-searches table")); 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);