1
1
/**
2
- * Ajax Autocomplete for jQuery, version 1.2.5
2
+ * Ajax Autocomplete for jQuery, version 1.2.6
3
3
* (c) 2013 Tomas Kirda
4
4
*
5
5
* Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
31
31
return $ . extend ( target , source ) ;
32
32
} ,
33
33
34
- addEvent : function ( element , eventType , handler ) {
35
- if ( element . addEventListener ) {
36
- element . addEventListener ( eventType , handler , false ) ;
37
- } else if ( element . attachEvent ) {
38
- element . attachEvent ( 'on' + eventType , handler ) ;
39
- } else {
40
- throw new Error ( 'Browser doesn\'t support addEventListener or attachEvent' ) ;
41
- }
42
- } ,
43
-
44
- removeEvent : function ( element , eventType , handler ) {
45
- if ( element . removeEventListener ) {
46
- element . removeEventListener ( eventType , handler , false ) ;
47
- } else if ( element . detachEvent ) {
48
- element . detachEvent ( 'on' + eventType , handler ) ;
49
- }
50
- } ,
51
-
52
34
createNode : function ( html ) {
53
35
var div = document . createElement ( 'div' ) ;
54
36
div . innerHTML = html ;
168
150
container . appendTo ( options . appendTo ) . width ( options . width ) ;
169
151
170
152
// Listen for mouse over event on suggestions list:
171
- container . on ( 'mouseover' , suggestionSelector , function ( ) {
153
+ container . on ( 'mouseover.autocomplete ' , suggestionSelector , function ( ) {
172
154
that . activate ( $ ( this ) . data ( 'index' ) ) ;
173
155
} ) ;
174
156
175
157
// Deselect active element when mouse leaves suggestions container:
176
- container . on ( 'mouseout' , function ( ) {
158
+ container . on ( 'mouseout.autocomplete ' , function ( ) {
177
159
that . selectedIndex = - 1 ;
178
160
container . children ( '.' + selected ) . removeClass ( selected ) ;
179
161
} ) ;
180
162
181
163
// Listen for click event on suggestions list:
182
- container . on ( 'click' , suggestionSelector , function ( ) {
164
+ container . on ( 'click.autocomplete ' , suggestionSelector , function ( ) {
183
165
that . select ( $ ( this ) . data ( 'index' ) , false ) ;
184
166
} ) ;
185
167
186
168
that . fixPosition ( ) ;
187
169
188
170
// Opera does not like keydown:
189
171
if ( window . opera ) {
190
- that . el . on ( 'keypress' , function ( e ) { that . onKeyPress ( e ) ; } ) ;
172
+ that . el . on ( 'keypress.autocomplete ' , function ( e ) { that . onKeyPress ( e ) ; } ) ;
191
173
} else {
192
- that . el . on ( 'keydown' , function ( e ) { that . onKeyPress ( e ) ; } ) ;
174
+ that . el . on ( 'keydown.autocomplete ' , function ( e ) { that . onKeyPress ( e ) ; } ) ;
193
175
}
194
176
195
- that . el . on ( 'keyup' , function ( e ) { that . onKeyUp ( e ) ; } ) ;
196
- that . el . on ( 'blur' , function ( ) { that . onBlur ( ) ; } ) ;
197
- that . el . on ( 'focus' , function ( ) { that . fixPosition ( ) ; } ) ;
177
+ that . el . on ( 'keyup.autocomplete ' , function ( e ) { that . onKeyUp ( e ) ; } ) ;
178
+ that . el . on ( 'blur.autocomplete ' , function ( ) { that . onBlur ( ) ; } ) ;
179
+ that . el . on ( 'focus.autocomplete ' , function ( ) { that . fixPosition ( ) ; } ) ;
198
180
} ,
199
181
200
182
onBlur : function ( ) {
226
208
this . badQueries = [ ] ;
227
209
} ,
228
210
211
+ clear : function ( ) {
212
+ this . clearCache ( ) ;
213
+ this . currentValue = null ;
214
+ this . suggestions = [ ] ;
215
+ } ,
216
+
229
217
disable : function ( ) {
230
218
this . disabled = true ;
231
219
} ,
253
241
254
242
enableKillerFn : function ( ) {
255
243
var that = this ;
256
- $ ( document ) . on ( 'click' , that . killerFn ) ;
244
+ $ ( document ) . on ( 'click.autocomplete ' , that . killerFn ) ;
257
245
} ,
258
246
259
247
disableKillerFn : function ( ) {
260
248
var that = this ;
261
- $ ( document ) . off ( 'click' , that . killerFn ) ;
249
+ $ ( document ) . off ( 'click.autocomplete ' , that . killerFn ) ;
262
250
} ,
263
251
264
252
killSuggestions : function ( ) {
611
599
}
612
600
613
601
return currentValue . substr ( 0 , currentValue . length - parts [ parts . length - 1 ] . length ) + value ;
602
+ } ,
603
+
604
+ dispose : function ( ) {
605
+ var that = this ;
606
+ that . el . off ( '.autocomplete' ) . removeData ( 'autocomplete' ) ;
607
+ that . disableKillerFn ( ) ;
608
+ $ ( that . suggestionsContainer ) . remove ( ) ;
614
609
}
615
610
} ;
616
611
617
612
// Create chainable jQuery plugin:
618
613
$ . fn . autocomplete = function ( options , args ) {
614
+ var dataKey = 'autocomplete' ;
615
+ // If function invoked without argument return
616
+ // instance of the first matched element:
617
+ if ( arguments . length === 0 ) {
618
+ return this . first ( ) . data ( dataKey ) ;
619
+ }
620
+
619
621
return this . each ( function ( ) {
620
- var dataKey = 'autocomplete' ,
621
- inputElement = $ ( this ) ,
622
- instance ;
622
+ var inputElement = $ ( this ) ,
623
+ instance = inputElement . data ( dataKey ) ;
623
624
624
625
if ( typeof options === 'string' ) {
625
- instance = inputElement . data ( dataKey ) ;
626
- if ( typeof instance [ options ] === 'function' ) {
626
+ if ( instance && typeof instance [ options ] === 'function' ) {
627
627
instance [ options ] ( args ) ;
628
628
}
629
629
} else {
630
+ // If instance already exists, destroy it:
631
+ if ( instance && instance . dispose ) {
632
+ instance . dispose ( ) ;
633
+ }
630
634
instance = new Autocomplete ( this , options ) ;
631
635
inputElement . data ( dataKey , instance ) ;
632
636
}
633
637
} ) ;
634
638
} ;
635
- } ) ) ;
639
+ } ) ) ;
0 commit comments