11/**
2- * Ajax Autocomplete for jQuery, version 1.2.5
2+ * Ajax Autocomplete for jQuery, version 1.2.6
33* (c) 2013 Tomas Kirda
44*
55* Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
3131 return $ . extend ( target , source ) ;
3232 } ,
3333
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-
5234 createNode : function ( html ) {
5335 var div = document . createElement ( 'div' ) ;
5436 div . innerHTML = html ;
168150 container . appendTo ( options . appendTo ) . width ( options . width ) ;
169151
170152 // Listen for mouse over event on suggestions list:
171- container . on ( 'mouseover' , suggestionSelector , function ( ) {
153+ container . on ( 'mouseover.autocomplete ' , suggestionSelector , function ( ) {
172154 that . activate ( $ ( this ) . data ( 'index' ) ) ;
173155 } ) ;
174156
175157 // Deselect active element when mouse leaves suggestions container:
176- container . on ( 'mouseout' , function ( ) {
158+ container . on ( 'mouseout.autocomplete ' , function ( ) {
177159 that . selectedIndex = - 1 ;
178160 container . children ( '.' + selected ) . removeClass ( selected ) ;
179161 } ) ;
180162
181163 // Listen for click event on suggestions list:
182- container . on ( 'click' , suggestionSelector , function ( ) {
164+ container . on ( 'click.autocomplete ' , suggestionSelector , function ( ) {
183165 that . select ( $ ( this ) . data ( 'index' ) , false ) ;
184166 } ) ;
185167
186168 that . fixPosition ( ) ;
187169
188170 // Opera does not like keydown:
189171 if ( window . opera ) {
190- that . el . on ( 'keypress' , function ( e ) { that . onKeyPress ( e ) ; } ) ;
172+ that . el . on ( 'keypress.autocomplete ' , function ( e ) { that . onKeyPress ( e ) ; } ) ;
191173 } else {
192- that . el . on ( 'keydown' , function ( e ) { that . onKeyPress ( e ) ; } ) ;
174+ that . el . on ( 'keydown.autocomplete ' , function ( e ) { that . onKeyPress ( e ) ; } ) ;
193175 }
194176
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 ( ) ; } ) ;
198180 } ,
199181
200182 onBlur : function ( ) {
226208 this . badQueries = [ ] ;
227209 } ,
228210
211+ clear : function ( ) {
212+ this . clearCache ( ) ;
213+ this . currentValue = null ;
214+ this . suggestions = [ ] ;
215+ } ,
216+
229217 disable : function ( ) {
230218 this . disabled = true ;
231219 } ,
253241
254242 enableKillerFn : function ( ) {
255243 var that = this ;
256- $ ( document ) . on ( 'click' , that . killerFn ) ;
244+ $ ( document ) . on ( 'click.autocomplete ' , that . killerFn ) ;
257245 } ,
258246
259247 disableKillerFn : function ( ) {
260248 var that = this ;
261- $ ( document ) . off ( 'click' , that . killerFn ) ;
249+ $ ( document ) . off ( 'click.autocomplete ' , that . killerFn ) ;
262250 } ,
263251
264252 killSuggestions : function ( ) {
611599 }
612600
613601 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 ( ) ;
614609 }
615610 } ;
616611
617612 // Create chainable jQuery plugin:
618613 $ . 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+
619621 return this . each ( function ( ) {
620- var dataKey = 'autocomplete' ,
621- inputElement = $ ( this ) ,
622- instance ;
622+ var inputElement = $ ( this ) ,
623+ instance = inputElement . data ( dataKey ) ;
623624
624625 if ( typeof options === 'string' ) {
625- instance = inputElement . data ( dataKey ) ;
626- if ( typeof instance [ options ] === 'function' ) {
626+ if ( instance && typeof instance [ options ] === 'function' ) {
627627 instance [ options ] ( args ) ;
628628 }
629629 } else {
630+ // If instance already exists, destroy it:
631+ if ( instance && instance . dispose ) {
632+ instance . dispose ( ) ;
633+ }
630634 instance = new Autocomplete ( this , options ) ;
631635 inputElement . data ( dataKey , instance ) ;
632636 }
633637 } ) ;
634638 } ;
635- } ) ) ;
639+ } ) ) ;
0 commit comments