2019-08-14 19:55:24 +03:00
|
|
|
///
|
2021-01-11 13:42:16 +02:00
|
|
|
/// Copyright © 2016-2021 The Thingsboard Authors
|
2019-08-14 19:55:24 +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.
|
|
|
|
|
///
|
|
|
|
|
|
2020-04-10 15:04:12 +03:00
|
|
|
import { AfterViewInit, Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core';
|
|
|
|
|
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
import { map, mergeMap, share, tap } from 'rxjs/operators';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@app/core/core.state';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { AliasEntityType, EntityType } from '@shared/models/entity-type.models';
|
|
|
|
|
import { BaseData } from '@shared/models/base-data';
|
|
|
|
|
import { EntityId } from '@shared/models/id/entity-id';
|
|
|
|
|
import { EntityService } from '@core/http/entity.service';
|
|
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
2020-08-10 15:34:23 +03:00
|
|
|
import { getCurrentAuthUser } from '@core/auth/auth.selectors';
|
|
|
|
|
import { Authority } from '@shared/models/authority.enum';
|
2019-08-14 19:55:24 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-entity-autocomplete',
|
|
|
|
|
templateUrl: './entity-autocomplete.component.html',
|
2020-04-14 11:38:30 +03:00
|
|
|
styleUrls: ['./entity-autocomplete.component.scss'],
|
2019-08-14 19:55:24 +03:00
|
|
|
providers: [{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => EntityAutocompleteComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit, AfterViewInit {
|
|
|
|
|
|
|
|
|
|
selectEntityFormGroup: FormGroup;
|
|
|
|
|
|
|
|
|
|
modelValue: string | null;
|
|
|
|
|
|
|
|
|
|
entityTypeValue: EntityType | AliasEntityType;
|
|
|
|
|
|
|
|
|
|
entitySubtypeValue: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set entityType(entityType: EntityType) {
|
|
|
|
|
if (this.entityTypeValue !== entityType) {
|
|
|
|
|
this.entityTypeValue = entityType;
|
|
|
|
|
this.load();
|
2019-08-19 20:09:41 +03:00
|
|
|
this.reset();
|
2019-08-20 20:42:48 +03:00
|
|
|
this.dirty = true;
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set entitySubtype(entitySubtype: string) {
|
|
|
|
|
if (this.entitySubtypeValue !== entitySubtype) {
|
|
|
|
|
this.entitySubtypeValue = entitySubtype;
|
|
|
|
|
const currentEntity = this.getCurrentEntity();
|
|
|
|
|
if (currentEntity) {
|
|
|
|
|
if ((currentEntity as any).type !== this.entitySubtypeValue) {
|
|
|
|
|
this.reset();
|
2019-08-20 20:42:48 +03:00
|
|
|
this.dirty = true;
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-01 17:41:27 +03:00
|
|
|
this.selectEntityFormGroup.get('entity').updateValueAndValidity();
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
excludeEntityIds: Array<string>;
|
|
|
|
|
|
2020-09-08 10:34:00 +03:00
|
|
|
@Input()
|
|
|
|
|
labelText: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
requiredText: string;
|
|
|
|
|
|
2019-08-15 20:39:56 +03:00
|
|
|
private requiredValue: boolean;
|
|
|
|
|
get required(): boolean {
|
|
|
|
|
return this.requiredValue;
|
|
|
|
|
}
|
2019-08-14 19:55:24 +03:00
|
|
|
@Input()
|
2019-08-15 20:39:56 +03:00
|
|
|
set required(value: boolean) {
|
|
|
|
|
this.requiredValue = coerceBooleanProperty(value);
|
|
|
|
|
}
|
2019-08-14 19:55:24 +03:00
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
|
|
|
|
@ViewChild('entityInput', {static: true}) entityInput: ElementRef;
|
|
|
|
|
|
|
|
|
|
entityText: string;
|
|
|
|
|
noEntitiesMatchingText: string;
|
|
|
|
|
entityRequiredText: string;
|
|
|
|
|
|
|
|
|
|
filteredEntities: Observable<Array<BaseData<EntityId>>>;
|
|
|
|
|
|
2019-12-23 14:36:44 +02:00
|
|
|
searchText = '';
|
2019-08-14 19:55:24 +03:00
|
|
|
|
2019-08-20 20:42:48 +03:00
|
|
|
private dirty = false;
|
|
|
|
|
|
2019-08-14 19:55:24 +03:00
|
|
|
private propagateChange = (v: any) => { };
|
|
|
|
|
|
|
|
|
|
constructor(private store: Store<AppState>,
|
|
|
|
|
public translate: TranslateService,
|
|
|
|
|
private entityService: EntityService,
|
|
|
|
|
private fb: FormBuilder) {
|
|
|
|
|
this.selectEntityFormGroup = this.fb.group({
|
|
|
|
|
entity: [null]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
2019-08-15 14:48:14 +03:00
|
|
|
this.filteredEntities = this.selectEntityFormGroup.get('entity').valueChanges
|
2019-08-14 19:55:24 +03:00
|
|
|
.pipe(
|
|
|
|
|
tap(value => {
|
|
|
|
|
let modelValue;
|
|
|
|
|
if (typeof value === 'string' || !value) {
|
|
|
|
|
modelValue = null;
|
|
|
|
|
} else {
|
|
|
|
|
modelValue = value.id.id;
|
|
|
|
|
}
|
|
|
|
|
this.updateView(modelValue);
|
2019-08-15 20:39:56 +03:00
|
|
|
if (value === null) {
|
|
|
|
|
this.clear();
|
|
|
|
|
}
|
2019-08-14 19:55:24 +03:00
|
|
|
}),
|
2019-08-20 20:42:48 +03:00
|
|
|
// startWith<string | BaseData<EntityId>>(''),
|
2019-08-14 19:55:24 +03:00
|
|
|
map(value => value ? (typeof value === 'string' ? value : value.name) : ''),
|
2019-08-15 20:39:56 +03:00
|
|
|
mergeMap(name => this.fetchEntities(name) ),
|
|
|
|
|
share()
|
2019-08-14 19:55:24 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit(): void {}
|
|
|
|
|
|
|
|
|
|
load(): void {
|
|
|
|
|
if (this.entityTypeValue) {
|
|
|
|
|
switch (this.entityTypeValue) {
|
|
|
|
|
case EntityType.ASSET:
|
|
|
|
|
this.entityText = 'asset.asset';
|
|
|
|
|
this.noEntitiesMatchingText = 'asset.no-assets-matching';
|
|
|
|
|
this.entityRequiredText = 'asset.asset-required';
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.DEVICE:
|
|
|
|
|
this.entityText = 'device.device';
|
|
|
|
|
this.noEntitiesMatchingText = 'device.no-devices-matching';
|
|
|
|
|
this.entityRequiredText = 'device.device-required';
|
|
|
|
|
break;
|
2020-06-21 20:38:43 +03:00
|
|
|
case EntityType.EDGE:
|
|
|
|
|
this.entityText = 'edge.edge';
|
|
|
|
|
this.noEntitiesMatchingText = 'edge.no-edges-matching';
|
|
|
|
|
this.entityRequiredText = 'edge.edge-required';
|
|
|
|
|
break;
|
2019-08-14 19:55:24 +03:00
|
|
|
case EntityType.ENTITY_VIEW:
|
|
|
|
|
this.entityText = 'entity-view.entity-view';
|
|
|
|
|
this.noEntitiesMatchingText = 'entity-view.no-entity-views-matching';
|
|
|
|
|
this.entityRequiredText = 'entity-view.entity-view-required';
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.RULE_CHAIN:
|
|
|
|
|
this.entityText = 'rulechain.rulechain';
|
|
|
|
|
this.noEntitiesMatchingText = 'rulechain.no-rulechains-matching';
|
|
|
|
|
this.entityRequiredText = 'rulechain.rulechain-required';
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.TENANT:
|
2020-04-10 15:04:12 +03:00
|
|
|
case AliasEntityType.CURRENT_TENANT:
|
2019-08-14 19:55:24 +03:00
|
|
|
this.entityText = 'tenant.tenant';
|
|
|
|
|
this.noEntitiesMatchingText = 'tenant.no-tenants-matching';
|
|
|
|
|
this.entityRequiredText = 'tenant.tenant-required';
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.CUSTOMER:
|
|
|
|
|
this.entityText = 'customer.customer';
|
|
|
|
|
this.noEntitiesMatchingText = 'customer.no-customers-matching';
|
|
|
|
|
this.entityRequiredText = 'customer.customer-required';
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.USER:
|
2020-07-02 17:30:15 +03:00
|
|
|
case AliasEntityType.CURRENT_USER:
|
2019-08-14 19:55:24 +03:00
|
|
|
this.entityText = 'user.user';
|
|
|
|
|
this.noEntitiesMatchingText = 'user.no-users-matching';
|
|
|
|
|
this.entityRequiredText = 'user.user-required';
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.DASHBOARD:
|
|
|
|
|
this.entityText = 'dashboard.dashboard';
|
|
|
|
|
this.noEntitiesMatchingText = 'dashboard.no-dashboards-matching';
|
|
|
|
|
this.entityRequiredText = 'dashboard.dashboard-required';
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.ALARM:
|
|
|
|
|
this.entityText = 'alarm.alarm';
|
|
|
|
|
this.noEntitiesMatchingText = 'alarm.no-alarms-matching';
|
|
|
|
|
this.entityRequiredText = 'alarm.alarm-required';
|
|
|
|
|
break;
|
|
|
|
|
case AliasEntityType.CURRENT_CUSTOMER:
|
|
|
|
|
this.entityText = 'customer.default-customer';
|
|
|
|
|
this.noEntitiesMatchingText = 'customer.no-customers-matching';
|
|
|
|
|
this.entityRequiredText = 'customer.default-customer-required';
|
|
|
|
|
break;
|
2020-08-10 15:34:23 +03:00
|
|
|
case AliasEntityType.CURRENT_USER_OWNER:
|
|
|
|
|
const authUser = getCurrentAuthUser(this.store);
|
|
|
|
|
if (authUser.authority === Authority.TENANT_ADMIN) {
|
|
|
|
|
this.entityText = 'tenant.tenant';
|
|
|
|
|
this.noEntitiesMatchingText = 'tenant.no-tenants-matching';
|
|
|
|
|
this.entityRequiredText = 'tenant.tenant-required';
|
|
|
|
|
} else {
|
2020-08-10 15:41:57 +03:00
|
|
|
this.entityText = 'customer.customer';
|
2020-08-10 15:34:23 +03:00
|
|
|
this.noEntitiesMatchingText = 'customer.no-customers-matching';
|
2020-08-10 15:41:57 +03:00
|
|
|
this.entityRequiredText = 'customer.customer-required';
|
2020-08-10 15:34:23 +03:00
|
|
|
}
|
|
|
|
|
break;
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-08 10:34:00 +03:00
|
|
|
if (this.labelText && this.labelText.length) {
|
|
|
|
|
this.entityText = this.labelText;
|
2020-09-03 10:04:03 +03:00
|
|
|
}
|
2020-09-08 10:34:00 +03:00
|
|
|
if (this.requiredText && this.requiredText.length) {
|
|
|
|
|
this.entityRequiredText = this.requiredText;
|
2020-09-03 10:04:03 +03:00
|
|
|
}
|
2019-08-14 19:55:24 +03:00
|
|
|
const currentEntity = this.getCurrentEntity();
|
|
|
|
|
if (currentEntity) {
|
|
|
|
|
const currentEntityType = currentEntity.id.entityType;
|
|
|
|
|
if (this.entityTypeValue && currentEntityType !== this.entityTypeValue) {
|
|
|
|
|
this.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getCurrentEntity(): BaseData<EntityId> | null {
|
|
|
|
|
const currentEntity = this.selectEntityFormGroup.get('entity').value;
|
|
|
|
|
if (currentEntity && typeof currentEntity !== 'string') {
|
|
|
|
|
return currentEntity as BaseData<EntityId>;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
2019-08-19 20:09:41 +03:00
|
|
|
if (this.disabled) {
|
2019-08-20 20:42:48 +03:00
|
|
|
this.selectEntityFormGroup.disable({emitEvent: false});
|
2019-08-19 20:09:41 +03:00
|
|
|
} else {
|
2019-08-20 20:42:48 +03:00
|
|
|
this.selectEntityFormGroup.enable({emitEvent: false});
|
2019-08-19 20:09:41 +03:00
|
|
|
}
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
|
2019-08-19 20:09:41 +03:00
|
|
|
writeValue(value: string | EntityId | null): void {
|
2019-08-14 19:55:24 +03:00
|
|
|
this.searchText = '';
|
|
|
|
|
if (value != null) {
|
2019-08-19 20:09:41 +03:00
|
|
|
if (typeof value === 'string') {
|
2020-04-10 15:04:12 +03:00
|
|
|
const targetEntityType = this.checkEntityType(this.entityTypeValue);
|
2020-04-17 15:12:37 +03:00
|
|
|
this.entityService.getEntity(targetEntityType, value, {ignoreLoading: true, ignoreErrors: true}).subscribe(
|
2019-08-19 20:09:41 +03:00
|
|
|
(entity) => {
|
|
|
|
|
this.modelValue = entity.id.id;
|
2019-08-20 20:42:48 +03:00
|
|
|
this.selectEntityFormGroup.get('entity').patchValue(entity, {emitEvent: false});
|
2020-04-17 15:12:37 +03:00
|
|
|
},
|
|
|
|
|
() => {
|
|
|
|
|
this.modelValue = null;
|
2020-04-17 15:13:19 +03:00
|
|
|
this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false});
|
2020-04-17 15:12:37 +03:00
|
|
|
if (value !== null) {
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
2019-08-19 20:09:41 +03:00
|
|
|
}
|
|
|
|
|
);
|
2021-03-12 14:50:58 +02:00
|
|
|
} else if (value.entityType && value.id) {
|
2020-04-10 15:04:12 +03:00
|
|
|
const targetEntityType = this.checkEntityType(value.entityType);
|
2020-04-17 15:12:37 +03:00
|
|
|
this.entityService.getEntity(targetEntityType, value.id, {ignoreLoading: true, ignoreErrors: true}).subscribe(
|
2019-08-19 20:09:41 +03:00
|
|
|
(entity) => {
|
|
|
|
|
this.modelValue = entity.id.id;
|
2019-08-20 20:42:48 +03:00
|
|
|
this.selectEntityFormGroup.get('entity').patchValue(entity, {emitEvent: false});
|
2020-04-17 15:12:37 +03:00
|
|
|
},
|
|
|
|
|
() => {
|
|
|
|
|
this.modelValue = null;
|
2020-04-17 15:13:19 +03:00
|
|
|
this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false});
|
2020-04-17 15:12:37 +03:00
|
|
|
if (value !== null) {
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
2019-08-19 20:09:41 +03:00
|
|
|
}
|
|
|
|
|
);
|
2021-03-12 14:50:58 +02:00
|
|
|
} else {
|
|
|
|
|
this.modelValue = null;
|
|
|
|
|
this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false});
|
2019-08-19 20:09:41 +03:00
|
|
|
}
|
2019-08-14 19:55:24 +03:00
|
|
|
} else {
|
|
|
|
|
this.modelValue = null;
|
2019-08-20 20:42:48 +03:00
|
|
|
this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
this.dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onFocus() {
|
|
|
|
|
if (this.dirty) {
|
|
|
|
|
this.selectEntityFormGroup.get('entity').updateValueAndValidity({onlySelf: true, emitEvent: true});
|
|
|
|
|
this.dirty = false;
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reset() {
|
2019-08-20 20:42:48 +03:00
|
|
|
this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false});
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateView(value: string | null) {
|
|
|
|
|
if (this.modelValue !== value) {
|
|
|
|
|
this.modelValue = value;
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displayEntityFn(entity?: BaseData<EntityId>): string | undefined {
|
|
|
|
|
return entity ? entity.name : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchEntities(searchText?: string): Observable<Array<BaseData<EntityId>>> {
|
|
|
|
|
this.searchText = searchText;
|
2020-04-10 15:04:12 +03:00
|
|
|
const targetEntityType = this.checkEntityType(this.entityTypeValue);
|
2019-08-14 19:55:24 +03:00
|
|
|
return this.entityService.getEntitiesByNameFilter(targetEntityType, searchText,
|
2019-11-15 12:22:14 +02:00
|
|
|
50, this.entitySubtypeValue, {ignoreLoading: true}).pipe(
|
2019-08-14 19:55:24 +03:00
|
|
|
map((data) => {
|
|
|
|
|
if (data) {
|
|
|
|
|
if (this.excludeEntityIds && this.excludeEntityIds.length) {
|
|
|
|
|
const entities: Array<BaseData<EntityId>> = [];
|
|
|
|
|
data.forEach((entity) => {
|
|
|
|
|
if (this.excludeEntityIds.indexOf(entity.id.id) === -1) {
|
|
|
|
|
entities.push(entity);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return entities;
|
|
|
|
|
} else {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear() {
|
2019-08-15 20:39:56 +03:00
|
|
|
this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: true});
|
2019-08-14 19:55:24 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
|
this.entityInput.nativeElement.blur();
|
|
|
|
|
this.entityInput.nativeElement.focus();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 15:04:12 +03:00
|
|
|
checkEntityType(entityType: EntityType | AliasEntityType): EntityType {
|
|
|
|
|
if (entityType === AliasEntityType.CURRENT_CUSTOMER) {
|
|
|
|
|
return EntityType.CUSTOMER;
|
|
|
|
|
} else if (entityType === AliasEntityType.CURRENT_TENANT) {
|
|
|
|
|
return EntityType.TENANT;
|
2020-07-02 17:30:15 +03:00
|
|
|
} else if (entityType === AliasEntityType.CURRENT_USER) {
|
|
|
|
|
return EntityType.USER;
|
2020-08-10 15:34:23 +03:00
|
|
|
} else if (entityType === AliasEntityType.CURRENT_USER_OWNER) {
|
|
|
|
|
const authUser = getCurrentAuthUser(this.store);
|
|
|
|
|
if (authUser.authority === Authority.TENANT_ADMIN) {
|
|
|
|
|
return EntityType.TENANT;
|
|
|
|
|
} else {
|
|
|
|
|
return EntityType.CUSTOMER;
|
|
|
|
|
}
|
2020-04-10 15:04:12 +03:00
|
|
|
}
|
|
|
|
|
return entityType;
|
|
|
|
|
}
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|