1
1
import {
2
- ComponentRef , Directive , ElementRef , EventEmitter , HostListener , Input , OnInit , Output ,
3
- ReflectiveInjector , Renderer , TemplateRef , ViewContainerRef
2
+ ComponentRef , Directive , ElementRef , EventEmitter , HostListener , Input ,
3
+ OnInit , Output ,
4
+ ReflectiveInjector , Renderer , TemplateRef , ViewContainerRef , OnDestroy
4
5
} from '@angular/core' ;
5
6
import { FormControl , NgControl } from '@angular/forms' ;
6
7
@@ -19,6 +20,7 @@ import 'rxjs/add/operator/toArray';
19
20
20
21
import { ComponentsHelper } from '../utils/components-helper.service' ;
21
22
import { TypeaheadMatch } from './typeahead-match.class' ;
23
+ import { ComponentLoaderFactory , ComponentLoader } from '../component-loader' ;
22
24
23
25
/* tslint:disable-next-line */
24
26
const KeyboardEvent = ( global as any ) . KeyboardEvent as KeyboardEvent ;
@@ -28,7 +30,7 @@ const KeyboardEvent = (global as any).KeyboardEvent as KeyboardEvent;
28
30
selector : '[typeahead][ngModel],[typeahead][formControlName]'
29
31
/* tslint:enable */
30
32
} )
31
- export class TypeaheadDirective implements OnInit {
33
+ export class TypeaheadDirective implements OnInit , OnDestroy {
32
34
@Output ( ) public typeaheadLoading :EventEmitter < boolean > = new EventEmitter < boolean > ( false ) ;
33
35
@Output ( ) public typeaheadNoResults :EventEmitter < boolean > = new EventEmitter < boolean > ( false ) ;
34
36
@Output ( ) public typeaheadOnSelect :EventEmitter < TypeaheadMatch > = new EventEmitter < TypeaheadMatch > ( false ) ;
@@ -46,32 +48,39 @@ export class TypeaheadDirective implements OnInit {
46
48
@Input ( ) public typeaheadPhraseDelimiters :string = '\'"' ;
47
49
@Input ( ) public typeaheadItemTemplate :TemplateRef < any > ;
48
50
51
+ /**
52
+ * A selector specifying the element the typeahead should be appended to.
53
+ * Currently only supports "body".
54
+ */
55
+ @Input ( ) public container : string ;
56
+
49
57
// not yet implemented
50
- // @Input () protected typeaheadAppendToBody:boolean;
51
58
// @Input () protected typeaheadEditable:boolean;
52
59
// @Input () protected typeaheadFocusFirst:boolean;
53
60
// @Input () protected typeaheadInputFormatter:any;
54
61
// @Input () protected typeaheadSelectOnExact:boolean;
55
62
// @Input () protected typeaheadSelectOnBlur:boolean;
56
63
// @Input () protected typeaheadFocusOnSelect:boolean;
57
64
58
- public container :TypeaheadContainerComponent ;
65
+ public _container :TypeaheadContainerComponent ;
59
66
public isTypeaheadOptionsListActive :boolean = false ;
60
67
61
68
protected keyUpEventEmitter :EventEmitter < any > = new EventEmitter ( ) ;
62
69
protected _matches :TypeaheadMatch [ ] ;
63
70
protected placement :string = 'bottom-left' ;
64
- protected popup :ComponentRef < TypeaheadContainerComponent > ;
71
+ // protected popup:ComponentRef<TypeaheadContainerComponent>;
65
72
66
73
protected ngControl :NgControl ;
67
74
protected viewContainerRef :ViewContainerRef ;
68
75
protected element :ElementRef ;
69
76
protected renderer :Renderer ;
70
77
protected componentsHelper :ComponentsHelper ;
71
78
79
+ private _typeahead : ComponentLoader < TypeaheadContainerComponent > ;
80
+
72
81
@HostListener ( 'keyup' , [ '$event' ] )
73
82
public onChange ( e :any ) :void {
74
- if ( this . container ) {
83
+ if ( this . _container ) {
75
84
// esc
76
85
if ( e . keyCode === 27 ) {
77
86
this . hide ( ) ;
@@ -80,19 +89,19 @@ export class TypeaheadDirective implements OnInit {
80
89
81
90
// up
82
91
if ( e . keyCode === 38 ) {
83
- this . container . prevActiveMatch ( ) ;
92
+ this . _container . prevActiveMatch ( ) ;
84
93
return ;
85
94
}
86
95
87
96
// down
88
97
if ( e . keyCode === 40 ) {
89
- this . container . nextActiveMatch ( ) ;
98
+ this . _container . nextActiveMatch ( ) ;
90
99
return ;
91
100
}
92
101
93
102
// enter
94
103
if ( e . keyCode === 13 ) {
95
- this . container . selectActiveMatch ( ) ;
104
+ this . _container . selectActiveMatch ( ) ;
96
105
return ;
97
106
}
98
107
}
@@ -120,15 +129,15 @@ export class TypeaheadDirective implements OnInit {
120
129
121
130
@HostListener ( 'blur' )
122
131
public onBlur ( ) :void {
123
- if ( this . container && ! this . container . isFocused ) {
132
+ if ( this . _container && ! this . _container . isFocused ) {
124
133
this . hide ( ) ;
125
134
}
126
135
}
127
136
128
137
@HostListener ( 'keydown' , [ '$event' ] )
129
138
public onKeydown ( e :KeyboardEvent ) :void {
130
139
// no container - no problems
131
- if ( ! this . container ) {
140
+ if ( ! this . _container ) {
132
141
return ;
133
142
}
134
143
@@ -146,12 +155,14 @@ export class TypeaheadDirective implements OnInit {
146
155
}
147
156
148
157
public constructor ( control :NgControl , viewContainerRef :ViewContainerRef , element :ElementRef ,
149
- renderer :Renderer , componentsHelper :ComponentsHelper ) {
158
+ renderer :Renderer , componentsHelper :ComponentsHelper , cis : ComponentLoaderFactory ) {
150
159
this . element = element ;
151
160
this . ngControl = control ;
152
161
this . viewContainerRef = viewContainerRef ;
153
162
this . renderer = renderer ;
154
- this . componentsHelper = componentsHelper ;
163
+ // this.componentsHelper = componentsHelper;
164
+ this . _typeahead = cis
165
+ . createLoader < TypeaheadContainerComponent > ( element , viewContainerRef , renderer ) ;
155
166
}
156
167
157
168
public ngOnInit ( ) :void {
@@ -187,39 +198,59 @@ export class TypeaheadDirective implements OnInit {
187
198
}
188
199
189
200
public show ( ) :void {
190
- let options = new TypeaheadOptions ( {
191
- typeaheadRef : this ,
192
- placement : this . placement ,
193
- animation : false
194
- } ) ;
195
-
196
- let binding = ReflectiveInjector . resolve ( [
197
- { provide : TypeaheadOptions , useValue : options }
198
- ] ) ;
199
-
200
- this . popup = this . componentsHelper
201
- . appendNextToLocation ( TypeaheadContainerComponent , this . viewContainerRef , binding ) ;
202
-
203
- this . popup . instance . position ( this . viewContainerRef . element ) ;
204
- this . container = this . popup . instance ;
205
- this . container . parent = this ;
201
+ this . _typeahead
202
+ . attach ( TypeaheadContainerComponent )
203
+ // todo: add append to body, after updating positioning service
204
+ // .to(this.container)
205
+ // .position({attachment: 'bottom left'})
206
+ . show ( null , {
207
+ typeaheadRef : this ,
208
+ placement : this . placement ,
209
+ animation : false
210
+ } ) ;
211
+
212
+ // let options = new TypeaheadOptions({
213
+ // typeaheadRef: this,
214
+ // placement: this.placement,
215
+ // animation: false
216
+ // });
217
+ //
218
+ // let binding = ReflectiveInjector.resolve([
219
+ // {provide: TypeaheadOptions, useValue: options}
220
+ // ]);
221
+ //
222
+ // this.popup = this.componentsHelper
223
+ // .appendNextToLocation(TypeaheadContainerComponent, this.viewContainerRef, binding);
224
+
225
+ // this.popup.instance.position(this.viewContainerRef.element);
226
+ // this._container = this.popup.instance;
227
+ this . _container = this . _typeahead . instance ;
228
+ this . _container . parent = this ;
206
229
// This improves the speed as it won't have to be done for each list item
207
230
let normalizedQuery = ( this . typeaheadLatinize
208
231
? TypeaheadUtils . latinize ( this . ngControl . control . value )
209
232
: this . ngControl . control . value ) . toString ( )
210
233
. toLowerCase ( ) ;
211
- this . container . query = this . typeaheadSingleWords
234
+ this . _container . query = this . typeaheadSingleWords
212
235
? TypeaheadUtils . tokenize ( normalizedQuery , this . typeaheadWordDelimiters , this . typeaheadPhraseDelimiters )
213
236
: normalizedQuery ;
214
- this . container . matches = this . _matches ;
237
+ this . _container . matches = this . _matches ;
215
238
this . element . nativeElement . focus ( ) ;
216
239
}
217
240
218
241
public hide ( ) :void {
219
- if ( this . container ) {
220
- this . popup . destroy ( ) ;
221
- this . container = void 0 ;
242
+ if ( this . _typeahead . isShown ) {
243
+ this . _typeahead . hide ( ) ;
244
+ this . _container = null ;
222
245
}
246
+ // if (this._container) {
247
+ // this.popup.destroy();
248
+ // this._container = void 0;
249
+ // }
250
+ }
251
+
252
+ public ngOnDestroy ( ) :any {
253
+ this . _typeahead . dispose ( ) ;
223
254
}
224
255
225
256
protected asyncActions ( ) :void {
@@ -305,16 +336,16 @@ export class TypeaheadDirective implements OnInit {
305
336
return ;
306
337
}
307
338
308
- if ( this . container ) {
339
+ if ( this . _container ) {
309
340
// This improves the speed as it won't have to be done for each list item
310
341
let normalizedQuery = ( this . typeaheadLatinize
311
342
? TypeaheadUtils . latinize ( this . ngControl . control . value )
312
343
: this . ngControl . control . value ) . toString ( )
313
344
. toLowerCase ( ) ;
314
- this . container . query = this . typeaheadSingleWords
345
+ this . _container . query = this . typeaheadSingleWords
315
346
? TypeaheadUtils . tokenize ( normalizedQuery , this . typeaheadWordDelimiters , this . typeaheadPhraseDelimiters )
316
347
: normalizedQuery ;
317
- this . container . matches = this . _matches ;
348
+ this . _container . matches = this . _matches ;
318
349
} else {
319
350
this . show ( ) ;
320
351
}
0 commit comments