Merge pull request #826 from pafcu/cache_autocomplete

Cache autocomplete results locally (the one in the editor)
This commit is contained in:
RedMatrix 2015-01-07 08:45:47 +11:00
commit 2417125c89

View File

@ -3,7 +3,18 @@
* *
* require jQuery, jquery.textcomplete * require jQuery, jquery.textcomplete
*/ */
function mysearch(term, callback, backend_url, extra_channels) { function mysearch(term, callback, backend_url, extra_channels) {
// Check if there is a cached result that contains the tsame information we would get with a full server-side search
for(t in mysearch.cache) {
if(term.indexOf(t) >= 0) { // A more broad search has been performed already, so use those results
// Filter old results locally
var matching = mysearch.cache[t].filter(function (x) { return (x.name.indexOf(term) >= 0 || x.nick.indexOf(term) >= 0); });
callback(matching);
return;
}
}
var postdata = { var postdata = {
start:0, start:0,
count:100, count:100,
@ -20,10 +31,16 @@ function mysearch(term, callback, backend_url, extra_channels) {
data: postdata, data: postdata,
dataType: 'json', dataType: 'json',
success:function(data){ success:function(data){
// Cache results if we got them all (more information would not improve results)
// data.count represents the maximum number of items
if(data.items.length < data.count) {
mysearch.cache[term] = data.items;
}
callback(data.items); callback(data.items);
}, },
}).fail(function () {callback([]); }); // Callback must be invoked even if something went wrong. }).fail(function () {callback([]); }); // Callback must be invoked even if something went wrong.
} }
mysearch.cache = {};
function format(item) { function format(item) {
return "<div class='{0}' title='{4}'><img src='{1}'>{2} ({3})</div>".format(item.taggable, item.photo, item.name, ((item.label) ? item.nick + ' ' + item.label : item.nick), item.link ) return "<div class='{0}' title='{4}'><img src='{1}'>{2} ({3})</div>".format(item.taggable, item.photo, item.name, ((item.label) ? item.nick + ' ' + item.label : item.nick), item.link )