2019-08-19 20:09:41 +03:00
|
|
|
///
|
2021-01-11 13:42:16 +02:00
|
|
|
/// Copyright © 2016-2021 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';
|
|
|
|
|
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
|
|
|
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';
|
2020-02-10 13:15:29 +02:00
|
|
|
import { MatAutocomplete, MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
2020-02-26 18:15:04 +02:00
|
|
|
import { MatChipInputEvent, MatChipList } 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',
|
2019-08-21 13:36:39 +03:00
|
|
|
styleUrls: ['./entity-keys-list.component.scss'],
|
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 {
|
|
|
|
|
|
|
|
|
|
keysListFormGroup: FormGroup;
|
|
|
|
|
|
|
|
|
|
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;
|
2019-08-20 20:42:48 +03:00
|
|
|
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;
|
|
|
|
|
@ViewChild('chipList') chipList: MatChipList;
|
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
|
|
|
|
2019-08-20 20:42:48 +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,
|
|
|
|
|
private fb: FormBuilder) {
|
|
|
|
|
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) {
|
2019-08-20 20:42:48 +03:00
|
|
|
this.keysListFormGroup.disable({emitEvent: false});
|
2019-08-19 20:09:41 +03:00
|
|
|
} else {
|
2019-08-20 20:42:48 +03:00
|
|
|
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 = [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 20:42:48 +03:00
|
|
|
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) {
|
2021-08-10 16:44:27 +03:00
|
|
|
const value = (event.value || '').trim();
|
|
|
|
|
if (value) {
|
|
|
|
|
this.addKey(value);
|
2019-08-19 20:09:41 +03:00
|
|
|
}
|
2021-08-10 16:44:27 +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,
|
2019-11-15 12:22:14 +02:00
|
|
|
this.dataKeyType, {ignoreLoading: true}).pipe(
|
2019-08-19 20:09:41 +03:00
|
|
|
map((data) => data ? data : [])) : of([]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 16:44:27 +03:00
|
|
|
clear(value: string = '', emitEvent = true) {
|
2019-08-19 20:09:41 +03:00
|
|
|
this.keyInput.nativeElement.value = value;
|
2021-08-10 16:44:27 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|