@@ -31,12 +31,22 @@ function Typeahead(o) {
3131 this . autoselectOnBlur = ! ! o . autoselectOnBlur ;
3232 this . openOnFocus = ! ! o . openOnFocus ;
3333 this . minLength = _ . isNumber ( o . minLength ) ? o . minLength : 1 ;
34+
35+ o . hint = ! ! o . hint ;
36+
37+ if ( o . hint && o . appendTo ) {
38+ throw new Error ( '[autocomplete.js] hint and appendTo options can\'t be used at the same time' ) ;
39+ }
40+
41+ this . css = o . css = _ . mixin ( { } , css , o . appendTo ? css . appendTo : { } ) ;
3442 this . cssClasses = o . cssClasses = _ . mixin ( { } , css . defaultClasses , o . cssClasses || { } ) ;
35- this . $node = buildDom ( o ) ;
3643
37- $menu = this . $node . find ( _ . className ( this . cssClasses . prefix , this . cssClasses . dropdownMenu ) ) ;
38- $input = this . $node . find ( _ . className ( this . cssClasses . prefix , this . cssClasses . input ) ) ;
39- $hint = this . $node . find ( _ . className ( this . cssClasses . prefix , this . cssClasses . hint ) ) ;
44+ var domElts = buildDom ( o ) ;
45+
46+ this . $node = domElts . wrapper ;
47+ $menu = domElts . menu ;
48+ $input = domElts . input ;
49+ $hint = domElts . hint ;
4050
4151 if ( o . dropdownMenuContainer ) {
4252 DOM . element ( o . dropdownMenuContainer )
@@ -64,7 +74,16 @@ function Typeahead(o) {
6474
6575 this . eventBus = o . eventBus || new EventBus ( { el : $input } ) ;
6676
67- this . dropdown = new Typeahead . Dropdown ( { menu : $menu , datasets : o . datasets , templates : o . templates , cssClasses : this . cssClasses , minLength : this . minLength } )
77+ this . dropdown = new Typeahead . Dropdown ( {
78+ input : $input ,
79+ appendTo : o . appendTo ,
80+ wrapper : this . $node ,
81+ menu : $menu ,
82+ datasets : o . datasets ,
83+ templates : o . templates ,
84+ cssClasses : o . cssClasses ,
85+ minLength : this . minLength
86+ } )
6887 . onSync ( 'suggestionClicked' , this . _onSuggestionClicked , this )
6988 . onSync ( 'cursorMoved' , this . _onCursorMoved , this )
7089 . onSync ( 'cursorRemoved' , this . _onCursorRemoved , this )
@@ -439,21 +458,21 @@ function buildDom(options) {
439458 var $hint ;
440459
441460 $input = DOM . element ( options . input ) ;
442- $wrapper = DOM . element ( html . wrapper . replace ( '%ROOT%' , options . cssClasses . root ) ) . css ( css . wrapper ) ;
461+ $wrapper = DOM . element ( html . wrapper . replace ( '%ROOT%' , options . cssClasses . root ) ) . css ( options . css . wrapper ) ;
443462 // override the display property with the table-cell value
444463 // if the parent element is a table and the original input was a block
445464 // -> https://github.com/algolia/autocomplete.js/issues/16
446- if ( $input . css ( 'display' ) === 'block' && $input . parent ( ) . css ( 'display' ) === 'table' ) {
465+ if ( ! options . appendTo && $input . css ( 'display' ) === 'block' && $input . parent ( ) . css ( 'display' ) === 'table' ) {
447466 $wrapper . css ( 'display' , 'table-cell' ) ;
448467 }
449468 var dropdownHtml = html . dropdown .
450469 replace ( '%PREFIX%' , options . cssClasses . prefix ) .
451470 replace ( '%DROPDOWN_MENU%' , options . cssClasses . dropdownMenu ) ;
452- $dropdown = DOM . element ( dropdownHtml ) . css ( css . dropdown ) ;
471+ $dropdown = DOM . element ( dropdownHtml ) . css ( options . css . dropdown ) ;
453472 if ( options . templates && options . templates . dropdownMenu ) {
454473 $dropdown . html ( _ . templatify ( options . templates . dropdownMenu ) ( ) ) ;
455474 }
456- $hint = $input . clone ( ) . css ( css . hint ) . css ( getBackgroundStyles ( $input ) ) ;
475+ $hint = $input . clone ( ) . css ( options . css . hint ) . css ( getBackgroundStyles ( $input ) ) ;
457476
458477 $hint
459478 . val ( '' )
@@ -477,7 +496,7 @@ function buildDom(options) {
477496 $input
478497 . addClass ( _ . className ( options . cssClasses . prefix , options . cssClasses . input , true ) )
479498 . attr ( { autocomplete : 'off' , spellcheck : false } )
480- . css ( options . hint ? css . input : css . inputWithNoHint ) ;
499+ . css ( options . hint ? options . css . input : options . css . inputWithNoHint ) ;
481500
482501 // ie7 does not like it when dir is set to auto
483502 try {
@@ -488,11 +507,20 @@ function buildDom(options) {
488507 // ignore
489508 }
490509
491- return $input
492- . wrap ( $wrapper )
493- . parent ( )
510+ $wrapper = options . appendTo
511+ ? $wrapper . appendTo ( DOM . element ( options . appendTo ) . eq ( 0 ) ) . eq ( 0 )
512+ : $input . wrap ( $wrapper ) . parent ( ) ;
513+
514+ $wrapper
494515 . prepend ( options . hint ? $hint : null )
495516 . append ( $dropdown ) ;
517+
518+ return {
519+ wrapper : $wrapper ,
520+ input : $input ,
521+ hint : $hint ,
522+ menu : $dropdown
523+ } ;
496524}
497525
498526function getBackgroundStyles ( $el ) {
0 commit comments