import { Component , Input } from '@angular/core' ; import { CmdbType } from '../models/cmdb-type' ; import { CmdbMode } from '../modes.enum' ; import { FormGroup } from '@angular/forms' ; import { CmdbObject } from '../models/cmdb-object' ; import { RenderResult } from '../models/cmdb-render' ; @ Component ( { selector : 'cmdb-render' , templateUrl : './render.component.html' , styleUrls : [ './render.component.scss' ] } ) export class RenderComponent { private typeInstanceBack : CmdbType ; private objectInstanceBack : CmdbObject ; private renderResultBack : RenderResult = undefined ; @ Input ( ) public renderForm : FormGroup ; @ Input ( ) public changeForm : FormGroup ; @ Input ( ) public mode : CmdbMode ; private field : any ; @ Input ( 'typeInstance' ) public set typeInstance ( type ) { if ( type !== undefined ) { this . typeInstanceBack = type ; } } public get typeInstance ( ) : CmdbType { return this . typeInstanceBack ; } @ Input ( 'objectInstance' ) public set objectInstance ( data ) { if ( data !== undefined ) { this . objectInstanceBack = data ; } } public get objectInstance ( ) : CmdbObject { return this . objectInstanceBack ; } @ Input ( 'renderResult' ) public set renderResult ( data ) { this . renderResultBack = data ; } public get renderResult ( ) : RenderResult { return this . renderResultBack ; } @ Input ( 'currentField' ) public get currentField ( ) { return this . field ; } public set currentField ( value ) { this . field = value ; } public get fields ( ) { return this . renderForm . get ( 'fields' ) ; } public constructor ( ) { if ( this . mode === CmdbMode . View ) { this . renderForm . disable ( ) ; } } public getFieldByName ( name ) { if ( this . renderResult !== undefined ) { return this . renderResult . fields . find ( field => field . name === name ) ; } else { const field = this . typeInstance . fields . find ( f => f . name === name ) ; switch ( field . type ) { case 'ref' : { field . default = parseInt ( field . default , 10 ) ; break ; } default : { field . default = field . value ; break ; } } return field ; } } public getValueByName ( name ) { if ( this . renderResult !== undefined ) { const fieldFound = this . renderResult . fields . find ( field => field . name === name ) ; if ( fieldFound === undefined ) { return { } ; } return fieldFound . value ; } else if ( this . objectInstance !== undefined ) { const fieldFound = this . objectInstance . fields . find ( field => field . name === name ) ; if ( fieldFound === undefined ) { return { } ; } return fieldFound . value ; } } }