2020-03-02 17:55:29 +02:00
|
|
|
///
|
2024-01-09 10:46:16 +02:00
|
|
|
/// Copyright © 2016-2024 The Thingsboard Authors
|
2020-03-02 17:55:29 +02: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-04-27 09:27:14 +03:00
|
|
|
import { Directive, ElementRef, forwardRef, HostListener, Renderer2, SkipSelf } from '@angular/core';
|
2020-03-02 17:55:29 +02:00
|
|
|
import {
|
|
|
|
|
ControlValueAccessor,
|
2020-04-27 09:27:14 +03:00
|
|
|
FormGroupDirective,
|
2020-03-02 17:55:29 +02:00
|
|
|
NG_VALIDATORS,
|
2020-04-27 09:27:14 +03:00
|
|
|
NG_VALUE_ACCESSOR,
|
|
|
|
|
NgForm,
|
2023-09-12 13:03:00 +03:00
|
|
|
UntypedFormControl,
|
2020-03-02 17:55:29 +02:00
|
|
|
ValidationErrors,
|
|
|
|
|
Validator
|
2020-03-20 16:57:35 +02:00
|
|
|
} from '@angular/forms';
|
2020-04-27 09:27:14 +03:00
|
|
|
import { ErrorStateMatcher } from '@angular/material/core';
|
2023-09-12 13:03:00 +03:00
|
|
|
import { isObject } from '@core/utils';
|
2020-03-02 17:55:29 +02:00
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
|
selector: '[tb-json-to-string]',
|
2023-09-12 13:03:00 +03:00
|
|
|
// eslint-disable-next-line @angular-eslint/no-host-metadata-property
|
|
|
|
|
host: {
|
|
|
|
|
'(blur)': 'onTouched()'
|
|
|
|
|
},
|
2020-03-02 17:55:29 +02:00
|
|
|
providers: [{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => TbJsonToStringDirective),
|
|
|
|
|
multi: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALIDATORS,
|
|
|
|
|
useExisting: forwardRef(() => TbJsonToStringDirective),
|
|
|
|
|
multi: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: ErrorStateMatcher,
|
|
|
|
|
useExisting: TbJsonToStringDirective
|
|
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export class TbJsonToStringDirective implements ControlValueAccessor, Validator, ErrorStateMatcher {
|
|
|
|
|
private propagateChange = null;
|
2023-09-12 13:03:00 +03:00
|
|
|
public onTouched = () => {};
|
2020-03-02 17:55:29 +02:00
|
|
|
private parseError: boolean;
|
|
|
|
|
private data: any;
|
|
|
|
|
|
|
|
|
|
@HostListener('input', ['$event.target.value']) input(newValue: any): void {
|
|
|
|
|
try {
|
2023-09-12 13:03:00 +03:00
|
|
|
if (newValue) {
|
|
|
|
|
this.data = JSON.parse(newValue);
|
|
|
|
|
if (isObject(this.data)) {
|
|
|
|
|
this.parseError = false;
|
|
|
|
|
} else {
|
|
|
|
|
this.data = null;
|
|
|
|
|
this.parseError = true;
|
|
|
|
|
}
|
2022-04-19 16:21:10 +03:00
|
|
|
} else {
|
2023-09-12 13:03:00 +03:00
|
|
|
this.data = null;
|
|
|
|
|
this.parseError = false;
|
2022-04-19 16:21:10 +03:00
|
|
|
}
|
2020-03-02 17:55:29 +02:00
|
|
|
} catch (e) {
|
2023-09-12 13:03:00 +03:00
|
|
|
this.data = null;
|
2020-03-02 17:55:29 +02:00
|
|
|
this.parseError = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.propagateChange(this.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(private render: Renderer2,
|
|
|
|
|
private element: ElementRef,
|
|
|
|
|
@SkipSelf() private errorStateMatcher: ErrorStateMatcher) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
isErrorState(control: UntypedFormControl | null, form: FormGroupDirective | NgForm | null): boolean {
|
2023-09-12 13:03:00 +03:00
|
|
|
return !!(control && control.invalid && !Array.isArray(control.value) && control.touched);
|
2020-03-02 17:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
validate(c: UntypedFormControl): ValidationErrors {
|
2020-03-02 17:55:29 +02:00
|
|
|
return (!this.parseError) ? null : {
|
|
|
|
|
invalidJSON: {
|
|
|
|
|
valid: false
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(obj: any): void {
|
2023-09-12 13:03:00 +03:00
|
|
|
this.data = obj;
|
|
|
|
|
this.parseError = false;
|
|
|
|
|
this.render.setProperty(this.element.nativeElement, 'value', obj ? JSON.stringify(obj) : '');
|
2020-03-02 17:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
2023-09-12 13:03:00 +03:00
|
|
|
this.onTouched = fn;
|
2020-03-02 17:55:29 +02:00
|
|
|
}
|
|
|
|
|
}
|