Files
mowesta/web-angular/src/app/widgets-common/location-autocomplete.component.ts
T
Adriano Markovic 83dc34fb53
LocationBasedServices/mowesta/pipeline/head There was a failure building this commit
Added location-autocomplete component.
2020-07-07 17:03:52 +02:00

107 lines
3.9 KiB
TypeScript

import { AfterViewInit, Component, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild } from '@angular/core';
import { FormControl } from "@angular/forms";
import { MatAutocomplete, MatAutocompleteSelectedEvent } from "@angular/material/autocomplete";
import { Subscription } from "rxjs";
import { ResourceService } from "../services";
import { DefaultService, LocationHierarchy } from "../api";
/**
* Autocomplete drop down menu which can be re-used for custom MatInput components.
* It displays dynamically location names while the user is entering information
* into the MatInput this component is connected to.
*/
@Component({
selector: 'app-location-autocomplete',
templateUrl: './location-autocomplete.component.html'
})
export class LocationAutocompleteComponent implements OnInit, AfterViewInit, OnDestroy {
/**
* Term which is detected for changes. Mandatory.
*/
@Input() term: FormControl;
/**
* Specifies the width of the drop down menu.
*/
@Input() panelWidth: string;
/**
* Callback function which is called when user is selecting an option from the drop down menu. Mandatory.
*/
@Input() onLocationSelected: (event: MatAutocompleteSelectedEvent) => void;
/**
* Optional filter function to filter the LocationHierarchy results to be displayed in the drop down menu.
* If omitted this function is initialized to return the given array.
*/
@Input() filterLocationsFn: (arr: LocationHierarchy[]) => LocationHierarchy[];
/**
* Event which is fired after the MatAutocomplete view element is initialized. Sends a reference of the
* MatAutocomplete view element to the parent to be plugged into the corresponding MatInput element.
*/
@Output() autocompleteInitEvent = new EventEmitter<MatAutocomplete>();
/**
* Event which emits current state of the autocompletion. Can be used for progress bar animations.
*/
@Output() idleEvent = new EventEmitter<boolean>();
@ViewChild('auto') autocompleteRef: MatAutocomplete;
locations: LocationHierarchy[];
private requestId: number;
private idle: boolean;
private subscription: Subscription;
constructor(public res: ResourceService, private apiClientService: DefaultService) { }
ngOnInit(): void {
if (!this.term) { new Error('Input term must not be empty!') }
if (!this.onLocationSelected) { new Error('Input onLocationSelected must not be empty!') }
if (!this.filterLocationsFn) { this.filterLocationsFn = arr => arr; }
if (!this.panelWidth) { this.panelWidth = '220px'; }
this.locations = [];
this.requestId = 0;
this.setIdle(true);
this.subscription = this.term.valueChanges.subscribe(() => {
this.setIdle(false);
const value = this.term.value;
const id = ++this.requestId;
this.locations = [];
if (value.length > 0) {
setTimeout(() => {
if (this.requestId === id) {
this.apiClientService.searchLocations(this.res.getLanguage(), value, 0, 20, null)
.subscribe({
next: page => {
if (id === this.requestId) {
this.locations = this.filterLocationsFn(page.elements);
this.setIdle(true);
}
},
error: _ => {
if (id === this.requestId) {
this.setIdle(true)
}
}
});
}
}, 250);
} else {
this.setIdle(true);
}
});
}
ngAfterViewInit(): void {
// Needs to be put onto the event queue or else the change detection will throw the
// nasty ExpressionChangedAfterItHasBeenCheckedError while in the debug mode.
setTimeout(() => this.autocompleteInitEvent.emit(this.autocompleteRef));
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
private setIdle(val: boolean): void {
this.idle = val;
this.idleEvent.emit(val);
}
}