import { Component, OnInit } from '@angular/core'; import { get, includes, isEqual, startsWith, toLower } from 'lodash'; import { isObservable, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { PtDynamicSelectComponent } from '../dynamic-select/dynamic-select.component'; @Component({ selector: 'prutech-dynamic-autocomplete', styleUrls: ['./dynamic-autocomplete.component.scss'], templateUrl: './dynamic-autocomplete.component.html', }) export class PtDynamicAutocompleteComponent extends PtDynamicSelectComponent implements OnInit { // tslint:disable-next-line:no-any filteredSelections: any[]; // tslint:disable-next-line:no-any filteredSelectionsAsync: Observable; get isAsyncSelections(): boolean { return isObservable(this.selections); } // tslint:disable-next-line:no-any get asyncSelections(): Observable { // tslint:disable-next-line:no-any return this.selections as Observable; } // tslint:disable-next-line:no-any get syncSelections(): any[] { // tslint:disable-next-line:no-any return this.selections as any[]; } // tslint:disable-next-line:no-any autocompleteDisplay(selection: any): string { if (get(selection, 'displayText')) { return selection.displayText; } const code: string = get(selection, 'code'); if (code) { const description: string = get(selection, 'description'); if (description && !isEqual(description, code)) { return `${code} - ${description}`; } return code; } return selection; } ngOnInit(): void { if (this.isAsyncSelections) { this.filteredSelectionsAsync = this.asyncSelections; if (this.filter) { // tslint:disable-next-line:no-any this.control.valueChanges.subscribe((value: any) => { this.filteredSelectionsAsync = this.filterSelectionsAsync(value); }); } } else { this.filteredSelections = this.filterSelections(this.control.value); if (this.filter) { // tslint:disable-next-line:no-any this.control.valueChanges.subscribe((value: any) => { this.filteredSelections = this.filterSelections(value); }); } } } // tslint:disable-next-line:no-any filterSelections(value: any): any[] { if (!value) { return this.syncSelections; } // tslint:disable-next-line:no-any return this.syncSelections.filter(((selection: any) => this.isValueMatched(selection, value))); } // tslint:disable-next-line:no-any filterSelectionsAsync(value: any): Observable { if (!value) { return this.asyncSelections; } // tslint:disable-next-line:no-any return this.asyncSelections.pipe(map((selections: any[]) => selections.filter((selection: any) => this.isValueMatched(selection, value)))); } // tslint:disable-next-line:no-any isValueMatched(selection: any, value: any): boolean { const compareWith: string = toLower(value); if (selection.correlation || selection.correlationId) { return this.matches(get(selection, 'correlationId.correlation', get(selection, 'correlation')), compareWith) || this.matches(get(selection, 'description'), compareWith) || this.matches(get(selection, 'code'), compareWith); } return this.matches(selection.label ? selection.label : selection, compareWith); } matches(compareTo: string, compareWith: string): boolean { return includes(toLower(compareTo), toLower(compareWith)); // if (this.filterType === PtFilterType.Contains) { // return includes(toLower(compareTo), toLower(compareWith)); // } else { // return startsWith(toLower(compareTo), toLower(compareWith)); // } } }