Merge branch 'localstorage-autocomplete'
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
//= require jquery.hotkeys.js
|
//= require jquery.hotkeys.js
|
||||||
//= require jquery.timeout.js
|
//= require jquery.timeout.js
|
||||||
//= require jquery-ui-autocomplete-custom.js
|
//= require jquery-ui-autocomplete-custom.js
|
||||||
|
//= require jquery.storageapi.js
|
||||||
//= require rails.js
|
//= require rails.js
|
||||||
//= require common.js
|
//= require common.js
|
||||||
//= require_self
|
//= require_self
|
||||||
|
|||||||
@@ -4,6 +4,13 @@
|
|||||||
Danbooru.Autocomplete.initialize_all = function() {
|
Danbooru.Autocomplete.initialize_all = function() {
|
||||||
if (Danbooru.meta("enable-auto-complete") === "true") {
|
if (Danbooru.meta("enable-auto-complete") === "true") {
|
||||||
this.initialize_tag_autocomplete();
|
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({
|
$fields_single.autocomplete({
|
||||||
minLength: 1,
|
minLength: 1,
|
||||||
source: function(req, resp) {
|
source: Danbooru.Autocomplete.normal_source
|
||||||
$.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
|
|
||||||
};
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$.merge($fields_multiple, $fields_single).each(function(i, field) {
|
$.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) {
|
Danbooru.Autocomplete.render_item = function(list, item) {
|
||||||
var $link = $("<a/>").text(item.label);
|
var $link = $("<a/>").text(item.label);
|
||||||
$link.attr("href", "/posts?tags=" + encodeURIComponent(item.value));
|
$link.attr("href", "/posts?tags=" + encodeURIComponent(item.value));
|
||||||
@@ -192,29 +216,6 @@
|
|||||||
return $("<li/>").data("item.autocomplete", item).append($link).appendTo(list);
|
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 = {
|
Danbooru.Autocomplete.static_metatags = {
|
||||||
order: [
|
order: [
|
||||||
"id", "id_desc",
|
"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