2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
21 import { Component, Input, OnInit, Output } from "@angular/core";
30 } from "@angular/forms";
31 import { EventEmitter } from "@angular/core";
34 selector: "nrcp-no-type-policy-editor",
35 templateUrl: "./no-type-policy-editor.component.html",
36 styleUrls: ["./no-type-policy-editor.component.scss"],
38 { provide: ControlContainer, useExisting: FormGroupDirective },
41 export class NoTypePolicyEditorComponent implements OnInit {
42 @Input() policyJson: string = null;
43 @Output() validJson: EventEmitter<string> = new EventEmitter<string>();
45 instanceForm: FormGroup = new FormGroup({});
50 this.instanceForm.addControl(
52 new FormControl(this.policyJson, [
57 if (!this.policyJson) this.policyJson = "{}";
60 get policyJsonTextArea(): AbstractControl {
61 return this.instanceForm
62 ? this.instanceForm.get("policyJsonTextArea")
66 formatJsonInput(): void {
67 let jsonBefore: string = this.policyJsonTextArea.value;
68 let jsonAfter = formatJsonString(JSON.parse(jsonBefore));
69 this.policyJsonTextArea.setValue(jsonAfter);
72 jsonValidator(): ValidatorFn {
73 return (control: AbstractControl): { [key: string]: any } | null => {
74 const notValid = !this.isJsonValid(control.value);
75 this.handleJsonChangeEvent(notValid, control.value);
76 return notValid ? { invalidJson: { value: control.value } } : null;
80 handleJsonChangeEvent(notValid: boolean, newValue: string): void {
85 this.validJson.emit(json);
88 isJsonValid(json: string): boolean {
89 let valid = false as boolean;
95 } catch (jsonError) {}
100 export function formatJsonString(jsonToFormat: any): string {
101 return JSON.stringify(jsonToFormat, null, 2);