import { AfterViewInit, Component, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'; import { UntypedFormControl } from "@angular/forms"; import { MatAutocomplete, MatAutocompleteSelectedEvent } from "@angular/material/autocomplete"; import { Subscription } from "rxjs"; import { ResourceService } from "../services/resource.service"; 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: UntypedFormControl; /** * 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(); /** * Event which emits current state of the autocompletion. Can be used for progress bar animations. */ @Output() idleEvent = new EventEmitter(); @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); } }