import { Component , ComponentFactoryResolver , ComponentRef , OnInit , ViewChild , ViewContainerRef } from '@angular/core' ; import { fieldComponents } from '../fields/fields.list' ; import { simpleComponents } from '../simple/simple.list' ; import { RenderFieldComponent } from '../fields/components.fields' ; import { ToastService } from '../../../layout/toast/toast.service' ; import { FormArray , FormControl , FormGroup , Validators } from '@angular/forms' ; import { CmdbMode } from '../../modes.enum' ; @ Component ( { selector : 'cmdb-render-element' , templateUrl : './render-element.component.html' , styleUrls : [ './render-element.component.scss' ] } ) export class RenderElementComponent extends RenderFieldComponent implements OnInit { @ ViewChild ( 'fieldContainer' , { read : ViewContainerRef , static : true } ) containerField ; public simpleRender : boolean = false ; private component : any ; private componentRef : ComponentRef < any > ; constructor ( private resolver , public toast ) { super ( ) ; } public ngOnInit ( ) { this . containerField . clear ( ) ; switch ( this . mode ) { case CmdbMode . View : case CmdbMode . Create : case CmdbMode . Edit : case CmdbMode . Bulk : { this . simpleRender = false ; this . component = fieldComponents [ this . data . type ] ; const factory = this . resolver . resolveComponentFactory ( this . component ) ; this . componentRef = this . containerField . createComponent ( factory ) ; this . componentRef . instance . parentFormGroup = this . parentFormGroup ; this . componentRef . instance . data = this . data ; this . componentRef . instance . mode = this . mode ; this . componentRef . instance . section = this . section ; this . componentRef . instance . toast = this . toast ; const fieldControl = new FormControl ( '' ) ; const validators = [ ] ; if ( this . mode === CmdbMode . View || CmdbMode . Edit ) { fieldControl . patchValue ( this . value ) ; } if ( this . data . required ) { validators . push ( Validators . required ) ; } if ( this . data . regex ) { validators . push ( Validators . pattern ( this . data . regex ) ) ; } fieldControl . setValidators ( validators ) ; if ( this . mode === CmdbMode . View ) { fieldControl . disable ( ) ; } if ( this . data . disabled ) { fieldControl . disable ( ) ; } this . parentFormGroup . removeControl ( this . data . name ) ; this . componentRef . instance . parentFormGroup . addControl ( this . data . name , fieldControl ) ; if ( CmdbMode . Bulk === this . mode ) { this . componentRef . instance . changeForm = this . changeForm ; } break ; } case CmdbMode . Simple : { this . data . value = this . value ; this . component = simpleComponents [ this . data . type ] ; const factory = this . resolver . resolveComponentFactory ( this . component ) ; this . componentRef = this . containerField . createComponent ( factory ) ; this . componentRef . instance . mode = this . mode ; this . componentRef . instance . data = this . data ; this . componentRef . instance . toast = this . toast ; break ; } } } }