thingsboard/ui-ngx/src/app/shared/components/entity/entity-list.component.ts

245 lines
7.2 KiB
TypeScript
Raw Normal View History

2019-08-15 20:39:56 +03:00
///
2021-01-11 13:42:16 +02:00
/// Copyright © 2016-2021 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,
Input,
OnChanges,
OnInit,
SimpleChanges,
2019-08-27 20:07:09 +03:00
ViewChild
} from '@angular/core';
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
import { Observable } from 'rxjs';
import { filter, 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 { 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';
2020-02-10 13:15:29 +02:00
import { MatChipList } from '@angular/material/chips';
2019-08-15 20:39:56 +03:00
import { coerceBooleanProperty } from '@angular/cdk/coercion';
@Component({
selector: 'tb-entity-list',
templateUrl: './entity-list.component.html',
styleUrls: ['./entity-list.component.scss'],
2019-08-15 20:39:56 +03:00
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => EntityListComponent),
multi: true
}
]
})
export class EntityListComponent implements ControlValueAccessor, OnInit, AfterViewInit, OnChanges {
2019-08-15 20:39:56 +03:00
entityListFormGroup: FormGroup;
modelValue: Array<string> | null;
@Input()
entityType: EntityType;
2019-08-15 20:39:56 +03:00
@Input()
subType: string;
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;
2020-02-10 13:10:14 +02:00
@ViewChild('entityInput') entityInput: ElementRef<HTMLInputElement>;
@ViewChild('entityAutocomplete') matAutocomplete: MatAutocomplete;
2019-08-27 20:07:09 +03:00
@ViewChild('chipList', {static: true}) chipList: MatChipList;
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
private dirty = false;
2019-08-15 20:39:56 +03:00
private propagateChange = (v: any) => { };
constructor(private store: Store<AppState>,
public translate: TranslateService,
private entityService: EntityService,
private fb: FormBuilder) {
this.entityListFormGroup = this.fb.group({
2019-08-27 20:07:09 +03:00
entities: [this.entities, this.required ? [Validators.required] : []],
2019-08-15 20:39:56 +03:00
entity: [null]
});
}
2019-08-27 20:07:09 +03:00
updateValidators() {
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(
// 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()
);
}
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) {
this.entityListFormGroup.disable({emitEvent: false});
2019-08-19 20:09:41 +03:00
} else {
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) {
this.modelValue = [...value];
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);
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;
}
this.dirty = true;
2019-08-15 20:39:56 +03:00
}
reset() {
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;
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);
this.dirty = true;
2019-08-15 20:39:56 +03:00
}
add(entity: BaseData<EntityId>): void {
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();
}
remove(entity: BaseData<EntityId>) {
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);
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();
}
}
displayEntityFn(entity?: BaseData<EntityId>): string | undefined {
return entity ? entity.name : undefined;
}
fetchEntities(searchText?: string): Observable<Array<BaseData<EntityId>>> {
this.searchText = searchText;
return this.entityService.getEntitiesByNameFilter(this.entityType, searchText,
50, this.subType ? this.subType : '', {ignoreLoading: true}).pipe(
2019-08-15 20:39:56 +03:00
map((data) => data ? data : []));
}
onFocus() {
if (this.dirty) {
this.entityListFormGroup.get('entity').updateValueAndValidity({onlySelf: true, emitEvent: true});
this.dirty = false;
}
}
2019-08-15 20:39:56 +03:00
clear(value: string = '') {
this.entityInput.nativeElement.value = value;
this.entityListFormGroup.get('entity').patchValue(value, {emitEvent: true});
setTimeout(() => {
this.entityInput.nativeElement.blur();
this.entityInput.nativeElement.focus();
}, 0);
}
}