2019-08-15 20:39:56 +03:00
|
|
|
///
|
2024-01-09 10:46:16 +02:00
|
|
|
/// Copyright © 2016-2024 The Thingsboard Authors
|
2019-08-15 20:39:56 +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.
|
|
|
|
|
///
|
|
|
|
|
|
2019-08-27 20:07:09 +03:00
|
|
|
import {
|
|
|
|
|
AfterViewInit,
|
|
|
|
|
Component,
|
|
|
|
|
ElementRef,
|
|
|
|
|
forwardRef,
|
2020-04-27 09:27:14 +03:00
|
|
|
Input,
|
|
|
|
|
OnChanges,
|
|
|
|
|
OnInit,
|
|
|
|
|
SimpleChanges,
|
2019-08-27 20:07:09 +03:00
|
|
|
ViewChild
|
|
|
|
|
} from '@angular/core';
|
2024-07-11 11:59:03 +03:00
|
|
|
import {
|
|
|
|
|
ControlValueAccessor,
|
|
|
|
|
NG_VALIDATORS,
|
|
|
|
|
NG_VALUE_ACCESSOR,
|
|
|
|
|
UntypedFormBuilder,
|
|
|
|
|
UntypedFormGroup,
|
|
|
|
|
ValidationErrors,
|
|
|
|
|
Validators
|
|
|
|
|
} from '@angular/forms';
|
2020-04-27 09:27:14 +03:00
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
import { filter, map, mergeMap, share, tap } from 'rxjs/operators';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { 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 { MatAutocomplete } from '@angular/material/autocomplete';
|
2023-02-17 19:24:01 +02:00
|
|
|
import { MatChipGrid } from '@angular/material/chips';
|
2019-08-15 20:39:56 +03:00
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
2023-03-07 12:14:49 +02:00
|
|
|
import { SubscriptSizing } from '@angular/material/form-field';
|
2024-08-02 12:37:22 +03:00
|
|
|
import { coerceBoolean } from '@shared/decorators/coercion';
|
2019-08-15 20:39:56 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-entity-list',
|
|
|
|
|
templateUrl: './entity-list.component.html',
|
2023-02-21 19:18:04 +02:00
|
|
|
styleUrls: [],
|
2019-08-15 20:39:56 +03:00
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => EntityListComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
2019-10-10 13:00:29 +03:00
|
|
|
export class EntityListComponent implements ControlValueAccessor, OnInit, AfterViewInit, OnChanges {
|
2019-08-15 20:39:56 +03:00
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
entityListFormGroup: UntypedFormGroup;
|
2019-08-15 20:39:56 +03:00
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
private modelValue: Array<string> | null;
|
2019-08-15 20:39:56 +03:00
|
|
|
|
|
|
|
|
@Input()
|
2019-10-10 13:00:29 +03:00
|
|
|
entityType: EntityType;
|
2019-08-15 20:39:56 +03:00
|
|
|
|
2021-01-10 01:27:49 +02:00
|
|
|
@Input()
|
|
|
|
|
subType: string;
|
|
|
|
|
|
2023-01-17 18:26:25 +02:00
|
|
|
@Input()
|
|
|
|
|
labelText: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
2023-02-01 15:58:00 +02:00
|
|
|
placeholderText = this.translate.instant('entity.entity-list');
|
2023-01-17 18:26:25 +02:00
|
|
|
|
|
|
|
|
@Input()
|
2023-02-01 15:58:00 +02:00
|
|
|
requiredText = this.translate.instant('entity.entity-list-empty');
|
2023-01-17 18:26:25 +02:00
|
|
|
|
2019-08-15 20:39:56 +03:00
|
|
|
private requiredValue: boolean;
|
|
|
|
|
get required(): boolean {
|
|
|
|
|
return this.requiredValue;
|
|
|
|
|
}
|
|
|
|
|
@Input()
|
|
|
|
|
set required(value: boolean) {
|
2019-08-27 20:07:09 +03:00
|
|
|
const newVal = coerceBooleanProperty(value);
|
|
|
|
|
if (this.requiredValue !== newVal) {
|
|
|
|
|
this.requiredValue = newVal;
|
|
|
|
|
this.updateValidators();
|
|
|
|
|
}
|
2019-08-15 20:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
2023-03-07 12:14:49 +02:00
|
|
|
@Input()
|
2023-03-20 16:51:42 +02:00
|
|
|
subscriptSizing: SubscriptSizing = 'fixed';
|
2023-03-07 12:14:49 +02:00
|
|
|
|
|
|
|
|
@Input()
|
2023-03-20 16:51:42 +02:00
|
|
|
hint: string;
|
2023-03-07 12:14:49 +02:00
|
|
|
|
2024-08-02 12:37:22 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
2024-08-05 09:38:48 +03:00
|
|
|
syncedIdListPropagator = false;
|
2024-08-02 12:37:22 +03:00
|
|
|
|
2020-02-10 13:10:14 +02:00
|
|
|
@ViewChild('entityInput') entityInput: ElementRef<HTMLInputElement>;
|
|
|
|
|
@ViewChild('entityAutocomplete') matAutocomplete: MatAutocomplete;
|
2023-02-17 19:24:01 +02:00
|
|
|
@ViewChild('chipList', {static: true}) chipList: MatChipGrid;
|
2019-08-15 20:39:56 +03:00
|
|
|
|
|
|
|
|
entities: Array<BaseData<EntityId>> = [];
|
|
|
|
|
filteredEntities: Observable<Array<BaseData<EntityId>>>;
|
|
|
|
|
|
2019-12-23 14:36:44 +02:00
|
|
|
searchText = '';
|
2019-08-15 20:39:56 +03:00
|
|
|
|
2019-08-28 16:02:27 +03:00
|
|
|
private dirty = false;
|
|
|
|
|
|
2019-08-15 20:39:56 +03:00
|
|
|
private propagateChange = (v: any) => { };
|
|
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
constructor(public translate: TranslateService,
|
2019-08-15 20:39:56 +03:00
|
|
|
private entityService: EntityService,
|
2023-02-02 15:55:06 +02:00
|
|
|
private fb: UntypedFormBuilder) {
|
2019-08-15 20:39:56 +03:00
|
|
|
this.entityListFormGroup = this.fb.group({
|
2024-07-11 12:20:10 +03:00
|
|
|
entities: [this.entities],
|
2019-08-15 20:39:56 +03:00
|
|
|
entity: [null]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
private updateValidators() {
|
2019-08-27 20:07:09 +03:00
|
|
|
this.entityListFormGroup.get('entities').setValidators(this.required ? [Validators.required] : []);
|
|
|
|
|
this.entityListFormGroup.get('entities').updateValueAndValidity();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 20:39:56 +03:00
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
this.filteredEntities = this.entityListFormGroup.get('entity').valueChanges
|
|
|
|
|
.pipe(
|
2019-08-28 16:02:27 +03:00
|
|
|
// startWith<string | BaseData<EntityId>>(''),
|
2019-08-15 20:39:56 +03:00
|
|
|
tap((value) => {
|
|
|
|
|
if (value && typeof value !== 'string') {
|
|
|
|
|
this.add(value);
|
|
|
|
|
} else if (value === null) {
|
|
|
|
|
this.clear(this.entityInput.nativeElement.value);
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
filter((value) => typeof value === 'string'),
|
|
|
|
|
map((value) => value ? (typeof value === 'string' ? value : value.name) : ''),
|
|
|
|
|
mergeMap(name => this.fetchEntities(name) ),
|
|
|
|
|
share()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 13:00:29 +03:00
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
|
for (const propName of Object.keys(changes)) {
|
|
|
|
|
const change = changes[propName];
|
|
|
|
|
if (!change.firstChange && change.currentValue !== change.previousValue) {
|
|
|
|
|
if (propName === 'entityType') {
|
|
|
|
|
this.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 20:07:09 +03:00
|
|
|
ngAfterViewInit(): void {
|
|
|
|
|
}
|
2019-08-15 20:39:56 +03:00
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
2019-08-27 20:07:09 +03:00
|
|
|
if (isDisabled) {
|
2019-08-28 16:02:27 +03:00
|
|
|
this.entityListFormGroup.disable({emitEvent: false});
|
2019-08-19 20:09:41 +03:00
|
|
|
} else {
|
2019-08-28 16:02:27 +03:00
|
|
|
this.entityListFormGroup.enable({emitEvent: false});
|
2019-08-19 20:09:41 +03:00
|
|
|
}
|
2019-08-15 20:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: Array<string> | null): void {
|
|
|
|
|
this.searchText = '';
|
2019-08-27 20:07:09 +03:00
|
|
|
if (value != null && value.length > 0) {
|
2019-08-20 20:42:48 +03:00
|
|
|
this.modelValue = [...value];
|
2019-10-10 13:00:29 +03:00
|
|
|
this.entityService.getEntities(this.entityType, value).subscribe(
|
2019-08-15 20:39:56 +03:00
|
|
|
(entities) => {
|
|
|
|
|
this.entities = entities;
|
2019-08-27 20:07:09 +03:00
|
|
|
this.entityListFormGroup.get('entities').setValue(this.entities);
|
2024-08-05 10:21:51 +03:00
|
|
|
if (this.syncedIdListPropagator && this.modelValue.length !== entities.length) {
|
|
|
|
|
this.modelValue = entities.map(entity => entity.id.id);
|
2024-08-02 12:37:22 +03:00
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
2019-08-15 20:39:56 +03:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
this.entities = [];
|
2019-08-27 20:07:09 +03:00
|
|
|
this.entityListFormGroup.get('entities').setValue(this.entities);
|
2019-08-15 20:39:56 +03:00
|
|
|
this.modelValue = null;
|
|
|
|
|
}
|
2019-08-28 16:02:27 +03:00
|
|
|
this.dirty = true;
|
2019-08-15 20:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
private reset() {
|
2019-08-15 20:39:56 +03:00
|
|
|
this.entities = [];
|
2019-08-27 20:07:09 +03:00
|
|
|
this.entityListFormGroup.get('entities').setValue(this.entities);
|
2019-08-15 20:39:56 +03:00
|
|
|
this.modelValue = null;
|
2019-08-28 16:02:27 +03:00
|
|
|
if (this.entityInput) {
|
|
|
|
|
this.entityInput.nativeElement.value = '';
|
|
|
|
|
}
|
|
|
|
|
this.entityListFormGroup.get('entity').patchValue('', {emitEvent: false});
|
2019-08-15 20:39:56 +03:00
|
|
|
this.propagateChange(this.modelValue);
|
2019-08-28 16:02:27 +03:00
|
|
|
this.dirty = true;
|
2019-08-15 20:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
private add(entity: BaseData<EntityId>): void {
|
2019-08-15 20:39:56 +03:00
|
|
|
if (!this.modelValue || this.modelValue.indexOf(entity.id.id) === -1) {
|
|
|
|
|
if (!this.modelValue) {
|
|
|
|
|
this.modelValue = [];
|
|
|
|
|
}
|
|
|
|
|
this.modelValue.push(entity.id.id);
|
|
|
|
|
this.entities.push(entity);
|
2019-08-27 20:07:09 +03:00
|
|
|
this.entityListFormGroup.get('entities').setValue(this.entities);
|
2019-08-15 20:39:56 +03:00
|
|
|
}
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
this.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
public remove(entity: BaseData<EntityId>) {
|
2020-04-27 09:26:17 +03:00
|
|
|
let index = this.entities.indexOf(entity);
|
2019-08-15 20:39:56 +03:00
|
|
|
if (index >= 0) {
|
|
|
|
|
this.entities.splice(index, 1);
|
2019-08-27 20:07:09 +03:00
|
|
|
this.entityListFormGroup.get('entities').setValue(this.entities);
|
2020-04-27 09:26:17 +03:00
|
|
|
index = this.modelValue.indexOf(entity.id.id);
|
2019-08-15 20:39:56 +03:00
|
|
|
this.modelValue.splice(index, 1);
|
|
|
|
|
if (!this.modelValue.length) {
|
|
|
|
|
this.modelValue = null;
|
|
|
|
|
}
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
this.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
public displayEntityFn(entity?: BaseData<EntityId>): string | undefined {
|
2019-08-15 20:39:56 +03:00
|
|
|
return entity ? entity.name : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
private fetchEntities(searchText?: string): Observable<Array<BaseData<EntityId>>> {
|
2019-08-15 20:39:56 +03:00
|
|
|
this.searchText = searchText;
|
2021-01-10 01:27:49 +02:00
|
|
|
|
2019-10-10 13:00:29 +03:00
|
|
|
return this.entityService.getEntitiesByNameFilter(this.entityType, searchText,
|
2021-01-10 01:27:49 +02:00
|
|
|
50, this.subType ? this.subType : '', {ignoreLoading: true}).pipe(
|
2019-08-15 20:39:56 +03:00
|
|
|
map((data) => data ? data : []));
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
public onFocus() {
|
2019-08-28 16:02:27 +03:00
|
|
|
if (this.dirty) {
|
|
|
|
|
this.entityListFormGroup.get('entity').updateValueAndValidity({onlySelf: true, emitEvent: true});
|
|
|
|
|
this.dirty = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
private clear(value: string = '') {
|
2019-08-15 20:39:56 +03:00
|
|
|
this.entityInput.nativeElement.value = value;
|
|
|
|
|
this.entityListFormGroup.get('entity').patchValue(value, {emitEvent: true});
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.entityInput.nativeElement.blur();
|
|
|
|
|
this.entityInput.nativeElement.focus();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 12:20:10 +03:00
|
|
|
public textIsNotEmpty(text: string): boolean {
|
2024-06-12 11:20:54 +03:00
|
|
|
return (text && text.length > 0);
|
|
|
|
|
}
|
2019-08-15 20:39:56 +03:00
|
|
|
}
|