@@ -31,12 +31,22 @@ function Typeahead(o) {
31
31
this . autoselectOnBlur = ! ! o . autoselectOnBlur ;
32
32
this . openOnFocus = ! ! o . openOnFocus ;
33
33
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 : { } ) ;
34
42
this . cssClasses = o . cssClasses = _ . mixin ( { } , css . defaultClasses , o . cssClasses || { } ) ;
35
- this . $node = buildDom ( o ) ;
36
43
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 ;
40
50
41
51
if ( o . dropdownMenuContainer ) {
42
52
DOM . element ( o . dropdownMenuContainer )
@@ -64,7 +74,16 @@ function Typeahead(o) {
64
74
65
75
this . eventBus = o . eventBus || new EventBus ( { el : $input } ) ;
66
76
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
+ } )
68
87
. onSync ( 'suggestionClicked' , this . _onSuggestionClicked , this )
69
88
. onSync ( 'cursorMoved' , this . _onCursorMoved , this )
70
89
. onSync ( 'cursorRemoved' , this . _onCursorRemoved , this )
@@ -439,21 +458,21 @@ function buildDom(options) {
439
458
var $hint ;
440
459
441
460
$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 ) ;
443
462
// override the display property with the table-cell value
444
463
// if the parent element is a table and the original input was a block
445
464
// -> 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' ) {
447
466
$wrapper . css ( 'display' , 'table-cell' ) ;
448
467
}
449
468
var dropdownHtml = html . dropdown .
450
469
replace ( '%PREFIX%' , options . cssClasses . prefix ) .
451
470
replace ( '%DROPDOWN_MENU%' , options . cssClasses . dropdownMenu ) ;
452
- $dropdown = DOM . element ( dropdownHtml ) . css ( css . dropdown ) ;
471
+ $dropdown = DOM . element ( dropdownHtml ) . css ( options . css . dropdown ) ;
453
472
if ( options . templates && options . templates . dropdownMenu ) {
454
473
$dropdown . html ( _ . templatify ( options . templates . dropdownMenu ) ( ) ) ;
455
474
}
456
- $hint = $input . clone ( ) . css ( css . hint ) . css ( getBackgroundStyles ( $input ) ) ;
475
+ $hint = $input . clone ( ) . css ( options . css . hint ) . css ( getBackgroundStyles ( $input ) ) ;
457
476
458
477
$hint
459
478
. val ( '' )
@@ -477,7 +496,7 @@ function buildDom(options) {
477
496
$input
478
497
. addClass ( _ . className ( options . cssClasses . prefix , options . cssClasses . input , true ) )
479
498
. attr ( { autocomplete : 'off' , spellcheck : false } )
480
- . css ( options . hint ? css . input : css . inputWithNoHint ) ;
499
+ . css ( options . hint ? options . css . input : options . css . inputWithNoHint ) ;
481
500
482
501
// ie7 does not like it when dir is set to auto
483
502
try {
@@ -488,11 +507,20 @@ function buildDom(options) {
488
507
// ignore
489
508
}
490
509
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
494
515
. prepend ( options . hint ? $hint : null )
495
516
. append ( $dropdown ) ;
517
+
518
+ return {
519
+ wrapper : $wrapper ,
520
+ input : $input ,
521
+ hint : $hint ,
522
+ menu : $dropdown
523
+ } ;
496
524
}
497
525
498
526
function getBackgroundStyles ( $el ) {
0 commit comments