2019-12-27 16:35:11 +02:00
|
|
|
///
|
2020-02-20 10:26:43 +02:00
|
|
|
/// Copyright © 2016-2020 The Thingsboard Authors
|
2019-12-27 16:35:11 +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.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
Component,
|
|
|
|
|
ElementRef,
|
|
|
|
|
forwardRef,
|
|
|
|
|
Input,
|
|
|
|
|
OnChanges,
|
2020-04-22 13:21:14 +03:00
|
|
|
OnDestroy,
|
2019-12-27 16:35:11 +02:00
|
|
|
OnInit,
|
|
|
|
|
SimpleChanges,
|
2020-04-22 13:21:14 +03:00
|
|
|
ViewChild
|
2019-12-27 16:35:11 +02:00
|
|
|
} from '@angular/core';
|
|
|
|
|
import { ControlValueAccessor, FormControl, NG_VALIDATORS, NG_VALUE_ACCESSOR, Validator } from '@angular/forms';
|
2020-12-28 16:06:36 +02:00
|
|
|
import { Ace } from 'ace-builds';
|
2019-12-27 16:35:11 +02:00
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
|
|
|
|
import { ActionNotificationHide, ActionNotificationShow } from '@core/notification/notification.actions';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { ContentType, contentTypesMap } from '@shared/models/constants';
|
|
|
|
|
import { CancelAnimationFrame, RafService } from '@core/services/raf.service';
|
2020-03-24 19:11:01 +02:00
|
|
|
import { guid } from '@core/utils';
|
2020-04-22 13:21:14 +03:00
|
|
|
import { ResizeObserver } from '@juggle/resize-observer';
|
2020-12-28 16:06:36 +02:00
|
|
|
import { getAce } from '@shared/models/ace/ace.models';
|
2021-01-05 11:37:05 +02:00
|
|
|
import { beautifyJs } from '@shared/models/beautify.models';
|
2019-12-27 16:35:11 +02:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-json-content',
|
|
|
|
|
templateUrl: './json-content.component.html',
|
|
|
|
|
styleUrls: ['./json-content.component.scss'],
|
|
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => JsonContentComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALIDATORS,
|
|
|
|
|
useExisting: forwardRef(() => JsonContentComponent),
|
|
|
|
|
multi: true,
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
export class JsonContentComponent implements OnInit, ControlValueAccessor, Validator, OnChanges, OnDestroy {
|
|
|
|
|
|
|
|
|
|
@ViewChild('jsonEditor', {static: true})
|
|
|
|
|
jsonEditorElmRef: ElementRef;
|
|
|
|
|
|
2020-12-28 16:06:36 +02:00
|
|
|
private jsonEditor: Ace.Editor;
|
2019-12-27 16:35:11 +02:00
|
|
|
private editorsResizeCaf: CancelAnimationFrame;
|
2020-04-22 13:21:14 +03:00
|
|
|
private editorResize$: ResizeObserver;
|
2020-10-15 12:16:14 +03:00
|
|
|
private ignoreChange = false;
|
2019-12-27 16:35:11 +02:00
|
|
|
|
2020-03-24 19:11:01 +02:00
|
|
|
toastTargetId = `jsonContentEditor-${guid()}`;
|
|
|
|
|
|
2019-12-27 16:35:11 +02:00
|
|
|
@Input() label: string;
|
|
|
|
|
|
|
|
|
|
@Input() contentType: ContentType;
|
|
|
|
|
|
|
|
|
|
@Input() disabled: boolean;
|
|
|
|
|
|
|
|
|
|
@Input() fillHeight: boolean;
|
|
|
|
|
|
|
|
|
|
@Input() editorStyle: {[klass: string]: any};
|
|
|
|
|
|
|
|
|
|
private readonlyValue: boolean;
|
|
|
|
|
get readonly(): boolean {
|
|
|
|
|
return this.readonlyValue;
|
|
|
|
|
}
|
|
|
|
|
@Input()
|
|
|
|
|
set readonly(value: boolean) {
|
|
|
|
|
this.readonlyValue = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private validateContentValue: boolean;
|
|
|
|
|
get validateContent(): boolean {
|
|
|
|
|
return this.validateContentValue;
|
|
|
|
|
}
|
|
|
|
|
@Input()
|
|
|
|
|
set validateContent(value: boolean) {
|
|
|
|
|
this.validateContentValue = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 17:21:14 +03:00
|
|
|
private validateOnChangeValue: boolean;
|
|
|
|
|
get validateOnChange(): boolean {
|
|
|
|
|
return this.validateOnChangeValue;
|
|
|
|
|
}
|
|
|
|
|
@Input()
|
|
|
|
|
set validateOnChange(value: boolean) {
|
|
|
|
|
this.validateOnChangeValue = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 16:35:11 +02:00
|
|
|
fullscreen = false;
|
|
|
|
|
|
|
|
|
|
contentBody: string;
|
|
|
|
|
|
|
|
|
|
contentValid: boolean;
|
|
|
|
|
|
|
|
|
|
errorShowed = false;
|
|
|
|
|
|
|
|
|
|
private propagateChange = null;
|
|
|
|
|
|
|
|
|
|
constructor(public elementRef: ElementRef,
|
|
|
|
|
protected store: Store<AppState>,
|
|
|
|
|
private raf: RafService) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
const editorElement = this.jsonEditorElmRef.nativeElement;
|
|
|
|
|
let mode = 'text';
|
|
|
|
|
if (this.contentType) {
|
|
|
|
|
mode = contentTypesMap.get(this.contentType).code;
|
|
|
|
|
}
|
2020-12-28 16:06:36 +02:00
|
|
|
let editorOptions: Partial<Ace.EditorOptions> = {
|
2019-12-27 16:35:11 +02:00
|
|
|
mode: `ace/mode/${mode}`,
|
|
|
|
|
showGutter: true,
|
|
|
|
|
showPrintMargin: false,
|
2020-04-06 10:20:20 +03:00
|
|
|
readOnly: this.disabled || this.readonly
|
2019-12-27 16:35:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const advancedOptions = {
|
|
|
|
|
enableSnippets: true,
|
|
|
|
|
enableBasicAutocompletion: true,
|
|
|
|
|
enableLiveAutocompletion: true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
editorOptions = {...editorOptions, ...advancedOptions};
|
2020-12-28 16:06:36 +02:00
|
|
|
getAce().subscribe(
|
|
|
|
|
(ace) => {
|
|
|
|
|
this.jsonEditor = ace.edit(editorElement, editorOptions);
|
|
|
|
|
this.jsonEditor.session.setUseWrapMode(true);
|
|
|
|
|
this.jsonEditor.setValue(this.contentBody ? this.contentBody : '', -1);
|
|
|
|
|
this.jsonEditor.setReadOnly(this.disabled || this.readonly);
|
|
|
|
|
this.jsonEditor.on('change', () => {
|
|
|
|
|
if (!this.ignoreChange) {
|
|
|
|
|
this.cleanupJsonErrors();
|
|
|
|
|
this.updateView();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.jsonEditor.on('blur', () => {
|
|
|
|
|
this.contentValid = !this.validateContent || this.doValidate(true);
|
|
|
|
|
});
|
|
|
|
|
this.editorResize$ = new ResizeObserver(() => {
|
|
|
|
|
this.onAceEditorResize();
|
|
|
|
|
});
|
|
|
|
|
this.editorResize$.observe(editorElement);
|
2020-10-15 12:16:14 +03:00
|
|
|
}
|
2020-12-28 16:06:36 +02:00
|
|
|
);
|
2019-12-27 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
2020-04-22 13:21:14 +03:00
|
|
|
if (this.editorResize$) {
|
|
|
|
|
this.editorResize$.disconnect();
|
2019-12-27 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onAceEditorResize() {
|
|
|
|
|
if (this.editorsResizeCaf) {
|
|
|
|
|
this.editorsResizeCaf();
|
|
|
|
|
this.editorsResizeCaf = null;
|
|
|
|
|
}
|
|
|
|
|
this.editorsResizeCaf = this.raf.raf(() => {
|
|
|
|
|
this.jsonEditor.resize();
|
|
|
|
|
this.jsonEditor.renderer.updateFull();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
|
for (const propName of Object.keys(changes)) {
|
|
|
|
|
const change = changes[propName];
|
|
|
|
|
if (!change.firstChange && change.currentValue !== change.previousValue) {
|
|
|
|
|
if (propName === 'contentType') {
|
|
|
|
|
if (this.jsonEditor) {
|
|
|
|
|
let mode = 'text';
|
|
|
|
|
if (this.contentType) {
|
|
|
|
|
mode = contentTypesMap.get(this.contentType).code;
|
|
|
|
|
}
|
|
|
|
|
this.jsonEditor.session.setMode(`ace/mode/${mode}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
2020-04-06 10:20:20 +03:00
|
|
|
if (this.jsonEditor) {
|
|
|
|
|
this.jsonEditor.setReadOnly(this.disabled || this.readonly);
|
|
|
|
|
}
|
2019-12-27 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public validate(c: FormControl) {
|
|
|
|
|
return (this.contentValid) ? null : {
|
|
|
|
|
contentBody: {
|
|
|
|
|
valid: false,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validateOnSubmit(): void {
|
2020-04-06 10:20:20 +03:00
|
|
|
if (!this.disabled && !this.readonly) {
|
2019-12-27 16:35:11 +02:00
|
|
|
this.cleanupJsonErrors();
|
|
|
|
|
this.contentValid = true;
|
|
|
|
|
this.propagateChange(this.contentBody);
|
2020-10-15 12:16:14 +03:00
|
|
|
this.contentValid = this.doValidate(true);
|
2019-12-27 16:35:11 +02:00
|
|
|
this.propagateChange(this.contentBody);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 12:16:14 +03:00
|
|
|
private doValidate(showErrorToast = false): boolean {
|
2019-12-27 16:35:11 +02:00
|
|
|
try {
|
|
|
|
|
if (this.validateContent && this.contentType === ContentType.JSON) {
|
|
|
|
|
JSON.parse(this.contentBody);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (ex) {
|
2020-10-15 12:16:14 +03:00
|
|
|
if (showErrorToast) {
|
|
|
|
|
let errorInfo = 'Error:';
|
|
|
|
|
if (ex.name) {
|
|
|
|
|
errorInfo += ' ' + ex.name + ':';
|
|
|
|
|
}
|
|
|
|
|
if (ex.message) {
|
|
|
|
|
errorInfo += ' ' + ex.message;
|
|
|
|
|
}
|
|
|
|
|
this.store.dispatch(new ActionNotificationShow(
|
|
|
|
|
{
|
|
|
|
|
message: errorInfo,
|
|
|
|
|
type: 'error',
|
|
|
|
|
target: this.toastTargetId,
|
|
|
|
|
verticalPosition: 'bottom',
|
|
|
|
|
horizontalPosition: 'left'
|
|
|
|
|
}));
|
|
|
|
|
this.errorShowed = true;
|
2019-12-27 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cleanupJsonErrors(): void {
|
|
|
|
|
if (this.errorShowed) {
|
|
|
|
|
this.store.dispatch(new ActionNotificationHide(
|
|
|
|
|
{
|
2020-03-24 19:11:01 +02:00
|
|
|
target: this.toastTargetId
|
2019-12-27 16:35:11 +02:00
|
|
|
}));
|
|
|
|
|
this.errorShowed = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: string): void {
|
|
|
|
|
this.contentBody = value;
|
|
|
|
|
this.contentValid = true;
|
|
|
|
|
if (this.jsonEditor) {
|
2020-10-15 12:16:14 +03:00
|
|
|
this.ignoreChange = true;
|
2019-12-27 16:35:11 +02:00
|
|
|
this.jsonEditor.setValue(this.contentBody ? this.contentBody : '', -1);
|
2020-10-15 12:16:14 +03:00
|
|
|
this.ignoreChange = false;
|
2019-12-27 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateView() {
|
|
|
|
|
const editorValue = this.jsonEditor.getValue();
|
|
|
|
|
if (this.contentBody !== editorValue) {
|
|
|
|
|
this.contentBody = editorValue;
|
2020-05-14 17:21:14 +03:00
|
|
|
this.contentValid = !this.validateOnChange || this.doValidate();
|
2019-12-27 16:35:11 +02:00
|
|
|
this.propagateChange(this.contentBody);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 17:55:29 +02:00
|
|
|
beautifyJSON() {
|
2021-01-05 11:37:05 +02:00
|
|
|
beautifyJs(this.contentBody, {indent_size: 4, wrap_line_length: 60}).subscribe(
|
|
|
|
|
(res) => {
|
|
|
|
|
this.jsonEditor.setValue(res ? res : '', -1);
|
|
|
|
|
this.updateView();
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-12-27 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 17:55:29 +02:00
|
|
|
minifyJSON() {
|
|
|
|
|
const res = JSON.stringify(this.contentBody);
|
|
|
|
|
this.jsonEditor.setValue(res ? res : '', -1);
|
|
|
|
|
this.updateView();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 16:35:11 +02:00
|
|
|
onFullscreen() {
|
|
|
|
|
if (this.jsonEditor) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.jsonEditor.resize();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|