argos/dmp-frontend/src/app/shared/components/autocomplete/autocomplete.component.ts

171 lines
4.8 KiB
TypeScript
Raw Normal View History

2017-12-21 10:26:11 +01:00
import { any } from 'codelyzer/util/function';
2017-12-20 16:15:27 +01:00
import { FormControl, FormGroupDirective, NgForm, FormGroup, FormBuilder } from '@angular/forms';
2017-12-18 11:01:22 +01:00
import { Observable } from 'rxjs/Rx';
import { setTimeout } from 'timers';
2017-12-20 16:15:27 +01:00
import { Component, EventEmitter, Input, OnInit, Output, ViewChild, ElementRef } from '@angular/core';
2017-12-18 11:01:22 +01:00
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/map';
import { AutoCompleteConfiguration } from './AutoCompleteConfiguration';
2017-12-20 16:15:27 +01:00
import { ErrorStateMatcher, MatInput } from '@angular/material';
2017-12-18 11:01:22 +01:00
@Component({
selector: 'auto-complete',
templateUrl: './autocomplete.component.html',
styleUrls: ['./autocomplete.component.scss']
})
export class AutocompleteComponent implements OnInit {
@Input()
configuration: AutoCompleteConfiguration;
@Input()
2017-12-20 14:34:32 +01:00
titleKey: String;
2017-12-18 11:01:22 +01:00
@Input()
2017-12-20 14:34:32 +01:00
subtitleKey: String;
2017-12-18 11:01:22 +01:00
@Input()
2017-12-20 14:34:32 +01:00
delay: number = 700;
2017-12-18 11:01:22 +01:00
@Input()
2017-12-20 14:34:32 +01:00
placeholder: String;
2017-12-20 16:15:27 +01:00
filteredItems: any[];
2017-12-20 14:34:32 +01:00
// @Input()
// validationErrorString: String;
2017-12-18 11:01:22 +01:00
2017-12-20 14:34:32 +01:00
// public errorStateMatcher: AutoCompleteErrorStateMatcher = new AutoCompleteErrorStateMatcher();
2017-12-18 11:01:22 +01:00
@Input()
required: boolean;
2017-12-21 10:26:11 +01:00
@Input()
disabled: boolean = false;
2017-12-20 14:34:32 +01:00
// @Input() selectedDropdownItem: AutoCompleteItem;
// @Output() selectedDropdownItemChange = new EventEmitter<AutoCompleteItem>();
2017-12-18 11:01:22 +01:00
2017-12-20 14:34:32 +01:00
// @Output()
// output: EventEmitter<AutoCompleteItem> = new EventEmitter<AutoCompleteItem>();
2017-12-18 11:01:22 +01:00
2017-12-20 14:34:32 +01:00
@Input() control: FormControl;
2017-12-18 11:01:22 +01:00
2017-12-20 14:34:32 +01:00
// @Input() createNew = false;
// //term = new FormControl();
// @Input()
// ClickFunctionCall: Function;
2017-12-18 11:01:22 +01:00
loading = false;
2017-12-20 16:15:27 +01:00
hasSelectedItem = false;
2017-12-18 11:01:22 +01:00
constructor() {
2017-12-20 16:15:27 +01:00
2017-12-18 11:01:22 +01:00
}
ngOnInit() {
2017-12-20 14:34:32 +01:00
const valueChanges = this.control.valueChanges.share();
2017-12-18 11:01:22 +01:00
valueChanges.subscribe(searchTerm => {
2017-12-20 16:15:27 +01:00
if (this.hasSelectedItem) {
2017-12-20 14:34:32 +01:00
this.resetFormControlValue();
2018-01-12 10:38:34 +01:00
}else{
this.loading = true;
2017-12-18 11:01:22 +01:00
}
});
2017-12-20 16:15:27 +01:00
valueChanges
2017-12-20 14:34:32 +01:00
.debounceTime(this.delay)
.distinctUntilChanged()
.switchMap(val => {
2018-01-12 10:38:34 +01:00
if (this.hasSelectedItem) {
this.loading = false;
return [];
}
2017-12-20 16:15:27 +01:00
this.configuration.requestItem.criteria.like = this.control.value;
2018-01-12 10:38:34 +01:00
return this.configuration.callback(this.configuration.requestItem).map(result => {
this.filteredItems = (<any[]>result)
this.loading = false;
})
2017-12-21 10:26:11 +01:00
}).subscribe()
2017-12-20 14:34:32 +01:00
2017-12-20 16:15:27 +01:00
// this.filteredItems = this.inputField.nativeElement.valueChanges.startWith(null)
// .debounceTime(this.delay)
// .finally(() => this.loading = false)
// .distinctUntilChanged()
// .switchMap(val => {
// this.configuration.requestItem.criteria.like = val;
// return this.configuration.callback(this.configuration.requestItem)
// })
2017-12-20 14:34:32 +01:00
// const valueChanges = this.form.controls['text'].valueChanges.share();
// valueChanges.subscribe(searchTerm => {
// this.loading = true;
// if (this.form.controls['value'].value) {
// this.resetFormGroupValue();
// }
// });
// valueChanges
// .debounceTime(this.typeaheadMS)
// .subscribe(searchTerm => {
// if (typeof searchTerm === 'string') {
// this.inputOnChange(searchTerm)
// }
// });
2017-12-18 11:01:22 +01:00
}
2017-12-20 14:34:32 +01:00
resetFormControlValue() {
2017-12-20 16:15:27 +01:00
this.hasSelectedItem = false;
2017-12-20 14:34:32 +01:00
//this.control.setValue(null, { emitEvent: false });
2017-12-18 11:01:22 +01:00
}
2017-12-20 14:34:32 +01:00
// // listingItemToDropDown(item: DropdownListingItem): AutoCompleteItem {
// // return (item as DropdownListingItem).toDropdownList();
// // }
2017-12-18 11:01:22 +01:00
2017-12-20 16:15:27 +01:00
optionSelected(event: any) {
this.hasSelectedItem = true;
this.control.setValue(event.option.value, { emitEvent: false });
2018-01-12 11:17:37 +01:00
this.filteredItems = [event.option.value];
2017-12-20 16:15:27 +01:00
//this.selectedDropdownItemChange.emit(event.option.value);
//this.form.updateValueAndValidity();
//this.options = [event.option.value];
//this.loading = false;
}
2017-12-18 11:01:22 +01:00
2017-12-20 14:34:32 +01:00
// inputOnChange(term: string) {
// //this.form.patchValue({ value: null, description: '', text: '' });
// //this.form.updateValueAndValidity();
// this.configuration.criteria.like = term;
// this.configuration.callback(this.configuration.criteria)
// .map((res: any) => this.mapper(res))
// .subscribe(
// (res: AutoCompleteItem[]) => {
// this.options = res;
// },
// null,
// () => { this.loading = false });
// }
2017-12-18 11:01:22 +01:00
2017-12-20 14:34:32 +01:00
displayWith(value: any): String {
2017-12-20 16:15:27 +01:00
if (!value) return '';
2017-12-20 14:34:32 +01:00
return value['' + this.titleKey];
2017-12-18 11:01:22 +01:00
}
2017-12-20 14:34:32 +01:00
// displayFn(item: AutoCompleteItem): string {
// return item.text ? item.text : '';
// }
2017-12-18 11:01:22 +01:00
//fieldHasErrors(control: FormControl, form: FormGroupDirective | NgForm): boolean {
// return this.errorStateMatcher(control, form);
// }
}
export class AutoCompleteErrorStateMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
const isFormSubmitted = form && form.submitted;
const isControlInvalid = control && control.invalid && (control.dirty || control.touched || isFormSubmitted);
const isFormInvalid = form && form.enabled && form.invalid && (form.dirty || form.touched || isFormSubmitted)
return !!(isControlInvalid || isFormInvalid);
}
}