@@ -24,6 +24,7 @@ interface Params {
2424 DOMResults : Element ;
2525 Request : XMLHttpRequest ;
2626 Input : Element ;
27+ Select : Element ;
2728
2829 // Workflow methods
2930 _Blur : any ;
@@ -175,6 +176,7 @@ class AutoComplete {
175176 DOMResults : document . createElement ( "div" ) ,
176177 Request : null ,
177178 Input : null ,
179+ Select : null ,
178180
179181 _EmptyMessage : function ( ) : string {
180182 console . log ( "EmptyMessage" , this ) ;
@@ -252,7 +254,7 @@ class AutoComplete {
252254 var params = this ;
253255 Array . prototype . forEach . call ( this . DOMResults . getElementsByTagName ( "li" ) , function ( li ) {
254256 li . onclick = function ( event ) {
255- params . _Select ( event . target ) ;
257+ params . _Select ( li ) ;
256258 } ;
257259 } ) ;
258260 } ,
@@ -340,11 +342,24 @@ class AutoComplete {
340342
341343 return this . Input . value ;
342344 } ,
343- _Select : function ( item ) : void {
345+ _Select : function ( item : Element ) : void {
344346 console . log ( "Select" , this ) ;
345347
346348 this . Input . value = item . getAttribute ( "data-autocomplete-value" , item . innerHTML ) ;
347349 this . Input . setAttribute ( "data-autocomplete-old-value" , this . Input . value ) ;
350+
351+ if ( this . Select !== void 0 ) {
352+ var option : Element = document . createElement ( "option" ) ;
353+ option . setAttribute ( "value" , this . Input . value ) ;
354+ option . setAttribute ( "selected" , "selected" ) ;
355+ option . innerHTML = item . innerHTML ;
356+
357+ if ( this . Select . hasChildNodes ( ) ) {
358+ this . Select . childNodes [ 0 ] . remove ( ) ;
359+ }
360+
361+ this . Select . appendChild ( option ) ;
362+ }
348363 } ,
349364 } ;
350365
@@ -369,15 +384,34 @@ class AutoComplete {
369384
370385 console . log ( "Selector" , selector ) ;
371386
372- AutoComplete . prototype . create ( AutoComplete . merge ( AutoComplete . defaults , params , {
373- Input : selector ,
374- } ) ) ;
387+ AutoComplete . prototype . create ( AutoComplete . merge ( AutoComplete . defaults , params ) , selector ) ;
375388 }
376389 }
377390
378- create ( params : Params ) : void {
391+ create ( params : Params , element : Element ) : void {
379392 console . log ( "Object" , params ) ;
380393
394+ if ( element . nodeName . match ( / ^ S E L E C T $ / i) ) {
395+ params . Select = element ;
396+
397+ params . Select . setAttribute ( "style" , "display:none;" ) ;
398+
399+ var input = document . createElement ( "input" ) ;
400+ input . setAttribute ( "type" , "search" ) ;
401+ input . setAttribute ( "autocomplete" , "off" ) ;
402+
403+ params . Select . parentNode . appendChild ( input ) ;
404+
405+ var attributes : NamedNodeMap = params . Select . attributes ;
406+ for ( var i = attributes . length - 1 ; i >= 0 ; i -- ) {
407+ if ( attributes [ i ] . name . match ( / ^ d a t a - a u t o c o m p l e t e / i) ) {
408+ input . setAttribute ( attributes [ i ] . name , attributes [ i ] . value ) ;
409+ }
410+ }
411+
412+ params . Input = input ;
413+ }
414+
381415 if ( params . Input . nodeName . match ( / ^ I N P U T $ / i) && params . Input . getAttribute ( "type" ) . match ( / ^ T E X T | S E A R C H $ / i) ) {
382416 params . Input . setAttribute ( "autocomplete" , "off" ) ;
383417 params . _Position ( params ) ;
0 commit comments