import { Component , Input , OnDestroy , OnInit } from '@angular/core' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { checkTypeExistsValidator , TypeService } from '../../../services/type.service' ; import { CmdbMode } from '../../../modes.enum' ; import { ReplaySubject } from 'rxjs' ; import { TypeBuilderStepComponent } from '../type-builder-step.component' ; import { takeUntil } from 'rxjs/operators' ; import { CmdbType } from '../../../models/cmdb-type' ; /**
 * Type builder step for basic type information.
 */ @ Component ( { selector : 'cmdb-type-basic-step' , templateUrl : './type-basic-step.component.html' , styleUrls : [ './type-basic-step.component.scss' ] , } ) export class TypeBasicStepComponent extends TypeBuilderStepComponent implements OnInit , OnDestroy { private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; public form : FormGroup ; @ Input ( 'typeInstance' ) public set TypeInstance ( instance ) { if ( instance ) { this . typeInstance = instance ; this . form . patchValue ( { name : this . typeInstance . name , label : this . typeInstance . label , description : this . typeInstance . description , active : this . typeInstance . active , icon : this . typeInstance . render_meta . icon } ) ; } } constructor ( private typeService ) { super ( ) ; this . form = new FormGroup ( { name : new FormControl ( '' , Validators . required ) , label : new FormControl ( '' , Validators . required ) , description : new FormControl ( '' ) , active : new FormControl ( true ) , icon : new FormControl ( 'fa fa-cube' ) } ) ; } public ngOnInit ( ) { if ( this . mode === CmdbMode . Create ) { this . form . get ( 'name' ) . setAsyncValidators ( checkTypeExistsValidator ( this . typeService ) ) ; } else if ( this . mode === CmdbMode . Edit ) { this . form . markAllAsTouched ( ) ; } this . form . valueChanges . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( changes ) => { this . assign ( changes ) ; } ) ; this . form . statusChanges . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( ) => { this . validateChange . emit ( this . form . valid ) ; this . valid = this . form . valid ; } ) ; } public assign ( changes ) { this . typeInstance . name = changes . name ; this . typeInstance . label = changes . label ; this . typeInstance . description = changes . description ; this . typeInstance . active = changes . active ; this . typeInstance . render_meta . icon = changes . icon ; } public get icon ( ) : FormControl { return this . form . get ( 'icon' ) as FormControl ; } public get name ( ) : FormControl { return this . form . get ( 'name' ) as FormControl ; } public get label ( ) : FormControl { return this . form . get ( 'label' ) as FormControl ; } public get description ( ) : FormControl { return this . form . get ( 'description' ) as FormControl ; } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }