cache autocomplete results

This commit is contained in:
r888888888
2014-02-11 15:56:06 -08:00
parent cc9b5a0c61
commit 06b92f9546
3 changed files with 46 additions and 45 deletions

View File

@@ -3,6 +3,7 @@
//= require jquery.hotkeys.js
//= require jquery.timeout.js
//= require jquery-ui-autocomplete-custom.js
//= require jquery.storageapi.js
//= require rails.js
//= require common.js
//= require_self

View File

@@ -130,28 +130,7 @@
$fields_single.autocomplete({
minLength: 1,
source: function(req, resp) {
$.ajax({
url: "/tags.json",
data: {
"search[order]": "count",
"search[name_matches]": req.term + "*",
"limit": 10
},
method: "get",
success: function(data) {
resp($.map(data, function(tag) {
return {
type: "tag",
label: tag.name.replace(/_/g, " "),
value: tag.name,
category: tag.category,
post_count: tag.post_count
};
}));
}
});
}
source: Danbooru.Autocomplete.normal_source
});
$.merge($fields_multiple, $fields_single).each(function(i, field) {
@@ -159,6 +138,48 @@
});
}
Danbooru.Autocomplete.normal_source = function(term, resp) {
var key = "ac-" + term;
var cached = $.localStorage.get(key);
if (cached) {
if (cached.expires < new Date()) {
console.log("localStorage: removing " + key);
$.localStorage.remove(key);
} else {
console.log("localStorage: reading " + key);
resp(cached.value);
return;
}
}
$.ajax({
url: "/tags.json",
data: {
"search[order]": "count",
"search[name_matches]": term + "*",
"limit": 10
},
method: "get",
success: function(data) {
var data = $.map(data, function(tag) {
return {
type: "tag",
label: tag.name.replace(/_/g, " "),
value: tag.name,
category: tag.category,
post_count: tag.post_count
};
});
var expiry = new Date();
expiry.setDate(expiry.getDate() + 7);
console.log("localStorage: setting " + key);
console.log("localStorage: expires at " + expiry);
$.localStorage.set(key, {"value": data, "expires": expiry});
resp(data);
}
});
}
Danbooru.Autocomplete.render_item = function(list, item) {
var $link = $("<a/>").text(item.label);
$link.attr("href", "/posts?tags=" + encodeURIComponent(item.value));
@@ -192,29 +213,6 @@
return $("<li/>").data("item.autocomplete", item).append($link).appendTo(list);
};
Danbooru.Autocomplete.normal_source = function(term, resp) {
$.ajax({
url: "/tags.json",
data: {
"search[order]": "count",
"search[name_matches]": term + "*",
"limit": 10
},
method: "get",
success: function(data) {
resp($.map(data, function(tag) {
return {
type: "tag",
label: tag.name.replace(/_/g, " "),
value: tag.name,
category: tag.category,
post_count: tag.post_count
};
}));
}
});
}
Danbooru.Autocomplete.static_metatags = {
order: [
"id", "id_desc",

File diff suppressed because one or more lines are too long