2019-08-14 19:55:24 +03:00
|
|
|
///
|
2025-02-25 09:39:16 +02:00
|
|
|
/// Copyright © 2016-2025 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.
|
|
|
|
|
///
|
|
|
|
|
|
2025-07-08 12:53:27 +03:00
|
|
|
import { Component, ElementRef, EventEmitter, forwardRef, Input, OnInit, Output, ViewChild } from '@angular/core';
|
2024-10-22 17:27:57 +03:00
|
|
|
import { MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field';
|
2023-07-13 11:46:04 +03:00
|
|
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
|
2025-02-07 19:53:31 +02:00
|
|
|
import { firstValueFrom, merge, Observable, of, Subject } from 'rxjs';
|
2021-08-12 19:52:43 +03:00
|
|
|
import { catchError, debounceTime, map, share, switchMap, tap } from 'rxjs/operators';
|
2020-04-10 15:04:12 +03:00
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@app/core/core.state';
|
|
|
|
|
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';
|
2020-08-10 15:34:23 +03:00
|
|
|
import { getCurrentAuthUser } from '@core/auth/auth.selectors';
|
|
|
|
|
import { Authority } from '@shared/models/authority.enum';
|
2023-11-16 12:34:42 +02:00
|
|
|
import { getEntityDetailsPageURL, isDefinedAndNotNull, isEqual } from '@core/utils';
|
2024-10-22 17:27:57 +03:00
|
|
|
import { coerceArray, coerceBoolean } from '@shared/decorators/coercion';
|
2019-08-14 19:55:24 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-entity-autocomplete',
|
|
|
|
|
templateUrl: './entity-autocomplete.component.html',
|
2023-02-27 16:24:50 +02:00
|
|
|
styleUrls: [],
|
2019-08-14 19:55:24 +03:00
|
|
|
providers: [{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => EntityAutocompleteComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}]
|
|
|
|
|
})
|
2025-02-07 19:53:31 +02:00
|
|
|
export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit {
|
2019-08-14 19:55:24 +03:00
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
selectEntityFormGroup: UntypedFormGroup;
|
2019-08-14 19:55:24 +03:00
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private modelValue: string | EntityId | null;
|
2019-08-14 19:55:24 +03:00
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private entityTypeValue: EntityType | AliasEntityType;
|
2019-08-14 19:55:24 +03:00
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private entitySubtypeValue: string;
|
2019-08-14 19:55:24 +03:00
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private entityText: string;
|
2023-03-27 14:02:48 +03:00
|
|
|
|
|
|
|
|
noEntitiesMatchingText: string;
|
2024-06-12 11:20:54 +03:00
|
|
|
notFoundEntities = 'entity.no-entities-text';
|
2023-03-27 14:02:48 +03:00
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private entityRequiredText: string;
|
2023-03-27 14:02:48 +03:00
|
|
|
|
|
|
|
|
filteredEntities: Observable<Array<BaseData<EntityId>>>;
|
|
|
|
|
|
|
|
|
|
searchText = '';
|
|
|
|
|
|
2023-11-16 12:34:42 +02:00
|
|
|
entityURL: string;
|
|
|
|
|
|
2023-03-27 14:02:48 +03:00
|
|
|
private dirty = false;
|
|
|
|
|
|
|
|
|
|
private refresh$ = new Subject<Array<BaseData<EntityId>>>();
|
|
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private propagateChange: (value: any) => void = () => { };
|
2023-03-27 14:02:48 +03:00
|
|
|
|
2019-08-14 19:55:24 +03:00
|
|
|
@Input()
|
|
|
|
|
set entityType(entityType: EntityType) {
|
|
|
|
|
if (this.entityTypeValue !== entityType) {
|
|
|
|
|
this.entityTypeValue = entityType;
|
|
|
|
|
this.load();
|
2019-08-19 20:09:41 +03:00
|
|
|
this.reset();
|
2021-08-12 19:52:43 +03:00
|
|
|
this.refresh$.next([]);
|
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();
|
2021-08-12 19:52:43 +03:00
|
|
|
this.refresh$.next([]);
|
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;
|
|
|
|
|
|
2025-01-07 20:17:04 +02:00
|
|
|
@Input()
|
|
|
|
|
placeholder: string;
|
|
|
|
|
|
2023-07-13 11:46:04 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
useFullEntityId: boolean;
|
|
|
|
|
|
2022-10-23 14:55:14 +05:00
|
|
|
@Input()
|
2023-02-17 19:24:01 +02:00
|
|
|
appearance: MatFormFieldAppearance = 'fill';
|
2022-10-23 14:55:14 +05:00
|
|
|
|
2025-01-07 20:17:04 +02:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
inlineField: boolean;
|
|
|
|
|
|
2019-08-14 19:55:24 +03:00
|
|
|
@Input()
|
2023-03-27 14:45:14 +03:00
|
|
|
@coerceBoolean()
|
|
|
|
|
required: boolean;
|
2019-08-14 19:55:24 +03:00
|
|
|
|
|
|
|
|
@Input()
|
2023-03-27 14:45:14 +03:00
|
|
|
@coerceBoolean()
|
2019-08-14 19:55:24 +03:00
|
|
|
disabled: boolean;
|
|
|
|
|
|
2024-10-22 15:35:32 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
allowCreateNew: boolean;
|
|
|
|
|
|
2024-10-22 17:27:57 +03:00
|
|
|
@Input()
|
|
|
|
|
subscriptSizing: SubscriptSizing = 'fixed';
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
@coerceArray()
|
|
|
|
|
additionalClasses: Array<string>;
|
|
|
|
|
|
2021-05-21 17:22:54 +03:00
|
|
|
@Output()
|
|
|
|
|
entityChanged = new EventEmitter<BaseData<EntityId>>();
|
|
|
|
|
|
2024-10-22 15:35:32 +03:00
|
|
|
@Output()
|
2025-08-08 18:27:42 +03:00
|
|
|
createNew = new EventEmitter<string>();
|
2024-10-22 15:35:32 +03:00
|
|
|
|
2019-08-14 19:55:24 +03:00
|
|
|
@ViewChild('entityInput', {static: true}) entityInput: ElementRef;
|
|
|
|
|
|
2023-03-27 14:02:48 +03:00
|
|
|
get requiredErrorText(): string {
|
|
|
|
|
if (this.requiredText && this.requiredText.length) {
|
|
|
|
|
return this.requiredText;
|
|
|
|
|
}
|
|
|
|
|
return this.entityRequiredText;
|
|
|
|
|
}
|
2019-08-20 20:42:48 +03:00
|
|
|
|
2023-03-27 14:02:48 +03:00
|
|
|
get label(): string {
|
|
|
|
|
if (this.labelText && this.labelText.length) {
|
|
|
|
|
return this.labelText;
|
|
|
|
|
}
|
|
|
|
|
return this.entityText;
|
|
|
|
|
}
|
2021-08-12 19:52:43 +03:00
|
|
|
|
2019-08-14 19:55:24 +03:00
|
|
|
|
|
|
|
|
constructor(private store: Store<AppState>,
|
|
|
|
|
private entityService: EntityService,
|
2023-02-02 15:55:06 +02:00
|
|
|
private fb: UntypedFormBuilder) {
|
2019-08-14 19:55:24 +03:00
|
|
|
this.selectEntityFormGroup = this.fb.group({
|
|
|
|
|
entity: [null]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
registerOnTouched(_fn: any): void {
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
2021-08-12 19:52:43 +03:00
|
|
|
this.filteredEntities = merge(
|
|
|
|
|
this.refresh$.asObservable(),
|
|
|
|
|
this.selectEntityFormGroup.get('entity').valueChanges
|
|
|
|
|
.pipe(
|
|
|
|
|
debounceTime(150),
|
|
|
|
|
tap(value => {
|
2025-02-07 19:53:31 +02:00
|
|
|
let modelValue: string | EntityId;
|
2021-08-12 19:52:43 +03:00
|
|
|
if (typeof value === 'string' || !value) {
|
|
|
|
|
modelValue = null;
|
|
|
|
|
} else {
|
2023-07-13 11:46:04 +03:00
|
|
|
modelValue = this.useFullEntityId ? value.id : value.id.id;
|
2021-08-12 19:52:43 +03:00
|
|
|
}
|
|
|
|
|
this.updateView(modelValue, value);
|
|
|
|
|
if (value === null) {
|
|
|
|
|
this.clear();
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
// startWith<string | BaseData<EntityId>>(''),
|
2024-06-05 15:31:39 +03:00
|
|
|
map(value => value ? (typeof value === 'string' ? value : value.name) : ''),
|
2021-08-12 19:52:43 +03:00
|
|
|
switchMap(name => this.fetchEntities(name)),
|
|
|
|
|
share()
|
|
|
|
|
)
|
|
|
|
|
);
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private load(): void {
|
2019-08-14 19:55:24 +03:00
|
|
|
if (this.entityTypeValue) {
|
|
|
|
|
switch (this.entityTypeValue) {
|
|
|
|
|
case EntityType.ASSET:
|
|
|
|
|
this.entityText = 'asset.asset';
|
|
|
|
|
this.noEntitiesMatchingText = 'asset.no-assets-matching';
|
|
|
|
|
this.entityRequiredText = 'asset.asset-required';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'asset.no-assets-text';
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.DEVICE:
|
|
|
|
|
this.entityText = 'device.device';
|
|
|
|
|
this.noEntitiesMatchingText = 'device.no-devices-matching';
|
|
|
|
|
this.entityRequiredText = 'device.device-required';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'device.no-devices-text';
|
2019-08-14 19:55:24 +03:00
|
|
|
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';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'edge.no-edges-text';
|
2020-06-21 20:38:43 +03:00
|
|
|
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';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'entity-view.no-entity-views-text';
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.RULE_CHAIN:
|
|
|
|
|
this.entityText = 'rulechain.rulechain';
|
|
|
|
|
this.noEntitiesMatchingText = 'rulechain.no-rulechains-matching';
|
|
|
|
|
this.entityRequiredText = 'rulechain.rulechain-required';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'rulechain.no-rulechains-text';
|
2019-08-14 19:55:24 +03:00
|
|
|
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';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'tenant.no-tenants-text';
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.CUSTOMER:
|
|
|
|
|
this.entityText = 'customer.customer';
|
|
|
|
|
this.noEntitiesMatchingText = 'customer.no-customers-matching';
|
|
|
|
|
this.entityRequiredText = 'customer.customer-required';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'customer.no-customers-text';
|
2019-08-14 19:55:24 +03:00
|
|
|
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';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'user.no-users-text';
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.DASHBOARD:
|
|
|
|
|
this.entityText = 'dashboard.dashboard';
|
|
|
|
|
this.noEntitiesMatchingText = 'dashboard.no-dashboards-matching';
|
|
|
|
|
this.entityRequiredText = 'dashboard.dashboard-required';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'dashboard.no-dashboards-text';
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.ALARM:
|
|
|
|
|
this.entityText = 'alarm.alarm';
|
|
|
|
|
this.noEntitiesMatchingText = 'alarm.no-alarms-matching';
|
|
|
|
|
this.entityRequiredText = 'alarm.alarm-required';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'alarm.no-alarms-prompt';
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
2024-06-04 13:18:18 +03:00
|
|
|
case EntityType.QUEUE_STATS:
|
|
|
|
|
this.entityText = 'queue-statistics.queue-statistics';
|
|
|
|
|
this.noEntitiesMatchingText = 'queue-statistics.no-queue-statistics-matching';
|
|
|
|
|
this.entityRequiredText = 'queue-statistics.queue-statistics-required';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'queue-statistics.no-queue-statistics-text';
|
2024-06-04 13:18:18 +03:00
|
|
|
break;
|
2024-10-22 15:35:32 +03:00
|
|
|
case EntityType.MOBILE_APP:
|
2024-10-22 17:27:57 +03:00
|
|
|
this.entityText = 'mobile.application';
|
2024-10-22 15:35:32 +03:00
|
|
|
this.noEntitiesMatchingText = 'mobile.no-application-matching';
|
|
|
|
|
this.entityRequiredText = 'mobile.application-required';
|
|
|
|
|
this.notFoundEntities = 'mobile.no-application-text';
|
|
|
|
|
break;
|
2024-10-22 17:27:57 +03:00
|
|
|
case EntityType.MOBILE_APP_BUNDLE:
|
|
|
|
|
this.entityText = 'mobile.bundle';
|
|
|
|
|
this.noEntitiesMatchingText = 'mobile.no-bundle-matching';
|
|
|
|
|
this.entityRequiredText = 'mobile.bundle-required';
|
|
|
|
|
this.notFoundEntities = 'mobile.no-bundle-text';
|
|
|
|
|
break;
|
2024-12-20 12:20:49 +02:00
|
|
|
case EntityType.NOTIFICATION_TARGET:
|
|
|
|
|
this.entityText = 'notification.notification-recipient';
|
|
|
|
|
this.noEntitiesMatchingText = 'notification.no-recipients-matching';
|
|
|
|
|
this.entityRequiredText = 'notification.notification-recipient-required';
|
|
|
|
|
this.notFoundEntities = 'notification.no-recipients-text';
|
|
|
|
|
break;
|
2025-07-09 18:07:02 +03:00
|
|
|
case EntityType.AI_MODEL:
|
2025-07-08 12:53:27 +03:00
|
|
|
this.entityText = 'ai-models.ai-model';
|
|
|
|
|
this.noEntitiesMatchingText = 'ai-models.no-model-matching';
|
|
|
|
|
this.entityRequiredText = 'ai-models.model-required';
|
|
|
|
|
this.notFoundEntities = 'ai-models.no-model-text';
|
|
|
|
|
break;
|
2019-08-14 19:55:24 +03:00
|
|
|
case AliasEntityType.CURRENT_CUSTOMER:
|
|
|
|
|
this.entityText = 'customer.default-customer';
|
|
|
|
|
this.noEntitiesMatchingText = 'customer.no-customers-matching';
|
|
|
|
|
this.entityRequiredText = 'customer.default-customer-required';
|
2024-06-12 11:20:54 +03:00
|
|
|
this.notFoundEntities = 'customer.no-customers-text';
|
2019-08-14 19:55:24 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const currentEntity = this.getCurrentEntity();
|
|
|
|
|
if (currentEntity) {
|
|
|
|
|
const currentEntityType = currentEntity.id.entityType;
|
|
|
|
|
if (this.entityTypeValue && currentEntityType !== this.entityTypeValue) {
|
|
|
|
|
this.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private getCurrentEntity(): BaseData<EntityId> | null {
|
2019-08-14 19:55:24 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-05-27 18:33:06 +03:00
|
|
|
async writeValue(value: string | EntityId | null): Promise<void> {
|
2019-08-14 19:55:24 +03:00
|
|
|
this.searchText = '';
|
2023-05-25 17:03:52 +03:00
|
|
|
if (isDefinedAndNotNull(value) && (typeof value === 'string' || (value.entityType && value.id))) {
|
2021-05-27 18:33:06 +03:00
|
|
|
let targetEntityType: EntityType;
|
|
|
|
|
let id: string;
|
2019-08-19 20:09:41 +03:00
|
|
|
if (typeof value === 'string') {
|
2021-05-27 18:33:06 +03:00
|
|
|
targetEntityType = this.checkEntityType(this.entityTypeValue);
|
|
|
|
|
id = value;
|
2021-03-12 14:50:58 +02:00
|
|
|
} else {
|
2021-05-27 18:33:06 +03:00
|
|
|
targetEntityType = this.checkEntityType(value.entityType);
|
|
|
|
|
id = value.id;
|
|
|
|
|
}
|
|
|
|
|
let entity: BaseData<EntityId> = null;
|
|
|
|
|
try {
|
2025-02-07 19:53:31 +02:00
|
|
|
entity = await firstValueFrom(this.entityService.getEntity(targetEntityType, id, {ignoreLoading: true, ignoreErrors: true}));
|
2021-05-27 18:33:06 +03:00
|
|
|
} catch (e) {
|
|
|
|
|
this.propagateChange(null);
|
2019-08-19 20:09:41 +03:00
|
|
|
}
|
2023-07-13 11:46:04 +03:00
|
|
|
this.modelValue = entity !== null ? (this.useFullEntityId ? entity.id : entity.id.id) : null;
|
2025-02-07 19:53:31 +02:00
|
|
|
this.entityURL = !entity ? '' : getEntityDetailsPageURL(entity.id.id, targetEntityType);
|
2021-05-27 18:33:06 +03:00
|
|
|
this.selectEntityFormGroup.get('entity').patchValue(entity !== null ? entity : '', {emitEvent: false});
|
|
|
|
|
this.entityChanged.emit(entity);
|
2019-08-14 19:55:24 +03:00
|
|
|
} else {
|
|
|
|
|
this.modelValue = null;
|
2025-02-07 19:53:31 +02:00
|
|
|
this.entityURL = '';
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private reset() {
|
2019-08-20 20:42:48 +03:00
|
|
|
this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false});
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private updateView(value: string | EntityId | null, entity: BaseData<EntityId> | null) {
|
2021-05-21 17:22:54 +03:00
|
|
|
if (!isEqual(this.modelValue, value)) {
|
2019-08-14 19:55:24 +03:00
|
|
|
this.modelValue = value;
|
2025-03-12 13:21:06 +02:00
|
|
|
this.entityURL = (typeof entity === 'string' || !entity) ? '' : getEntityDetailsPageURL(entity.id.id, entity.id.entityType as EntityType);
|
2019-08-14 19:55:24 +03:00
|
|
|
this.propagateChange(this.modelValue);
|
2021-05-21 17:22:54 +03:00
|
|
|
this.entityChanged.emit(entity);
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displayEntityFn(entity?: BaseData<EntityId>): string | undefined {
|
|
|
|
|
return entity ? entity.name : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private fetchEntities(searchText?: string): Observable<Array<BaseData<EntityId>>> {
|
2019-08-14 19:55:24 +03:00
|
|
|
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(
|
2021-06-24 01:40:10 +03:00
|
|
|
catchError(() => of(null)),
|
2019-08-14 19:55:24 +03:00
|
|
|
map((data) => {
|
|
|
|
|
if (data) {
|
|
|
|
|
if (this.excludeEntityIds && this.excludeEntityIds.length) {
|
2023-01-11 23:02:27 +05:00
|
|
|
const excludeEntityIdsSet = new Set(this.excludeEntityIds);
|
2019-08-14 19:55:24 +03:00
|
|
|
const entities: Array<BaseData<EntityId>> = [];
|
2023-01-11 23:02:27 +05:00
|
|
|
data.forEach(entity => !excludeEntityIdsSet.has(entity.id.id) && entities.push(entity));
|
2019-08-14 19:55:24 +03:00
|
|
|
return entities;
|
|
|
|
|
} else {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 11:20:54 +03:00
|
|
|
textIsNotEmpty(text: string): boolean {
|
|
|
|
|
return (text && text.length > 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 19:55:24 +03:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 19:53:31 +02:00
|
|
|
private checkEntityType(entityType: EntityType | AliasEntityType): EntityType {
|
2020-04-10 15:04:12 +03:00
|
|
|
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;
|
|
|
|
|
}
|
2024-10-22 15:35:32 +03:00
|
|
|
|
2025-08-08 18:27:42 +03:00
|
|
|
createNewEntity($event: Event, searchText?: string) {
|
2024-10-22 15:35:32 +03:00
|
|
|
$event.stopPropagation();
|
2025-08-08 18:27:42 +03:00
|
|
|
this.createNew.emit(searchText);
|
2024-10-22 15:35:32 +03:00
|
|
|
}
|
2025-02-07 19:53:31 +02:00
|
|
|
|
|
|
|
|
get showEntityLink(): boolean {
|
|
|
|
|
return this.selectEntityFormGroup.get('entity').value && this.disabled && this.entityURL !== '';
|
|
|
|
|
}
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|