/* Copyright (c) 2019 AT&T Intellectual Property. # # # # 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, OnInit, Input, Output, EventEmitter, ViewChild, NgModuleRef, Injector, Compiler, NgModule, ViewContainerRef } from '@angular/core'; import { FormGroup, FormControl, ReactiveFormsModule } from '@angular/forms'; import { MatSnackBar } from '@angular/material'; import { AlertSnackbarComponent } from '../alert-snackbar/alert-snackbar.component'; @Component({ selector: 'app-form-generator', templateUrl: './form-generator.component.html', styleUrls: ['./form-generator.component.scss'] }) export class FormGeneratorComponent implements OnInit { public test = { text1: "Hello please enter text1", text2: "Hello please enter text2", one: { text1: "lol" }, siteSpecific: { port: "1234", host: "google.com" }, list: ["Enter", "some", "values"], list2: [{"test": "hello"}, {"test2": "hello2"}] } public isSaved = false; public arrayCheck; @Input() public JSONData : any; @Input() public taskId: any; @ViewChild('test', { read: ViewContainerRef }) public containerDiv; @Output() public childEvent = new EventEmitter(); form = new FormGroup({ }); //public textAreaTemplate = "';// + path + "'> "+ data[key] + "" const tmpCmp = Component({template: textAreaTemplate})(class { }); const tmpModule = NgModule({imports:[ReactiveFormsModule], declarations: [tmpCmp]})(class { }); //this.containerDiv.element.nativeElement.appendChild(document.createElement("label")).innerHTML = key.trim(); this.compiler.compileModuleAndAllComponentsAsync(tmpModule) .then((factories) => { const f = factories.componentFactories[0]; const cmpRef = this.containerDiv.createComponent(f); cmpRef.instance.form = this.form; }) } else{ const textAreaTemplate = '
' + key.trim() + '
';// + path + "'> "+ data[key] + "" const tmpCmp = Component({template: textAreaTemplate})(class { }); const tmpModule = NgModule({imports:[ReactiveFormsModule], declarations: [tmpCmp]})(class { }); //this.containerDiv.element.nativeElement.appendChild(document.createElement("label")).innerHTML = key.trim(); this.compiler.compileModuleAndAllComponentsAsync(tmpModule) .then((factories) => { const f = factories.componentFactories[0]; const cmpRef = this.containerDiv.createComponent(f); cmpRef.instance.form = this.form; }) } // const textAreaTemplate = '
';// + path + "'> "+ data[key] + "" // const tmpCmp = Component({template: textAreaTemplate})(class { // }); // const tmpModule = NgModule({imports:[ReactiveFormsModule] ,declarations: [tmpCmp]})(class { // }); // //this.containerDiv.element.nativeElement.appendChild(document.createElement("label")).innerHTML = key.trim(); // this.compiler.compileModuleAndAllComponentsAsync(tmpModule) // .then((factories) => { // const f = factories.componentFactories[0]; // const cmpRef = this.containerDiv.createComponent(f); // cmpRef.instance.form = this.form; // }) } else if(typeof data[key] === 'boolean'){ let str = ''; let str2 = 'h5'; let bold = ' style="font-weight:bold"' if(level > 1){ str2 = 'label'; bold = ''; } if(data[key]){ str = ''; }else{ str = ''; } this.form.addControl(keyPath + key.trim(), new FormControl(data[key])); const textAreaTemplate = '
<' + str2 + bold + ' class="mr-2">' + key.trim() + '
';// + path + "'> "+ data[key] + "" const tmpCmp = Component({template: textAreaTemplate})(class { }); const tmpModule = NgModule({imports:[ReactiveFormsModule], declarations: [tmpCmp]})(class { }); //this.containerDiv.element.nativeElement.appendChild(document.createElement("label")).innerHTML = key.trim(); this.compiler.compileModuleAndAllComponentsAsync(tmpModule) .then((factories) => { const f = factories.componentFactories[0]; const cmpRef = this.containerDiv.createComponent(f); cmpRef.instance.form = this.form; }) } else if(typeof data[key] === typeof 23){ let str = 'h5'; let bold = ' style="font-weight:bold"'; if(level > 1){ str = 'label'; bold = ''; } this.form.addControl(keyPath + key.trim(), new FormControl(data[key])); const textInputTemplate = '
<' + str + bold + ' class="mr-2">' + key.trim() + '
'; const tmpCmp = Component({template: textInputTemplate})(class { }); const tmpModule = NgModule({imports:[ReactiveFormsModule], declarations: [tmpCmp]})(class { }); //this.containerDiv.element.nativeElement.appendChild(document.createElement("label")).innerHTML = key.trim(); this.compiler.compileModuleAndAllComponentsAsync(tmpModule) .then((factories) => { const f = factories.componentFactories[0]; const cmpRef = this.containerDiv.createComponent(f); cmpRef.instance.form = this.form; }) } else{ const textAreaTemplate = '
' + key.trim() + ': Type Not Supported
';// + path + "'> "+ data[key] + "" const tmpCmp = Component({template: textAreaTemplate})(class { }); const tmpModule = NgModule({imports:[ReactiveFormsModule], declarations: [tmpCmp]})(class { }); //this.containerDiv.element.nativeElement.appendChild(document.createElement("label")).innerHTML = key.trim(); this.compiler.compileModuleAndAllComponentsAsync(tmpModule) .then((factories) => { const f = factories.componentFactories[0]; const cmpRef = this.containerDiv.createComponent(f); cmpRef.instance.form = this.form; }) } } } }