cache autocomplete results
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
2
vendor/assets/javascripts/jquery.storageapi.js
vendored
Normal file
2
vendor/assets/javascripts/jquery.storageapi.js
vendored
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user