-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
typeahead.directive.ts
650 lines (563 loc) · 20.4 KB
/
typeahead.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/* tslint:disable:max-file-line-count */
import {
ChangeDetectorRef,
Directive,
ElementRef,
EventEmitter,
HostListener,
Input,
OnDestroy,
OnInit,
Output,
Renderer2,
TemplateRef,
ViewContainerRef
} from '@angular/core';
import { NgControl } from '@angular/forms';
import { from, Subscription, isObservable, Observable } from 'rxjs';
import { ComponentLoader, ComponentLoaderFactory } from 'ngx-bootstrap/component-loader';
import { debounceTime, filter, mergeMap, switchMap, toArray } from 'rxjs/operators';
import { TypeaheadContainerComponent } from './typeahead-container.component';
import { TypeaheadMatch } from './typeahead-match.class';
import { TypeaheadConfig } from './typeahead.config';
import { getValueFromObject, latinize, tokenize } from './typeahead-utils';
import { TypeaheadOrder } from './typeahead-order.class';
import { TypeaheadOptionItemContext, TypeaheadOptionListContext } from './models';
type TypeaheadOption = string | {[key in string | number]: any};
type Typeahead = TypeaheadOption[] | Observable<TypeaheadOption[]>;
@Directive({
selector: '[typeahead]',
exportAs: 'bs-typeahead',
host: {
'[attr.aria-activedescendant]': 'activeDescendant',
'[attr.aria-aria-owns]': 'isOpen ? this._container.popupId : null',
'[attr.aria-aria-expanded]': 'isOpen',
'[attr.aria-autocomplete]': 'list'
}
})
export class TypeaheadDirective implements OnInit, OnDestroy {
/** options source, can be Array of strings, objects or
* an Observable for external matching process
*/
@Input() typeahead: Typeahead;
/** minimal no of characters that needs to be entered before
* typeahead kicks-in. When set to 0, typeahead shows on focus with full
* list of options (limited as normal by typeaheadOptionsLimit)
*/
@Input() typeaheadMinLength: number = void 0;
/** sets use adaptive position */
@Input() adaptivePosition: boolean;
/** turn on/off animation */
@Input() isAnimated = false;
/** minimal wait time after last character typed before typeahead kicks-in */
@Input() typeaheadWaitMs: number;
/** maximum length of options items list. The default value is 20 */
@Input() typeaheadOptionsLimit: number;
/** when options source is an array of objects, the name of field
* that contains the options value, we use array item as option in case
* of this field is missing. Supports nested properties and methods.
*/
@Input() typeaheadOptionField: string;
/** when options source is an array of objects, the name of field that
* contains the group value, matches are grouped by this field when set.
*/
@Input() typeaheadGroupField: string;
/** Used to specify a custom order of matches. When options source is an array of objects
* a field for sorting has to be set up. In case of options source is an array of string,
* a field for sorting is absent. The ordering direction could be changed to ascending or descending.
*/
@Input() typeaheadOrderBy: TypeaheadOrder;
/** should be used only in case of typeahead attribute is Observable of array.
* If true - loading of options will be async, otherwise - sync.
* true make sense if options array is large.
*/
@Input() typeaheadAsync: boolean = void 0;
/** match latin symbols.
* If true the word súper would match super and vice versa.
*/
@Input() typeaheadLatinize = true;
/** Can be use to search words by inserting a single white space between each characters
* for example 'C a l i f o r n i a' will match 'California'.
*/
@Input() typeaheadSingleWords = true;
/** should be used only in case typeaheadSingleWords attribute is true.
* Sets the word delimiter to break words. Defaults to space.
*/
@Input() typeaheadWordDelimiters = ' ';
/** should be used only in case typeaheadSingleWords attribute is true.
* Sets the word delimiter to match exact phrase.
* Defaults to simple and double quotes.
*/
@Input() typeaheadPhraseDelimiters = '\'"';
/** used to specify a custom item template.
* Template variables exposed are called item and index;
*/
@Input() typeaheadItemTemplate: TemplateRef<TypeaheadOptionItemContext>;
/** used to specify a custom options list template.
* Template variables: matches, itemTemplate, query
*/
@Input() optionsListTemplate: TemplateRef<TypeaheadOptionListContext>;
/** specifies if typeahead is scrollable */
@Input() typeaheadScrollable = false;
/** specifies number of options to show in scroll view */
@Input() typeaheadOptionsInScrollableView = 5;
/** used to hide result on blur */
@Input() typeaheadHideResultsOnBlur: boolean;
/** fired when an options list was opened and the user clicked Tab
* If a value equal true, it will be chosen first or active item in the list
* If value equal false, it will be chosen an active item in the list or nothing
*/
@Input() typeaheadSelectFirstItem = true;
/** makes active first item in a list */
@Input() typeaheadIsFirstItemActive = true;
/** fired when 'busy' state of this component was changed,
* fired on async mode only, returns boolean
*/
@Output() typeaheadLoading = new EventEmitter<boolean>();
/** fired on every key event and returns true
* in case of matches are not detected
*/
@Output() typeaheadNoResults = new EventEmitter<boolean>();
/** fired when option was selected, return object with data of this option */
@Output() typeaheadOnSelect = new EventEmitter<TypeaheadMatch>();
/** fired when blur event occurs. returns the active item */
@Output() typeaheadOnBlur = new EventEmitter<TypeaheadMatch>();
/**
* A selector specifying the element the typeahead should be appended to.
*/
@Input() container: string;
/** This attribute indicates that the dropdown should be opened upwards */
@Input() dropup = false;
// not yet implemented
/** if false restrict model values to the ones selected from the popup only will be provided */
// @Input() protected typeaheadEditable:boolean;
/** if false the first match automatically will not be focused as you type */
// @Input() protected typeaheadFocusFirst:boolean;
/** format the ng-model result after selection */
// @Input() protected typeaheadInputFormatter:any;
/** if true automatically select an item when there is one option that exactly matches the user input */
// @Input() protected typeaheadSelectOnExact:boolean;
/** if true select the currently highlighted match on blur */
// @Input() protected typeaheadSelectOnBlur:boolean;
/** if false don't focus the input element the typeahead directive is associated with on selection */
// @Input() protected typeaheadFocusOnSelect:boolean;
activeDescendant: string;
isOpen = false;
list = 'list';
_container: TypeaheadContainerComponent;
isActiveItemChanged = false;
isFocused = false;
cancelRequestOnFocusLost = false;
// tslint:disable-next-line:no-any
protected keyUpEventEmitter: EventEmitter<string> = new EventEmitter();
protected _matches: TypeaheadMatch[];
protected placement = 'bottom left';
private _typeahead: ComponentLoader<TypeaheadContainerComponent>;
private _subscriptions: Subscription[] = [];
private _outsideClickListener: Function;
constructor(
cis: ComponentLoaderFactory,
config: TypeaheadConfig,
private changeDetection: ChangeDetectorRef,
private element: ElementRef,
private ngControl: NgControl,
private renderer: Renderer2,
viewContainerRef: ViewContainerRef
) {
this._typeahead = cis.createLoader<TypeaheadContainerComponent>(
element,
viewContainerRef,
renderer
)
.provide({ provide: TypeaheadConfig, useValue: config });
Object.assign(this,
{
typeaheadHideResultsOnBlur: config.hideResultsOnBlur,
typeaheadCancelRequestOnFocusLost: config.cancelRequestOnFocusLost,
typeaheadSelectFirstItem: config.selectFirstItem,
typeaheadIsFirstItemActive: config.isFirstItemActive,
typeaheadMinLength: config.minLength,
adaptivePosition: config.adaptivePosition,
isAnimated: config.isAnimated
}
);
}
ngOnInit(): void {
this.typeaheadOptionsLimit = this.typeaheadOptionsLimit || 20;
this.typeaheadMinLength =
this.typeaheadMinLength === void 0 ? 1 : this.typeaheadMinLength;
this.typeaheadWaitMs = this.typeaheadWaitMs || 0;
// async should be false in case of array
if (this.typeaheadAsync === undefined && !(isObservable(this.typeahead))) {
this.typeaheadAsync = false;
}
if (isObservable(this.typeahead)) {
this.typeaheadAsync = true;
}
if (this.typeaheadAsync) {
this.asyncActions();
} else {
this.syncActions();
}
}
@HostListener('input', ['$event'])
// tslint:disable-next-line:no-any
onInput(e: any): void {
// For `<input>`s, use the `value` property. For others that don't have a
// `value` (such as `<span contenteditable="true">`), use either
// `textContent` or `innerText` (depending on which one is supported, i.e.
// Firefox or IE).
const value =
e.target.value !== undefined
? e.target.value
: e.target.textContent !== undefined
? e.target.textContent
: e.target.innerText;
if (value != null && value.trim().length >= this.typeaheadMinLength) {
this.typeaheadLoading.emit(true);
this.keyUpEventEmitter.emit(e.target.value);
} else {
this.typeaheadLoading.emit(false);
this.typeaheadNoResults.emit(false);
this.hide();
}
}
@HostListener('keyup', ['$event'])
onChange(event: KeyboardEvent): void {
if (this._container) {
// esc
/* tslint:disable-next-line: deprecation */
if (event.keyCode === 27 || event.key === 'Escape') {
this.hide();
return;
}
// up
/* tslint:disable-next-line: deprecation */
if (event.keyCode === 38 || event.key === 'ArrowUp') {
this.isActiveItemChanged = true;
this._container.prevActiveMatch();
return;
}
// down
/* tslint:disable-next-line: deprecation */
if (event.keyCode === 40 || event.key === 'ArrowDown') {
this.isActiveItemChanged = true;
this._container.nextActiveMatch();
return;
}
// enter
/* tslint:disable-next-line: deprecation */
if (event.keyCode === 13 || event.key === 'Enter') {
this._container.selectActiveMatch();
return;
}
}
}
@HostListener('click')
@HostListener('focus')
onFocus(): void {
this.isFocused = true;
// add setTimeout to fix issue #5251
// to get and emit updated value if it's changed on focus
setTimeout(() => {
if (this.typeaheadMinLength === 0) {
this.typeaheadLoading.emit(true);
this.keyUpEventEmitter.emit(this.element.nativeElement.value || '');
}
}, 0);
}
@HostListener('blur')
onBlur(): void {
this.isFocused = false;
if (this._container && !this._container.isFocused) {
this.typeaheadOnBlur.emit(this._container.active);
}
if (!this.container && this._matches.length === 0) {
this.typeaheadOnBlur.emit(new TypeaheadMatch(
this.element.nativeElement.value,
this.element.nativeElement.value,
false));
}
}
@HostListener('keydown', ['$event'])
onKeydown(event: KeyboardEvent): void {
// no container - no problems
if (!this._container) {
return;
}
/* tslint:disable-next-line: deprecation */
if (event.keyCode === 9 || event.key === 'Tab') {
this.onBlur();
}
/* tslint:disable-next-line: deprecation */
if (event.keyCode === 9 || event.key === 'Tab' || event.keyCode === 13 || event.key === 'Enter') {
event.preventDefault();
if (this.typeaheadSelectFirstItem) {
this._container.selectActiveMatch();
return;
}
if (!this.typeaheadSelectFirstItem) {
this._container.selectActiveMatch(this.isActiveItemChanged);
this.isActiveItemChanged = false;
this.hide();
}
}
}
changeModel(match: TypeaheadMatch): void {
const valueStr: string = match.value;
this.ngControl.viewToModelUpdate(valueStr);
(this.ngControl.control).setValue(valueStr);
this.changeDetection.markForCheck();
this.hide();
}
get matches(): TypeaheadMatch[] {
return this._matches;
}
show(): void {
this._typeahead
.attach(TypeaheadContainerComponent)
.to(this.container)
.position({attachment: `${this.dropup ? 'top' : 'bottom'} left`})
.show({
typeaheadRef: this,
placement: this.placement,
animation: false,
dropup: this.dropup
});
this._outsideClickListener = this.renderer.listen('document', 'click', (e: MouseEvent) => {
if (this.typeaheadMinLength === 0 && this.element.nativeElement.contains(e.target)) {
return undefined;
}
if (!this.typeaheadHideResultsOnBlur || this.element.nativeElement.contains(e.target)) {
return undefined;
}
this.onOutsideClick();
});
this._container = this._typeahead.instance;
this._container.parent = this;
// This improves the speed as it won't have to be done for each list item
const normalizedQuery = (this.typeaheadLatinize
? latinize(this.ngControl.control.value)
: this.ngControl.control.value)
.toString()
.toLowerCase();
this._container.query = this.typeaheadSingleWords
? tokenize(
normalizedQuery,
this.typeaheadWordDelimiters,
this.typeaheadPhraseDelimiters
)
: normalizedQuery;
this._container.matches = this._matches;
this.element.nativeElement.focus();
this._container.activeChangeEvent.subscribe((activeId: string) => {
this.activeDescendant = activeId;
this.changeDetection.markForCheck();
});
this.isOpen = true;
}
hide(): void {
if (this._typeahead.isShown) {
this._typeahead.hide();
this._outsideClickListener();
this._container = null;
this.isOpen = false;
this.changeDetection.markForCheck();
}
}
onOutsideClick(): void {
if (this._container && !this._container.isFocused) {
this.hide();
}
}
ngOnDestroy() {
// clean up subscriptions
for (const sub of this._subscriptions) {
sub.unsubscribe();
}
this._typeahead.dispose();
}
protected asyncActions(): void {
this._subscriptions.push(
this.keyUpEventEmitter
.pipe(
debounceTime<string>(this.typeaheadWaitMs),
switchMap(() => this.typeahead)
)
.subscribe((matches: TypeaheadOption[]) => {
this.finalizeAsyncCall(matches);
})
);
}
protected syncActions(): void {
this._subscriptions.push(
this.keyUpEventEmitter
.pipe(
debounceTime<string>(this.typeaheadWaitMs),
mergeMap((value: string) => {
const normalizedQuery = this.normalizeQuery(value);
return from(this.typeahead)
.pipe(
filter((option: TypeaheadOption) => {
return option && this.testMatch(this.normalizeOption(option), normalizedQuery);
}),
toArray()
);
})
)
.subscribe((matches: TypeaheadOption[]) => {
this.finalizeAsyncCall(matches);
})
);
}
protected normalizeOption(option: TypeaheadOption): string {
const optionValue: string = getValueFromObject(
option,
this.typeaheadOptionField
);
const normalizedOption = this.typeaheadLatinize
? latinize(optionValue)
: optionValue;
return normalizedOption.toLowerCase();
}
protected normalizeQuery(value: string): string | string[] {
// If singleWords, break model here to not be doing extra work on each iteration
let normalizedQuery: string | string[] = (this.typeaheadLatinize
? latinize(value)
: value)
.toString()
.toLowerCase();
normalizedQuery = this.typeaheadSingleWords
? tokenize(
normalizedQuery,
this.typeaheadWordDelimiters,
this.typeaheadPhraseDelimiters
)
: normalizedQuery;
return normalizedQuery;
}
protected testMatch(match: string, test: string[] | string): boolean {
let spaceLength: number;
if (typeof test === 'object') {
spaceLength = test.length;
for (let i = 0; i < spaceLength; i += 1) {
if (test[i].length > 0 && match.indexOf(test[i]) < 0) {
return false;
}
}
return true;
}
return match.indexOf(test) >= 0;
}
protected finalizeAsyncCall(matches: TypeaheadOption[]): void {
this.prepareMatches(matches || []);
this.typeaheadLoading.emit(false);
this.typeaheadNoResults.emit(!this.hasMatches());
if (!this.hasMatches()) {
this.hide();
return;
}
if (!this.isFocused && this.cancelRequestOnFocusLost) {
return;
}
if (this._container) {
// fix: remove usage of ngControl internals
const _controlValue = (this.typeaheadLatinize
? latinize(this.ngControl.control.value)
: this.ngControl.control.value) || '';
// This improves the speed as it won't have to be done for each list item
const normalizedQuery = _controlValue.toString().toLowerCase();
this._container.query = this.typeaheadSingleWords
? tokenize(
normalizedQuery,
this.typeaheadWordDelimiters,
this.typeaheadPhraseDelimiters
)
: normalizedQuery;
this._container.matches = this._matches;
} else {
this.show();
}
}
protected prepareMatches(options: TypeaheadOption[]): void {
const limited = options.slice(0, this.typeaheadOptionsLimit);
const sorted = !this.typeaheadOrderBy ? limited : this.orderMatches(limited);
if (this.typeaheadGroupField) {
let matches: TypeaheadMatch[] = [];
// extract all group names
const groups = sorted
.map((option: TypeaheadMatch) =>
getValueFromObject(option, this.typeaheadGroupField)
)
.filter((v: string, i: number, a: string[]) => a.indexOf(v) === i);
groups.forEach((group: string) => {
// add group header to array of matches
matches.push(new TypeaheadMatch(group, group, true));
// add each item of group to array of matches
matches = matches.concat(
sorted
.filter((option: TypeaheadOption) =>
getValueFromObject(option, this.typeaheadGroupField) === group
)
.map((option: TypeaheadOption) =>
new TypeaheadMatch(
option,
getValueFromObject(option, this.typeaheadOptionField)
)
)
);
});
this._matches = matches;
} else {
this._matches = sorted.map(
// tslint:disable-next-line:no-any
(option: any) =>
new TypeaheadMatch(
option,
getValueFromObject(option, this.typeaheadOptionField)
)
);
}
}
protected orderMatches(options: TypeaheadOption[]): TypeaheadOption[] {
if (!options.length) {
return options;
}
if (this.typeaheadOrderBy !== null
&& this.typeaheadOrderBy !== undefined
&& typeof this.typeaheadOrderBy === 'object'
&& Object.keys(this.typeaheadOrderBy).length === 0) {
// tslint:disable-next-line:no-console
console.error('Field and direction properties for typeaheadOrderBy have to be set according to documentation!');
return options;
}
const { field, direction } = this.typeaheadOrderBy;
if (!direction || !(direction === 'asc' || direction === 'desc')) {
// tslint:disable-next-line:no-console
console.error('typeaheadOrderBy direction has to equal "asc" or "desc". Please follow the documentation.');
return options;
}
if (typeof options[0] === 'string') {
return direction === 'asc' ? options.sort() : options.sort().reverse();
}
if (!field || typeof field !== 'string') {
// tslint:disable-next-line:no-console
console.error('typeaheadOrderBy field has to set according to the documentation.');
return options;
}
return options.sort((a: TypeaheadOption, b: TypeaheadOption) => {
const stringA = getValueFromObject(a, field);
const stringB = getValueFromObject(b, field);
if (stringA < stringB) {
return direction === 'asc' ? -1 : 1;
}
if (stringA > stringB) {
return direction === 'asc' ? 1 : -1;
}
return 0;
});
}
protected hasMatches(): boolean {
return this._matches.length > 0;
}
}