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 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

@@ -550,21 +550,14 @@
$("#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() {
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);