diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 3d689de9f..67202dab6 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -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 diff --git a/app/assets/javascripts/autocomplete.js b/app/assets/javascripts/autocomplete.js index 11d98aa18..2c6253f44 100644 --- a/app/assets/javascripts/autocomplete.js +++ b/app/assets/javascripts/autocomplete.js @@ -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 = $("").text(item.label); $link.attr("href", "/posts?tags=" + encodeURIComponent(item.value)); @@ -192,29 +213,6 @@ return $("
").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", diff --git a/vendor/assets/javascripts/jquery.storageapi.js b/vendor/assets/javascripts/jquery.storageapi.js new file mode 100644 index 000000000..d23b72bda --- /dev/null +++ b/vendor/assets/javascripts/jquery.storageapi.js @@ -0,0 +1,2 @@ +/* jQuery Storage API Plugin 1.7.0 https://github.com/julien-maurel/jQuery-Storage-API */ +!function(e){function t(t){var r,n,i,o=arguments.length,s=window[t],a=arguments,u=a[1];if(2>o)throw Error("Minimum 2 arguments must be given");if(e.isArray(u)){n={};for(var f in u){r=u[f];try{n[r]=JSON.parse(s.getItem(r))}catch(c){n[r]=s.getItem(r)}}return n}if(2!=o){try{n=JSON.parse(s.getItem(u))}catch(c){throw new ReferenceError(u+" is not defined in this storage")}for(var f=2;o-1>f;f++)if(n=n[a[f]],void 0===n)throw new ReferenceError([].slice.call(a,1,f+1).join(".")+" is not defined in this storage");if(e.isArray(a[f])){i=n,n={};for(var m in a[f])n[a[f][m]]=i[a[f][m]];return n}return n[a[f]]}try{return JSON.parse(s.getItem(u))}catch(c){return s.getItem(u)}}function n(t){var r,n,i=arguments.length,o=window[t],s=arguments,a=s[1],u=s[2],f={};if(2>i||!e.isPlainObject(a)&&3>i)throw Error("Minimum 3 arguments must be given or second parameter must be an object");if(e.isPlainObject(a)){for(var c in a)r=a[c],e.isPlainObject(r)?o.setItem(c,JSON.stringify(r)):o.setItem(c,r);return a}if(3==i)return"object"==typeof u?o.setItem(a,JSON.stringify(u)):o.setItem(a,u),u;try{n=o.getItem(a),null!=n&&(f=JSON.parse(n))}catch(m){}n=f;for(var c=2;i-2>c;c++)r=s[c],n[r]&&e.isPlainObject(n[r])||(n[r]={}),n=n[r];return n[s[c]]=s[c+1],o.setItem(a,JSON.stringify(f)),f}function i(t){var r,n,i=arguments.length,o=window[t],s=arguments,a=s[1];if(2>i)throw Error("Minimum 2 arguments must be given");if(e.isArray(a)){for(var u in a)o.removeItem(a[u]);return!0}if(2==i)return o.removeItem(a),!0;try{r=n=JSON.parse(o.getItem(a))}catch(f){throw new ReferenceError(a+" is not defined in this storage")}for(var u=2;i-1>u;u++)if(n=n[s[u]],void 0===n)throw new ReferenceError([].slice.call(s,1,u).join(".")+" is not defined in this storage");if(e.isArray(s[u]))for(var c in s[u])delete n[s[u][c]];else delete n[s[u]];return o.setItem(a,JSON.stringify(r)),!0}function o(t,r){var n=u(t);for(var o in n)i(t,n[o]);if(r)for(var o in e.namespaceStorages)f(o)}function s(r){var n=arguments.length,i=arguments,o=(window[r],i[1]);if(1==n)return 0==u(r).length;if(e.isArray(o)){for(var a=0;a