2020-06-25 20:08:07 +03:00
|
|
|
///
|
2022-01-17 14:07:46 +02:00
|
|
|
/// Copyright © 2016-2022 The Thingsboard Authors
|
2020-06-25 20:08:07 +03:00
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import { AfterViewInit, Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core';
|
|
|
|
|
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
|
2021-06-24 01:40:10 +03:00
|
|
|
import { Observable, of } from 'rxjs';
|
2021-10-20 10:24:11 +03:00
|
|
|
import { catchError, debounceTime, distinctUntilChanged, map, share, switchMap, tap } from 'rxjs/operators';
|
2021-06-24 01:40:10 +03:00
|
|
|
import { emptyPageData, PageData } from '@shared/models/page/page-data';
|
2020-06-25 20:08:07 +03:00
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@app/core/core.state';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
|
|
|
|
import { EntityInfo } from '@shared/models/entity.models';
|
|
|
|
|
import { EntityFilter } from '@shared/models/query/query.models';
|
|
|
|
|
import { EntityService } from '@core/http/entity.service';
|
2021-10-20 10:24:11 +03:00
|
|
|
import { isDefinedAndNotNull } from '@core/utils';
|
2020-06-25 20:08:07 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-aliases-entity-autocomplete',
|
|
|
|
|
templateUrl: './aliases-entity-autocomplete.component.html',
|
|
|
|
|
styleUrls: [],
|
|
|
|
|
providers: [{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => AliasesEntityAutocompleteComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
export class AliasesEntityAutocompleteComponent implements ControlValueAccessor, OnInit, AfterViewInit {
|
|
|
|
|
|
|
|
|
|
selectEntityInfoFormGroup: FormGroup;
|
|
|
|
|
|
|
|
|
|
modelValue: EntityInfo | null;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
alias: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
entityFilter: EntityFilter;
|
|
|
|
|
|
|
|
|
|
private requiredValue: boolean;
|
|
|
|
|
get required(): boolean {
|
|
|
|
|
return this.requiredValue;
|
|
|
|
|
}
|
|
|
|
|
@Input()
|
|
|
|
|
set required(value: boolean) {
|
|
|
|
|
this.requiredValue = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
|
|
|
|
@ViewChild('entityInfoInput', {static: true}) entityInfoInput: ElementRef;
|
|
|
|
|
|
|
|
|
|
filteredEntityInfos: Observable<Array<EntityInfo>>;
|
|
|
|
|
|
|
|
|
|
searchText = '';
|
|
|
|
|
|
|
|
|
|
private propagateChange = (v: any) => { };
|
|
|
|
|
|
|
|
|
|
constructor(private store: Store<AppState>,
|
|
|
|
|
public translate: TranslateService,
|
|
|
|
|
private entityService: EntityService,
|
|
|
|
|
private fb: FormBuilder) {
|
|
|
|
|
this.selectEntityInfoFormGroup = this.fb.group({
|
|
|
|
|
entityInfo: [null]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
this.filteredEntityInfos = this.selectEntityInfoFormGroup.get('entityInfo').valueChanges
|
|
|
|
|
.pipe(
|
2021-06-24 01:40:10 +03:00
|
|
|
debounceTime(150),
|
2020-06-25 20:08:07 +03:00
|
|
|
tap(value => {
|
|
|
|
|
let modelValue;
|
|
|
|
|
if (typeof value === 'string' || !value) {
|
|
|
|
|
modelValue = null;
|
|
|
|
|
} else {
|
|
|
|
|
modelValue = value;
|
|
|
|
|
}
|
|
|
|
|
this.updateView(modelValue);
|
|
|
|
|
}),
|
|
|
|
|
map(value => value ? (typeof value === 'string' ? value : value.name) : ''),
|
2021-06-24 01:40:10 +03:00
|
|
|
distinctUntilChanged(),
|
2021-10-20 10:24:11 +03:00
|
|
|
switchMap(name => this.fetchEntityInfos(name)),
|
|
|
|
|
share()
|
2020-06-25 20:08:07 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit(): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: EntityInfo | null): void {
|
|
|
|
|
this.searchText = '';
|
2021-10-20 10:24:11 +03:00
|
|
|
if (isDefinedAndNotNull(value)) {
|
2020-06-25 20:08:07 +03:00
|
|
|
this.modelValue = value;
|
|
|
|
|
this.selectEntityInfoFormGroup.get('entityInfo').patchValue(value, {emitEvent: true});
|
|
|
|
|
} else {
|
|
|
|
|
this.modelValue = null;
|
2021-10-20 10:24:11 +03:00
|
|
|
this.selectEntityInfoFormGroup.get('entityInfo').patchValue(null, {emitEvent: false});
|
2020-06-25 20:08:07 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateView(value: EntityInfo | null) {
|
|
|
|
|
if (this.modelValue !== value) {
|
|
|
|
|
this.modelValue = value;
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displayEntityInfoFn(entityInfo?: EntityInfo): string | undefined {
|
|
|
|
|
return entityInfo ? entityInfo.name : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchEntityInfos(searchText?: string): Observable<Array<EntityInfo>> {
|
|
|
|
|
this.searchText = searchText;
|
|
|
|
|
return this.getEntityInfos(this.searchText).pipe(
|
|
|
|
|
map(pageData => {
|
|
|
|
|
return pageData.data;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getEntityInfos(searchText: string): Observable<PageData<EntityInfo>> {
|
2021-06-24 01:40:10 +03:00
|
|
|
return this.entityService.findEntityInfosByFilterAndName(this.entityFilter, searchText, {ignoreLoading: true}).pipe(
|
|
|
|
|
catchError(() => of(emptyPageData<EntityInfo>()))
|
|
|
|
|
);
|
2020-06-25 20:08:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
|
this.selectEntityInfoFormGroup.get('entityInfo').patchValue(null, {emitEvent: true});
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.entityInfoInput.nativeElement.blur();
|
|
|
|
|
this.entityInfoInput.nativeElement.focus();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|