// ========================LICENSE_END===================================
// /
-import { Component, Input, OnInit } from '@angular/core';
-import { AbstractControl, ControlContainer, FormBuilder, FormControl, FormGroup, FormGroupDirective, ValidatorFn, Validators } from '@angular/forms';
+import { Component, Input, OnInit, Output } from "@angular/core";
+import {
+ AbstractControl,
+ ControlContainer,
+ FormControl,
+ FormGroup,
+ FormGroupDirective,
+ ValidatorFn,
+ Validators,
+} from "@angular/forms";
+import { EventEmitter } from "@angular/core";
@Component({
- selector: 'nrcp-no-type-policy-editor',
- templateUrl: './no-type-policy-editor.component.html',
- styleUrls: ['./no-type-policy-editor.component.scss'],
- viewProviders: [{ provide: ControlContainer, useExisting: FormGroupDirective }]
+ selector: "nrcp-no-type-policy-editor",
+ templateUrl: "./no-type-policy-editor.component.html",
+ styleUrls: ["./no-type-policy-editor.component.scss"],
+ viewProviders: [
+ { provide: ControlContainer, useExisting: FormGroupDirective },
+ ],
})
export class NoTypePolicyEditorComponent implements OnInit {
+ @Input() policyJson: string = null;
+ @Output() validJson: EventEmitter<string> = new EventEmitter<string>();
- @Input() instanceForm: FormGroup;
- @Input() policyJson: string;
+ instanceForm: FormGroup = new FormGroup({});
- constructor(
- private formBuilder: FormBuilder) { }
+ constructor() {}
ngOnInit(): void {
+ let initialJson: string;
+ if (this.policyJson) {
+ initialJson = formatJsonString(this.policyJson);
+ } else {
+ initialJson = "{}";
+ }
this.instanceForm.addControl(
- 'policyJsonTextArea', new FormControl(this.policyJson, [
+ "policyJsonTextArea",
+ new FormControl(initialJson, [
Validators.required,
- jsonValidator()
+ this.jsonValidator(),
])
- )
+ );
}
get policyJsonTextArea(): AbstractControl {
- return this.instanceForm ? this.instanceForm.get('policyJsonTextArea') : null;
+ return this.instanceForm
+ ? this.instanceForm.get("policyJsonTextArea")
+ : null;
}
formatJsonInput(): void {
- this.policyJson = formatJsonString(JSON.parse(this.policyJsonTextArea.value));
+ let jsonBefore: string = this.policyJsonTextArea.value;
+ let jsonAfter = formatJsonString(JSON.parse(jsonBefore));
+ this.policyJsonTextArea.setValue(jsonAfter);
}
-}
-
-export function formatJsonString(jsonToFormat: any): string {
- return JSON.stringify(jsonToFormat, null, 2);
-}
-export function jsonValidator(): ValidatorFn {
- return (control: AbstractControl): { [key: string]: any } | null => {
- const notValid = !isJsonValid(control.value);
- return notValid ? { 'invalidJson': { value: control.value } } : null;
- };
-}
+ jsonValidator(): ValidatorFn {
+ return (control: AbstractControl): { [key: string]: any } | null => {
+ const notValid = !this.isJsonValid(control.value);
+ this.handleJsonChangeEvent(notValid, control.value);
+ return notValid ? { invalidJson: { value: control.value } } : null;
+ };
+ }
-export function isJsonValid(json: string): boolean {
- try {
- if (json != null) {
- JSON.parse(json);
- return true;
- } else {
- return false;
+ handleJsonChangeEvent(notValid: boolean, newValue: string): void {
+ let json = newValue;
+ if (notValid) {
+ json = null;
}
- } catch (jsonError) {
- return false;
+ this.validJson.emit(json);
+ }
+
+ isJsonValid(json: string): boolean {
+ let valid = false as boolean;
+ try {
+ if (json != null) {
+ JSON.parse(json);
+ valid = true;
+ }
+ } catch (jsonError) {}
+ return valid;
}
}
+
+export function formatJsonString(jsonToFormat: any): string {
+ return JSON.stringify(jsonToFormat, null, 2);
+}