add check for localstorage implementation in autocomplete

This commit is contained in:
r888888888
2014-02-13 11:36:33 -08:00
parent a285089f7a
commit 2abfcd9063

View File

@@ -3,13 +3,24 @@
Danbooru.Autocomplete.initialize_all = function() { Danbooru.Autocomplete.initialize_all = function() {
if (Danbooru.meta("enable-auto-complete") === "true") { if (Danbooru.meta("enable-auto-complete") === "true") {
Danbooru.Autocomplete.enable_local_storage = this.test_local_storage();
this.initialize_tag_autocomplete(); this.initialize_tag_autocomplete();
this.prune_local_storage(); this.prune_local_storage();
} }
} }
Danbooru.Autocomplete.test_local_storage = function() {
try {
localStorage.setItem("test", "test");
localStorage.removeItem("test");
return true;
} catch(e) {
return false;
}
}
Danbooru.Autocomplete.prune_local_storage = function() { Danbooru.Autocomplete.prune_local_storage = function() {
if ($.localStorage.keys().length > 10000) { if (this.enable_local_storage && $.localStorage.keys().length > 10000) {
$.localStorage.removeAll(); $.localStorage.removeAll();
} }
} }
@@ -149,14 +160,16 @@
Danbooru.Autocomplete.normal_source = function(term, resp) { Danbooru.Autocomplete.normal_source = function(term, resp) {
var key = "ac-" + term; var key = "ac-" + term;
var cached = $.localStorage.get(key); if (this.enable_local_storage) {
if (cached) { var cached = $.localStorage.get(key);
if (cached.expires < new Date()) { if (cached) {
$.localStorage.remove(key); if (cached.expires < new Date()) {
} else { $.localStorage.remove(key);
resp(cached.value); } else {
return; resp(cached.value);
} return;
}
}
} }
$.ajax({ $.ajax({
@@ -177,9 +190,12 @@
post_count: tag.post_count post_count: tag.post_count
}; };
}); });
var expiry = new Date();
expiry.setDate(expiry.getDate() + 7); if (Danbooru.Autocomplete.enable_local_storage) {
$.localStorage.set(key, {"value": data, "expires": expiry}); var expiry = new Date();
expiry.setDate(expiry.getDate() + 7);
$.localStorage.set(key, {"value": data, "expires": expiry});
}
resp(data); resp(data);
} }
}); });