X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-frontend%2Fsrc%2Fapp%2Fpolicy%2Fno-type-policy-editor%2Fno-type-policy-editor.component.ts;h=5fa92d9e08d1d219e6e4a6518a7f8291ef832c87;hb=66372cb88b6b3e94dada9197c5efeaa7b2c93e17;hp=f167adae7b62cee4728b76f8bd29caea7d2994ef;hpb=b3f6966f626c9d58e443280ff3ce17e44a2873e4;p=portal%2Fnonrtric-controlpanel.git diff --git a/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.ts b/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.ts index f167ada..5fa92d9 100644 --- a/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.ts +++ b/webapp-frontend/src/app/policy/no-type-policy-editor/no-type-policy-editor.component.ts @@ -17,10 +17,10 @@ // limitations under the License. // ========================LICENSE_END=================================== // / -// -import { Component, Input, OnInit } from '@angular/core'; +import { Component, Input, OnInit, Output } from '@angular/core'; import { AbstractControl, ControlContainer, FormBuilder, FormControl, FormGroup, FormGroupDirective, ValidatorFn, Validators } from '@angular/forms'; +import { EventEmitter } from '@angular/core'; @Component({ selector: 'nrcp-no-type-policy-editor', @@ -29,51 +29,61 @@ import { AbstractControl, ControlContainer, FormBuilder, FormControl, FormGroup, viewProviders: [{ provide: ControlContainer, useExisting: FormGroupDirective }] }) export class NoTypePolicyEditorComponent implements OnInit { + @Input() policyJson: string = null; + @Output() validJson: EventEmitter = new EventEmitter(); - @Input() instanceForm: FormGroup; - @Input() policyJson: string; + instanceForm: FormGroup = new FormGroup({}); constructor( private formBuilder: FormBuilder) { } - ngOnInit(): void { - this.instanceForm.addControl( - 'policyJsonTextArea', new FormControl(this.policyJson, [ - Validators.required, - jsonValidator() - ]) - ) - } + ngOnInit(): void { + this.instanceForm.addControl( + 'policyJsonTextArea', new FormControl(this.policyJson, [ + Validators.required, + this.jsonValidator() + ]) + ) + } - get policyJsonTextArea(): AbstractControl { + get policyJsonTextArea(): AbstractControl { return this.instanceForm ? this.instanceForm.get('policyJsonTextArea') : null; } formatJsonInput(): void { this.policyJson = formatJsonString(JSON.parse(this.policyJsonTextArea.value)); } -} -export function formatJsonString(jsonToFormat: any): string { - return JSON.stringify(jsonToFormat, null, 2); -} + 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 jsonValidator(): ValidatorFn { - return (control: AbstractControl): { [key: string]: any } | null => { - const notValid = !isJsonValid(control.value); - return notValid ? { 'invalidJson': { value: control.value } } : null; - }; -} + handleJsonChangeEvent(notValid: boolean, newValue: string): void { + let json = newValue; + if (notValid) { + json = null; + } + this.validJson.emit(json); + } -export function isJsonValid(json: string): boolean { - try { - if (json != null) { - JSON.parse(json); - return true; - } else { + isJsonValid(json: string): boolean { + try { + if (json != null) { + JSON.parse(json); + return true; + } else { + return false; + } + } catch (jsonError) { return false; } - } catch (jsonError) { - return false; } } + +export function formatJsonString(jsonToFormat: any): string { + return JSON.stringify(jsonToFormat, null, 2); +}