Files
Prowlarr/src/UI/Mixins/AutoComplete.js
T
Keivan Beigi 428a1439e5 rjs -> webpack
2015-02-02 17:43:14 -08:00

49 lines
1.7 KiB
JavaScript

var $ = require('jquery');
require('typeahead');
module.exports = (function(){
$.fn.autoComplete = function(options){
if(!options) {
throw 'options are required';
}
if(!options.resource) {
throw 'resource is required';
}
if(!options.query) {
throw 'query is required';
}
$(this).typeahead({
hint : true,
highlight : true,
minLength : 3,
items : 20
}, {
name : options.resource.replace('/'),
displayKey : '',
source : function(filter, callback){
var data = {};
data[options.query] = filter;
$.ajax({
url : window.NzbDrone.ApiRoot + options.resource,
dataType : 'json',
type : 'GET',
data : data,
success : function(response){
if(options.filter) {
options.filter.call(this, filter, response, callback);
}
else {
var matches = [];
$.each(response, function(i, d){
if(d[options.query] && d[options.property].startsWith(filter)) {
matches.push({value : d[options.property]});
}
});
callback(matches);
}
}
});
}
});
};
}).call(this);