argos/dmp-frontend/src/app/form/dynamic-fields/dynamic-field-autocomplete/autocomplete-remote.compone...

96 lines
2.2 KiB
TypeScript
Raw Normal View History

2017-11-27 14:40:16 +01:00
import { Field } from '../../../models/Field';
2017-12-06 12:39:49 +01:00
import { Component, OnInit, Input, Output, EventEmitter, forwardRef, ViewEncapsulation } from '@angular/core';
2017-11-27 14:40:16 +01:00
import { ControlValueAccessor, FormControl, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
2018-03-08 11:54:56 +01:00
import { DatasetProfileService } from '@app/services/dataset-profile.service';
import { ActivatedRoute } from '@angular/router';
2017-11-27 14:40:16 +01:00
declare var $: any;
@Component({
selector: 'df-autocomplete',
templateUrl: './autocomplete-remote.component.html',
2017-12-06 12:39:49 +01:00
styleUrls: ['./autocomplete-remote.component.css'],
2018-03-08 11:54:56 +01:00
encapsulation: ViewEncapsulation.None,
2017-11-27 14:40:16 +01:00
})
export class AutocompleteRemoteComponent implements OnInit/* , ControlValueAccessor */ {
@Input() field: Field;
2018-03-28 15:24:47 +02:00
@Input() disabled = false
2017-11-27 14:40:16 +01:00
@Input() form: FormGroup;
private loading: boolean;
2018-03-08 11:54:56 +01:00
private datasetId;
2017-11-27 14:40:16 +01:00
values: any[] = new Array();
typeaheadMS: number = 1400;
2017-12-06 12:39:49 +01:00
2018-03-08 11:54:56 +01:00
constructor(private datasetProfileService: DatasetProfileService, route: ActivatedRoute) {
this.datasetId = route.snapshot.params['id'];
2017-11-27 14:40:16 +01:00
}
ngOnInit() {
2018-03-08 11:54:56 +01:00
let valueChanges = this.form.controls['value'].valueChanges.share();
2017-11-27 14:40:16 +01:00
valueChanges.subscribe(searchTerm => {
this.loading = true;
2018-03-08 11:54:56 +01:00
if (this.form.controls['value'].value)
2017-11-27 14:40:16 +01:00
this.resetFormGroupValue();
});
valueChanges
.debounceTime(this.typeaheadMS)
.subscribe(searchTerm => {
if (typeof searchTerm === 'string') {
this.updateByQuery(searchTerm)
}
});
}
resetFormGroupValue() {
this.form.patchValue({ value: null }, { emitEvent: false });
}
updateByQuery(query: string) {
2018-03-08 11:54:56 +01:00
this.datasetProfileService.queryAutocomplete({ profileID: this.datasetId, fieldID: this.field.id, searchTerm: query })
.subscribe(
response => {
this.values.length = 0;
response.payload.forEach(element => {
this.values.push(element.attributes.name);
});
},
error => {
}
);
2017-11-27 14:40:16 +01:00
}
onChange: any = () => { };
onTouched: any = () => { };
registerOnChange(fn) {
this.onChange = fn;
}
registerOnTouched(fn) {
this.onTouched = fn;
}
/* writeValue(value) {
if (value) {
this.value = value;
}
} */
}