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

209 lines
5.8 KiB
TypeScript
Raw Normal View History

2019-08-19 20:09:41 +03:00
///
2023-01-31 10:43:56 +02:00
/// Copyright © 2016-2023 The Thingsboard Authors
2019-08-19 20:09:41 +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-02-26 18:15:04 +02:00
import { COMMA, ENTER, SEMICOLON } from '@angular/cdk/keycodes';
import { AfterViewInit, Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core';
2023-02-02 15:55:06 +02:00
import { ControlValueAccessor, UntypedFormBuilder, UntypedFormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
2020-02-26 18:15:04 +02:00
import { Observable, of } from 'rxjs';
import { map, mergeMap, share } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { AppState } from '@app/core/core.state';
import { TranslateService } from '@ngx-translate/core';
import { EntityId } from '@shared/models/id/entity-id';
import { EntityService } from '@core/http/entity.service';
2023-02-17 19:24:01 +02:00
import { MatAutocomplete, MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
import { MatChipInputEvent, MatChipGrid } from '@angular/material/chips';
2019-08-19 20:09:41 +03:00
import { coerceBooleanProperty } from '@angular/cdk/coercion';
2020-02-26 18:15:04 +02:00
import { DataKeyType } from '@shared/models/telemetry/telemetry.models';
import { isEqual } from '@core/utils';
2019-08-19 20:09:41 +03:00
@Component({
selector: 'tb-entity-keys-list',
templateUrl: './entity-keys-list.component.html',
2023-02-21 19:18:04 +02:00
styleUrls: [],
2019-08-19 20:09:41 +03:00
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => EntityKeysListComponent),
multi: true
}
]
})
export class EntityKeysListComponent implements ControlValueAccessor, OnInit, AfterViewInit {
2023-02-02 15:55:06 +02:00
keysListFormGroup: UntypedFormGroup;
2019-08-19 20:09:41 +03:00
modelValue: Array<string> | null;
entityIdValue: EntityId;
@Input()
set entityId(entityId: EntityId) {
2020-02-26 18:15:04 +02:00
if (!isEqual(this.entityIdValue, entityId)) {
2019-08-19 20:09:41 +03:00
this.entityIdValue = entityId;
this.dirty = true;
2019-08-19 20:09:41 +03:00
}
}
@Input()
keysText: string;
@Input()
dataKeyType: DataKeyType;
private requiredValue: boolean;
get required(): boolean {
return this.requiredValue;
}
@Input()
set required(value: boolean) {
this.requiredValue = coerceBooleanProperty(value);
}
@Input()
disabled: boolean;
2020-02-10 13:10:14 +02:00
@ViewChild('keyInput') keyInput: ElementRef<HTMLInputElement>;
@ViewChild('keyAutocomplete') matAutocomplete: MatAutocomplete;
2023-02-17 19:24:01 +02:00
@ViewChild('chipList') chipList: MatChipGrid;
2019-08-19 20:09:41 +03:00
filteredKeys: Observable<Array<string>>;
separatorKeysCodes: number[] = [ENTER, COMMA, SEMICOLON];
2019-12-23 14:36:44 +02:00
searchText = '';
2019-08-19 20:09:41 +03:00
private dirty = false;
2019-08-19 20:09:41 +03:00
private propagateChange = (v: any) => { };
constructor(private store: Store<AppState>,
public translate: TranslateService,
private entityService: EntityService,
2023-02-02 15:55:06 +02:00
private fb: UntypedFormBuilder) {
2019-08-19 20:09:41 +03:00
this.keysListFormGroup = this.fb.group({
key: [null]
});
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {
}
ngOnInit() {
this.filteredKeys = this.keysListFormGroup.get('key').valueChanges
.pipe(
map((value) => value ? value : ''),
mergeMap(name => this.fetchKeys(name) ),
share()
);
}
ngAfterViewInit(): void {}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
if (this.disabled) {
this.keysListFormGroup.disable({emitEvent: false});
2019-08-19 20:09:41 +03:00
} else {
this.keysListFormGroup.enable({emitEvent: false});
2019-08-19 20:09:41 +03:00
}
}
writeValue(value: Array<string> | null): void {
this.searchText = '';
if (value != null) {
this.modelValue = [...value];
} else {
this.modelValue = [];
}
}
onFocus() {
if (this.dirty) {
this.keysListFormGroup.get('key').updateValueAndValidity({onlySelf: true, emitEvent: true});
this.dirty = false;
}
2019-08-19 20:09:41 +03:00
}
addKey(key: string): void {
if (!this.modelValue || this.modelValue.indexOf(key) === -1) {
if (!this.modelValue) {
this.modelValue = [];
}
this.modelValue.push(key);
if (this.required) {
this.chipList.errorState = false;
}
}
this.propagateChange(this.modelValue);
}
add(event: MatChipInputEvent): void {
if (!this.matAutocomplete.isOpen) {
const value = (event.value || '').trim();
if (value) {
this.addKey(value);
2019-08-19 20:09:41 +03:00
}
this.clear('', document.activeElement === this.keyInput.nativeElement);
2019-08-19 20:09:41 +03:00
}
}
remove(key: string) {
const index = this.modelValue.indexOf(key);
if (index >= 0) {
this.modelValue.splice(index, 1);
if (!this.modelValue.length) {
if (this.required) {
this.chipList.errorState = true;
}
}
this.propagateChange(this.modelValue.length ? this.modelValue : null);
}
}
selected(event: MatAutocompleteSelectedEvent): void {
this.addKey(event.option.viewValue);
this.clear('');
}
displayKeyFn(key?: string): string | undefined {
return key ? key : undefined;
}
fetchKeys(searchText?: string): Observable<Array<string>> {
this.searchText = searchText;
return this.entityIdValue ? this.entityService.getEntityKeys(this.entityIdValue, searchText,
this.dataKeyType, {ignoreLoading: true}).pipe(
2019-08-19 20:09:41 +03:00
map((data) => data ? data : [])) : of([]);
}
clear(value: string = '', emitEvent = true) {
2019-08-19 20:09:41 +03:00
this.keyInput.nativeElement.value = value;
this.keysListFormGroup.get('key').patchValue(null, {emitEvent});
if (emitEvent) {
setTimeout(() => {
this.keyInput.nativeElement.blur();
this.keyInput.nativeElement.focus();
}, 0);
}
2019-08-19 20:09:41 +03:00
}
}