forked from valor-software/ng2-select
-
Notifications
You must be signed in to change notification settings - Fork 42
/
ngx-select.component.ts
622 lines (551 loc) · 25.6 KB
/
ngx-select.component.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
import {
AfterContentChecked, DoCheck, Input, Output, ViewChild,
Component, ElementRef, EventEmitter, forwardRef, HostListener, IterableDiffer, IterableDiffers, ChangeDetectorRef, ContentChild,
TemplateRef, Optional, Inject, InjectionToken, ChangeDetectionStrategy
} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
import {Observable, Subject, BehaviorSubject, EMPTY, of, from, merge, combineLatest} from 'rxjs';
import {tap, filter, map, share, flatMap, toArray, distinctUntilChanged} from 'rxjs/operators';
import * as lodashNs from 'lodash';
import * as escapeStringNs from 'escape-string-regexp';
import {NgxSelectOptGroup, NgxSelectOption, TSelectOption} from './ngx-select.classes';
import {NgxSelectOptionDirective, NgxSelectOptionNotFoundDirective, NgxSelectOptionSelectedDirective} from './ngx-templates.directive';
import {INgxOptionNavigated, INgxSelectOption, INgxSelectOptions} from './ngx-select.interfaces';
const _ = lodashNs;
const escapeString = escapeStringNs;
export const NGX_SELECT_OPTIONS = new InjectionToken<any>('NGX_SELECT_OPTIONS');
export interface INgxSelectComponentMouseEvent extends MouseEvent {
clickedSelectComponent?: NgxSelectComponent;
}
enum ENavigation {
first, previous, next, last,
firstSelected, firstIfOptionActiveInvisible
}
function propertyExists(obj: Object, propertyName: string) {
return propertyName in obj;
}
@Component({
selector: 'ngx-select',
templateUrl: './ngx-select.component.html',
styleUrls: ['./ngx-select.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NgxSelectComponent),
multi: true
}
]
})
export class NgxSelectComponent implements INgxSelectOptions, ControlValueAccessor, DoCheck, AfterContentChecked {
@Input() public items: any[];
@Input() public optionValueField = 'id';
@Input() public optionTextField = 'text';
@Input() public optGroupLabelField = 'label';
@Input() public optGroupOptionsField = 'options';
@Input() public multiple = false;
@Input() public allowClear = false;
@Input() public placeholder = '';
@Input() public noAutoComplete = false;
@Input() public disabled = false;
@Input() public defaultValue: any[] = [];
@Input() public autoSelectSingleOption = false;
@Input() public autoClearSearch = false;
@Input() public noResultsFound = 'No results found';
@Input() public keepSelectedItems: false;
@Input() public size: 'small' | 'default' | 'large' = 'default';
@Input() public searchCallback: (search: string, item: INgxSelectOption) => boolean;
@Input() public autoActiveOnMouseEnter = true;
@Input() public isFocused = false;
public keyCodeToRemoveSelected = 'Delete';
public keyCodeToOptionsOpen = ['Enter', 'NumpadEnter'];
public keyCodeToOptionsClose = 'Escape';
public keyCodeToOptionsSelect = ['Enter', 'NumpadEnter'];
public keyCodeToNavigateFirst = 'ArrowLeft';
public keyCodeToNavigatePrevious = 'ArrowUp';
public keyCodeToNavigateNext = 'ArrowDown';
public keyCodeToNavigateLast = 'ArrowRight';
@Output() public typed = new EventEmitter<string>();
@Output() public focus = new EventEmitter<void>();
@Output() public blur = new EventEmitter<void>();
@Output() public open = new EventEmitter<void>();
@Output() public close = new EventEmitter<void>();
@Output() public select = new EventEmitter<any>();
@Output() public remove = new EventEmitter<any>();
@Output() public navigated = new EventEmitter<INgxOptionNavigated>();
@Output() public selectionChanges = new EventEmitter<INgxSelectOption[]>();
@ViewChild('main') protected mainElRef: ElementRef;
@ViewChild('input') protected inputElRef: ElementRef;
@ViewChild('choiceMenu') protected choiceMenuElRef: ElementRef;
@ContentChild(NgxSelectOptionDirective, {read: TemplateRef}) templateOption: NgxSelectOptionDirective;
@ContentChild(NgxSelectOptionSelectedDirective, {read: TemplateRef}) templateSelectedOption: NgxSelectOptionSelectedDirective;
@ContentChild(NgxSelectOptionNotFoundDirective, {read: TemplateRef}) templateOptionNotFound: NgxSelectOptionNotFoundDirective;
public optionsOpened = false;
public optionsFiltered: TSelectOption[];
private optionActive: NgxSelectOption;
private itemsDiffer: IterableDiffer<any>;
private defaultValueDiffer: IterableDiffer<any[]>;
private actualValue: any[] = [];
public subjOptions = new BehaviorSubject<TSelectOption[]>([]);
private subjSearchText = new BehaviorSubject<string>('');
private subjOptionsSelected = new BehaviorSubject<NgxSelectOption[]>([]);
private subjExternalValue = new BehaviorSubject<any[]>([]);
private subjDefaultValue = new BehaviorSubject<any[]>([]);
private subjRegisterOnChange = new Subject();
private cacheOptionsFilteredFlat: NgxSelectOption[];
private cacheElementOffsetTop: number;
private _focusToInput = false;
/** @internal */
public get inputText() {
if (this.inputElRef && this.inputElRef.nativeElement) {
return this.inputElRef.nativeElement.value;
}
return '';
}
constructor(iterableDiffers: IterableDiffers, private sanitizer: DomSanitizer, private cd: ChangeDetectorRef,
@Inject(NGX_SELECT_OPTIONS) @Optional() defaultOptions: INgxSelectOptions) {
Object.assign(this, defaultOptions);
// DIFFERS
this.itemsDiffer = iterableDiffers.find([]).create<any>(null);
this.defaultValueDiffer = iterableDiffers.find([]).create<any>(null);
// OBSERVERS
this.typed.subscribe((text: string) => this.subjSearchText.next(text));
this.subjOptionsSelected.subscribe((options: NgxSelectOption[]) => this.selectionChanges.emit(options));
let cacheExternalValue: any[];
// Get actual value
const subjActualValue = combineLatest(
merge(
this.subjExternalValue.pipe(map(
(v: any[]) => cacheExternalValue = v === null ? [] : [].concat(v)
)),
this.subjOptionsSelected.pipe(map(
(options: NgxSelectOption[]) => options.map((o: NgxSelectOption) => o.value)
))
),
this.subjDefaultValue
).pipe(
map(([eVal, dVal]: [any[], any[]]) => {
const newVal = _.isEqual(eVal, dVal) ? [] : eVal;
return newVal.length ? newVal : dVal;
}),
distinctUntilChanged((x, y) => _.isEqual(x, y)),
share()
);
// Export actual value
combineLatest(subjActualValue, this.subjRegisterOnChange)
.pipe(map(([actualValue]: [any[], any[]]) => actualValue))
.subscribe((actualValue: any[]) => {
this.actualValue = actualValue;
if (!_.isEqual(actualValue, cacheExternalValue)) {
cacheExternalValue = actualValue;
if (this.multiple) {
this.onChange(actualValue);
} else {
this.onChange(actualValue.length ? actualValue[0] : null);
}
}
});
// Correct selected options when the options changed
combineLatest(
this.subjOptions.pipe(
flatMap((options: TSelectOption[]) => from(options).pipe(
flatMap((option: TSelectOption) => option instanceof NgxSelectOption
? of(option)
: (option instanceof NgxSelectOptGroup ? from(option.options) : EMPTY)
),
toArray()
))
),
subjActualValue
).pipe(
map(([optionsFlat, actualValue]: [NgxSelectOption[], any[]]) => {
const optionsSelected = [];
actualValue.forEach((value: any) => {
const selectedOption = optionsFlat.find((option: NgxSelectOption) => option.value === value);
if (selectedOption) {
optionsSelected.push(selectedOption);
}
});
if (this.keepSelectedItems) {
const optionValues = optionsSelected.map((option: NgxSelectOption) => option.value);
const keptSelectedOptions = this.subjOptionsSelected.value
.filter((selOption: NgxSelectOption) => optionValues.indexOf(selOption.value) === -1);
optionsSelected.push(...keptSelectedOptions);
}
if (!_.isEqual(optionsSelected, this.subjOptionsSelected.value)) {
this.subjOptionsSelected.next(optionsSelected);
this.cd.markForCheck();
}
})
).subscribe();
// Ensure working filter by a search text
combineLatest(this.subjOptions, this.subjOptionsSelected, this.subjSearchText).pipe(
map(([options, selectedOptions, search]: [TSelectOption[], NgxSelectOption[], string]) => {
this.optionsFiltered = this.filterOptions(search, options, selectedOptions).map(option => {
if (option instanceof NgxSelectOption) {
option.highlightedText = this.highlightOption(option);
} else if (option instanceof NgxSelectOptGroup) {
option.options.map(subOption => {
subOption.highlightedText = this.highlightOption(subOption);
return subOption;
});
}
return option;
});
this.cacheOptionsFilteredFlat = null;
this.navigateOption(ENavigation.firstIfOptionActiveInvisible);
this.cd.markForCheck();
return selectedOptions;
}),
flatMap((selectedOptions: NgxSelectOption[]) => this.optionsFilteredFlat().pipe(filter(
(flatOptions: NgxSelectOption[]) => this.autoSelectSingleOption && flatOptions.length === 1 && !selectedOptions.length
)))
).subscribe((flatOptions: NgxSelectOption[]) => {
this.subjOptionsSelected.next(flatOptions);
this.cd.markForCheck();
});
}
public setFormControlSize(otherClassNames: Object = {}, useFormControl: boolean = true) {
const formControlExtraClasses = useFormControl ? {
'form-control-sm input-sm': this.size === 'small',
'form-control-lg input-lg': this.size === 'large'
} : {};
return Object.assign(formControlExtraClasses, otherClassNames);
}
public setBtnSize() {
return {'btn-sm': this.size === 'small', 'btn-lg': this.size === 'large'};
}
public get optionsSelected(): NgxSelectOption[] {
return this.subjOptionsSelected.value;
}
public mainClicked(event: INgxSelectComponentMouseEvent) {
event.clickedSelectComponent = this;
if (!this.isFocused) {
this.isFocused = true;
this.focus.emit();
}
}
@HostListener('document:focusin', ['$event'])
@HostListener('document:click', ['$event'])
public documentClick(event: INgxSelectComponentMouseEvent) {
if (event.clickedSelectComponent !== this) {
if (this.optionsOpened) {
this.optionsClose();
this.cd.detectChanges(); // fix error because of delay between different events
}
if (this.isFocused) {
this.isFocused = false;
this.blur.emit();
}
}
}
private optionsFilteredFlat(): Observable<NgxSelectOption[]> {
if (this.cacheOptionsFilteredFlat) {
return of(this.cacheOptionsFilteredFlat);
}
return from(this.optionsFiltered).pipe(
flatMap<TSelectOption, NgxSelectOption>((option: TSelectOption) =>
option instanceof NgxSelectOption ? of(option) :
(option instanceof NgxSelectOptGroup ? from(option.optionsFiltered) : EMPTY)
),
filter((optionsFilteredFlat: NgxSelectOption) => !optionsFilteredFlat.disabled),
toArray(),
tap((optionsFilteredFlat: NgxSelectOption[]) => this.cacheOptionsFilteredFlat = optionsFilteredFlat)
);
}
private navigateOption(navigation: ENavigation) {
this.optionsFilteredFlat().pipe(
map<NgxSelectOption[], INgxOptionNavigated>((options: NgxSelectOption[]) => {
const navigated: INgxOptionNavigated = {index: -1, activeOption: null, filteredOptionList: options};
let newActiveIdx;
switch (navigation) {
case ENavigation.first:
navigated.index = 0;
break;
case ENavigation.previous:
newActiveIdx = options.indexOf(this.optionActive) - 1;
navigated.index = newActiveIdx >= 0 ? newActiveIdx : options.length - 1;
break;
case ENavigation.next:
newActiveIdx = options.indexOf(this.optionActive) + 1;
navigated.index = newActiveIdx < options.length ? newActiveIdx : 0;
break;
case ENavigation.last:
navigated.index = options.length - 1;
break;
case ENavigation.firstSelected:
if (this.subjOptionsSelected.value.length) {
navigated.index = options.indexOf(this.subjOptionsSelected.value[0]);
}
break;
case ENavigation.firstIfOptionActiveInvisible:
const idxOfOptionActive = options.indexOf(this.optionActive);
navigated.index = idxOfOptionActive > 0 ? idxOfOptionActive : 0;
break;
}
navigated.activeOption = options[navigated.index];
return navigated;
})
).subscribe((newNavigated: INgxOptionNavigated) => this.optionActivate(newNavigated));
}
public ngDoCheck(): void {
if (this.itemsDiffer.diff(this.items)) {
this.subjOptions.next(this.buildOptions(this.items));
}
const defVal = this.defaultValue ? [].concat(this.defaultValue) : [];
if (this.defaultValueDiffer.diff(defVal)) {
this.subjDefaultValue.next(defVal);
}
}
public ngAfterContentChecked(): void {
if (this._focusToInput && this.checkInputVisibility() && this.inputElRef &&
this.inputElRef.nativeElement !== document.activeElement) {
this._focusToInput = false;
this.inputElRef.nativeElement.focus();
}
if (this.choiceMenuElRef) {
const ulElement = this.choiceMenuElRef.nativeElement as HTMLUListElement;
const element = ulElement.querySelector('a.ngx-select__item_active.active') as HTMLLinkElement;
if (element && element.offsetHeight > 0) {
this.ensureVisibleElement(element);
}
}
}
public canClearNotMultiple(): boolean {
return this.allowClear && !!this.subjOptionsSelected.value.length &&
(!this.subjDefaultValue.value.length || this.subjDefaultValue.value[0] !== this.actualValue[0]);
}
public focusToInput(): void {
this._focusToInput = true;
}
public inputKeyDown(event: KeyboardEvent) {
const keysForOpenedState = [].concat(
this.keyCodeToOptionsSelect,
this.keyCodeToNavigateFirst,
this.keyCodeToNavigatePrevious,
this.keyCodeToNavigateNext,
this.keyCodeToNavigateLast
);
const keysForClosedState = [].concat(this.keyCodeToOptionsOpen, this.keyCodeToRemoveSelected);
if (this.optionsOpened && keysForOpenedState.indexOf(event.code) !== -1) {
event.preventDefault();
event.stopPropagation();
switch (event.code) {
case ([].concat(this.keyCodeToOptionsSelect).indexOf(event.code) + 1) && event.code:
this.optionSelect(this.optionActive);
this.navigateOption(ENavigation.next);
break;
case this.keyCodeToNavigateFirst:
this.navigateOption(ENavigation.first);
break;
case this.keyCodeToNavigatePrevious:
this.navigateOption(ENavigation.previous);
break;
case this.keyCodeToNavigateLast:
this.navigateOption(ENavigation.last);
break;
case this.keyCodeToNavigateNext:
this.navigateOption(ENavigation.next);
break;
}
} else if (!this.optionsOpened && keysForClosedState.indexOf(event.code) !== -1) {
event.preventDefault();
event.stopPropagation();
switch (event.code) {
case ([].concat(this.keyCodeToOptionsOpen).indexOf(event.code) + 1) && event.code:
this.optionsOpen();
break;
case this.keyCodeToRemoveSelected:
this.optionRemove(this.subjOptionsSelected.value[this.subjOptionsSelected.value.length - 1], event);
break;
}
}
}
public trackByOption(index: number, option: TSelectOption) {
return option instanceof NgxSelectOption ? option.value :
(option instanceof NgxSelectOptGroup ? option.label : option);
}
public checkInputVisibility(): boolean {
return (this.multiple === true) || (this.optionsOpened && !this.noAutoComplete);
}
/** @internal */
public inputKeyUp(value: string = '', event: KeyboardEvent) {
if (event.code === this.keyCodeToOptionsClose) {
this.optionsClose(/*true*/);
} else if (this.optionsOpened) {
this.typed.emit(value);
} else if (!this.optionsOpened && value) {
this.optionsOpen(value);
}
}
/** @internal */
public inputClick(value: string = '') {
if (!this.optionsOpened) {
this.optionsOpen(value);
}
}
/** @internal */
public sanitize(html: string): SafeHtml {
return html ? this.sanitizer.bypassSecurityTrustHtml(html) : null;
}
/** @internal */
public highlightOption(option: NgxSelectOption): SafeHtml {
if (this.inputElRef) {
return option.renderText(this.sanitizer, this.inputElRef.nativeElement.value);
}
return option.renderText(this.sanitizer, '');
}
/** @internal */
public optionSelect(option: NgxSelectOption, event: Event = null): void {
if (event) {
event.preventDefault();
event.stopPropagation();
}
if (option && !option.disabled) {
this.subjOptionsSelected.next((this.multiple ? this.subjOptionsSelected.value : []).concat([option]));
this.select.emit(option.value);
this.optionsClose(/*true*/);
this.onTouched();
}
}
/** @internal */
public optionRemove(option: NgxSelectOption, event: Event): void {
if (!this.disabled && option) {
event.stopPropagation();
this.subjOptionsSelected.next((this.multiple ? this.subjOptionsSelected.value : []).filter(o => o !== option));
this.remove.emit(option.value);
}
}
/** @internal */
public optionActivate(navigated: INgxOptionNavigated): void {
if ((this.optionActive !== navigated.activeOption) &&
(!navigated.activeOption || !navigated.activeOption.disabled)) {
if (this.optionActive) {
this.optionActive.active = false;
}
this.optionActive = navigated.activeOption;
if (this.optionActive) {
this.optionActive.active = true;
}
this.navigated.emit(navigated);
this.cd.detectChanges();
}
}
/** @internal */
public onMouseEnter(navigated: INgxOptionNavigated): void {
if (this.autoActiveOnMouseEnter) {
this.optionActivate(navigated);
}
}
private filterOptions(search: string, options: TSelectOption[], selectedOptions: NgxSelectOption[]): TSelectOption[] {
const regExp = new RegExp(escapeString(search), 'i'),
filterOption = (option: NgxSelectOption) => {
if (this.searchCallback) {
return this.searchCallback(search, option);
}
return (!search || regExp.test(option.text)) && (!this.multiple || selectedOptions.indexOf(option) === -1);
};
return options.filter((option: TSelectOption) => {
if (option instanceof NgxSelectOption) {
return filterOption(<NgxSelectOption>option);
} else if (option instanceof NgxSelectOptGroup) {
const subOp = <NgxSelectOptGroup>option;
subOp.filter((subOption: NgxSelectOption) => filterOption(subOption));
return subOp.optionsFiltered.length;
}
});
}
private ensureVisibleElement(element: HTMLElement) {
if (this.choiceMenuElRef && this.cacheElementOffsetTop !== element.offsetTop) {
this.cacheElementOffsetTop = element.offsetTop;
const container: HTMLElement = this.choiceMenuElRef.nativeElement;
if (this.cacheElementOffsetTop < container.scrollTop) {
container.scrollTop = this.cacheElementOffsetTop;
} else if (this.cacheElementOffsetTop + element.offsetHeight > container.scrollTop + container.clientHeight) {
container.scrollTop = this.cacheElementOffsetTop + element.offsetHeight - container.clientHeight;
}
}
}
public optionsOpen(search: string = '') {
if (!this.disabled) {
this.optionsOpened = true;
this.subjSearchText.next(search);
if (!this.multiple && this.subjOptionsSelected.value.length) {
this.navigateOption(ENavigation.firstSelected);
} else {
this.navigateOption(ENavigation.first);
}
this.focusToInput();
this.open.emit();
this.cd.markForCheck();
}
}
public optionsClose(/*focusToHost: boolean = false*/) {
this.optionsOpened = false;
// if (focusToHost) {
// const x = window.scrollX, y = window.scrollY;
// this.mainElRef.nativeElement.focus();
// window.scrollTo(x, y);
// }
this.close.emit();
if (this.autoClearSearch && this.multiple && this.inputElRef) {
this.inputElRef.nativeElement.value = null;
}
}
private buildOptions(data: any[]): Array<NgxSelectOption | NgxSelectOptGroup> {
const result: Array<NgxSelectOption | NgxSelectOptGroup> = [];
if (Array.isArray(data)) {
let option: NgxSelectOption;
data.forEach((item: any) => {
const isOptGroup = typeof item === 'object' && item !== null &&
propertyExists(item, this.optGroupLabelField) && propertyExists(item, this.optGroupOptionsField) &&
Array.isArray(item[this.optGroupOptionsField]);
if (isOptGroup) {
const optGroup = new NgxSelectOptGroup(item[this.optGroupLabelField]);
item[this.optGroupOptionsField].forEach((subOption: NgxSelectOption) => {
if (option = this.buildOption(subOption, optGroup)) {
optGroup.options.push(option);
}
});
result.push(optGroup);
} else if (option = this.buildOption(item, null)) {
result.push(option);
}
});
}
return result;
}
private buildOption(data: any, parent: NgxSelectOptGroup): NgxSelectOption {
let value, text, disabled;
if (typeof data === 'string' || typeof data === 'number') {
value = text = data;
disabled = false;
} else if (typeof data === 'object' && data !== null &&
(propertyExists(data, this.optionValueField) || propertyExists(data, this.optionTextField))) {
value = propertyExists(data, this.optionValueField) ? data[this.optionValueField] : data[this.optionTextField];
text = propertyExists(data, this.optionTextField) ? data[this.optionTextField] : data[this.optionValueField];
disabled = propertyExists(data, 'disabled') ? data['disabled'] : false;
} else {
return null;
}
return new NgxSelectOption(value, text, disabled, data, parent);
}
//////////// interface ControlValueAccessor ////////////
public onChange = (v: any) => v;
public onTouched: () => void = () => null;
public writeValue(obj: any): void {
this.subjExternalValue.next(obj);
}
public registerOnChange(fn: (_: any) => {}): void {
this.onChange = fn;
this.subjRegisterOnChange.next();
}
public registerOnTouched(fn: () => {}): void {
this.onTouched = fn;
}
public setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
this.cd.markForCheck();
}
}