import { Component , Input , OnDestroy , OnInit } from '@angular/core' ; import { FormArray , FormBuilder , FormControl , FormGroup , Validators } from '@angular/forms' ; import { CmdbMode } from '../../../framework/modes.enum' ; import { CmdbType } from '../../../framework/models/cmdb-type' ; import { TypeService } from '../../../framework/services/type.service' ; import { BehaviorSubject , Observable , ReplaySubject , Subscription } from 'rxjs' ; import { ExportdJobBaseStepComponent } from '../exportd-job-base-step.component' ; import { takeUntil } from 'rxjs/operators' ; @ Component ( { selector : 'cmdb-task-sources-step' , templateUrl : './exportd-job-sources-step.component.html' , styleUrls : [ './exportd-job-sources-step.component.scss' ] } ) export class ExportdJobSourcesStepComponent extends ExportdJobBaseStepComponent implements OnInit , OnDestroy { private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; @ Input ( ) set preData ( data ) { if ( data !== undefined ) { if ( data . sources ) { this . sourcesForm = this . formBuilder . group ( { sources : new FormArray ( [ ] ) } ) ; this . sourcesForm . removeControl ( 'sources' ) ; const forArray = this . formBuilder . array ( [ ] ) ; for ( const source of data . sources ) { forArray . push ( this . formBuilder . group ( { type_id : new FormControl ( '' , Validators . required ) , condition : this . formBuilder . array ( [ ] ) } ) ) ; } let i = 0 ; while ( i < forArray . controls . length ) { for ( const item of data . sources [ i ] . condition ) { const control = forArray . controls [ i ] . get ( 'condition' ) as FormArray ; control . push ( this . createCondition ( ) ) ; } i ++ ; } this . sourcesForm . addControl ( 'sources' , forArray ) ; this . sourcesForm . patchValue ( data ) ; } } } public operators : any [ ] = [ '!=' , '==' ] ; public sourcesForm : FormGroup ; readonly SOURCES = 'sources' ; constructor ( private formBuilder , private typeService ) { super ( ) ; } public ngOnInit ( ) { this . sourcesForm = this . formBuilder . group ( { sources : this . formBuilder . array ( [ this . createSource ( ) ] ) } ) ; if ( this . mode === CmdbMode . Create ) { this . delCondition ( 0 , 0 ) ; } } param publicID public loadDisplayType ( publicID ) { return this . typeService . getType ( publicID ) . pipe ( takeUntil ( this . subscriber ) ) ; } public getFields ( publicID ) { try { return this . types . find ( id => id . public_id === publicID ) . fields ; } catch ( e ) { return [ ] ; } } private createSource ( ) { return this . formBuilder . group ( { type_id : new FormControl ( null , Validators . required ) , condition : this . formBuilder . array ( [ this . createCondition ( ) ] ) } ) ; } private createCondition ( ) { return this . formBuilder . group ( { name : new FormControl ( '' , Validators . required ) , value : new FormControl ( '' , Validators . required ) , operator : new FormControl ( '' , Validators . required ) } ) ; } private getSourceAsFormArray ( ) { return this . sourcesForm . controls [ this . SOURCES ] as FormArray ; } public addSource ( ) { const control = this . getSourceAsFormArray ( ) ; control . push ( this . createSource ( ) ) ; } public addCondition ( event ) { const control = this . getSourceAsFormArray ( ) . at ( event ) . get ( 'condition' ) as FormArray ; control . push ( this . createCondition ( ) ) ; } public delSource ( index ) { const control = this . getSourceAsFormArray ( ) ; control . removeAt ( index ) ; } public delCondition ( index , event ) { const control = this . getSourceAsFormArray ( ) . at ( event ) . get ( 'condition' ) as FormArray ; control . removeAt ( index ) ; } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }