2019-08-29 20:04:59 +03:00
|
|
|
///
|
2021-01-11 13:42:16 +02:00
|
|
|
/// Copyright © 2016-2021 The Thingsboard Authors
|
2019-08-29 20:04:59 +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.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import { Component, forwardRef, Input, OnInit, ViewChild } from '@angular/core';
|
|
|
|
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NgForm } from '@angular/forms';
|
|
|
|
|
import { ValueType, valueTypesMap } from '@shared/models/constants';
|
2021-09-30 12:44:04 +03:00
|
|
|
import { isObject } from '@core/utils';
|
2020-03-20 16:57:35 +02:00
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
2020-03-02 17:55:29 +02:00
|
|
|
import {
|
|
|
|
|
JsonObjectEditDialogComponent,
|
2020-03-26 14:49:26 +02:00
|
|
|
JsonObjectEditDialogData
|
2020-03-20 16:57:35 +02:00
|
|
|
} from '@shared/components/dialog/json-object-edit-dialog.component';
|
2019-08-29 20:04:59 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-value-input',
|
|
|
|
|
templateUrl: './value-input.component.html',
|
|
|
|
|
styleUrls: ['./value-input.component.scss'],
|
|
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => ValueInputComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
export class ValueInputComponent implements OnInit, ControlValueAccessor {
|
|
|
|
|
|
|
|
|
|
@Input() disabled: boolean;
|
|
|
|
|
|
|
|
|
|
@Input() requiredText: string;
|
|
|
|
|
|
|
|
|
|
@ViewChild('inputForm', {static: true}) inputForm: NgForm;
|
|
|
|
|
|
|
|
|
|
modelValue: any;
|
|
|
|
|
|
|
|
|
|
valueType: ValueType;
|
|
|
|
|
|
|
|
|
|
public valueTypeEnum = ValueType;
|
|
|
|
|
|
|
|
|
|
valueTypeKeys = Object.keys(ValueType);
|
|
|
|
|
|
|
|
|
|
valueTypes = valueTypesMap;
|
|
|
|
|
|
|
|
|
|
private propagateChange = null;
|
|
|
|
|
|
2020-03-02 17:55:29 +02:00
|
|
|
constructor(
|
|
|
|
|
public dialog: MatDialog,
|
|
|
|
|
) {
|
2019-08-29 20:04:59 +03:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 17:55:29 +02:00
|
|
|
openEditJSONDialog($event: Event) {
|
|
|
|
|
if ($event) {
|
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
}
|
2020-03-26 14:49:26 +02:00
|
|
|
this.dialog.open<JsonObjectEditDialogComponent, JsonObjectEditDialogData, object>(JsonObjectEditDialogComponent, {
|
2020-03-02 17:55:29 +02:00
|
|
|
disableClose: true,
|
|
|
|
|
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
|
|
|
|
|
data: {
|
|
|
|
|
jsonValue: this.modelValue
|
|
|
|
|
}
|
|
|
|
|
}).afterClosed().subscribe(
|
|
|
|
|
(res) => {
|
|
|
|
|
if (res) {
|
|
|
|
|
this.modelValue = res;
|
2020-03-20 16:57:35 +02:00
|
|
|
this.inputForm.control.patchValue({value: this.modelValue});
|
2020-03-02 17:55:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 20:04:59 +03:00
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: any): void {
|
|
|
|
|
this.modelValue = value;
|
|
|
|
|
if (this.modelValue === true || this.modelValue === false) {
|
|
|
|
|
this.valueType = ValueType.BOOLEAN;
|
|
|
|
|
} else if (typeof this.modelValue === 'number') {
|
|
|
|
|
if (this.modelValue.toString().indexOf('.') === -1) {
|
|
|
|
|
this.valueType = ValueType.INTEGER;
|
|
|
|
|
} else {
|
|
|
|
|
this.valueType = ValueType.DOUBLE;
|
|
|
|
|
}
|
2020-03-02 17:55:29 +02:00
|
|
|
} else if (isObject(this.modelValue)) {
|
|
|
|
|
this.valueType = ValueType.JSON;
|
2019-08-29 20:04:59 +03:00
|
|
|
} else {
|
|
|
|
|
this.valueType = ValueType.STRING;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateView() {
|
2021-09-30 12:39:16 +03:00
|
|
|
if (this.inputForm.valid || this.valueType === ValueType.BOOLEAN || this.valueType === ValueType.JSON) {
|
2019-08-29 20:04:59 +03:00
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
} else {
|
|
|
|
|
this.propagateChange(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onValueTypeChanged() {
|
|
|
|
|
if (this.valueType === ValueType.BOOLEAN) {
|
|
|
|
|
this.modelValue = false;
|
2021-09-30 12:39:16 +03:00
|
|
|
} else if (this.valueType === ValueType.JSON) {
|
2020-03-02 17:55:29 +02:00
|
|
|
this.modelValue = {};
|
2019-08-29 20:04:59 +03:00
|
|
|
} else {
|
|
|
|
|
this.modelValue = null;
|
|
|
|
|
}
|
|
|
|
|
this.updateView();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onValueChanged() {
|
|
|
|
|
this.updateView();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|