forked from flaptor/indextank-jquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.indextank.autocomplete.js
82 lines (68 loc) · 3.06 KB
/
jquery.indextank.autocomplete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(function($){
if(!$.Indextank){
$.Indextank = new Object();
};
$.Indextank.Autocomplete = function(el, options){
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("Indextank.Autocomplete", base);
base.init = function(){
base.options = $.extend({},$.Indextank.Autocomplete.defaultOptions, options);
// Put your initialization code here
var ize = $(base.el.form).data("Indextank.Ize");
base.$el.autocomplete({
select: function( event, ui ) {
event.target.value = ui.item.value;
// wrap form into a jQuery object, so submit honors onsubmit.
$(event.target.form).submit();
},
source: function ( request, responseCallback ) {
$.ajax( {
url: ize.apiurl + "/v1/indexes/" + ize.indexName + "/autocomplete",
dataType: "jsonp",
data: { query: request.term, field: base.options.fieldName },
success: function( data ) { responseCallback(data.suggestions); base.$el.trigger("Indextank.Autocomplete.success", [data.suggestions]); }
} );
},
minLength: base.options.minLength,
delay: base.options.delay
});
// make sure autocomplete closes when IndextankIzed form submits
ize.$el.submit(function(e){
base.$el.data("autocomplete").close();
});
// and also disable it when Indextank.AjaxSearch is searching ..
base.$el.bind("Indextank.AjaxSearch.searching", function(e) {
// hacky way to abort a request on jquery.ui.autocomplete.
//base.$el.data("autocomplete").disable();
//window.setTimeout(function(){base.$el.data("autocomplete").enable();}, 1000);
});
};
// Sample Function, Uncomment to use
// base.functionName = function(paramaters){
//
// };
// Run initializer
base.init();
};
$.Indextank.Autocomplete.defaultOptions = {
fieldName: "text",
minLength: 2,
delay: 100
};
$.fn.indextank_Autocomplete = function(options){
return this.each(function(){
(new $.Indextank.Autocomplete(this, options));
});
};
// This function breaks the chain, but returns
// the Indextank.autocomplete if it has been attached to the object.
$.fn.getIndextank_Autocomplete = function(){
this.data("Indextank.Autocomplete");
};
})(jQuery);