Merge branch 'localstorage-autocomplete'

This commit is contained in:
r888888888
2014-02-12 15:20:43 -08:00
3 changed files with 49 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

@@ -4,6 +4,13 @@
Danbooru.Autocomplete.initialize_all = function() {
if (Danbooru.meta("enable-auto-complete") === "true") {
this.initialize_tag_autocomplete();
this.prune_local_storage();
}
}
Danbooru.Autocomplete.prune_local_storage = function() {
if ($.localStorage.keys().length > 10000) {
$.localStorage.removeAll();
}
}
@@ -130,28 +137,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 +145,44 @@
});
}
Danbooru.Autocomplete.normal_source = function(term, resp) {
var key = "ac-" + term;
var cached = $.localStorage.get(key);
if (cached) {
if (cached.expires < new Date()) {
$.localStorage.remove(key);
} else {
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);
$.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 +216,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