import { Component , Injectable , Input , OnDestroy , OnInit } from '@angular/core' ; import { CmdbMode } from '../../../framework/modes.enum' ; import { AbstractControl , FormControl , FormGroup , Validators } from '@angular/forms' ; import { checkJobExistsValidator , ExportdJobService } from '../../exportd-job.service' ; import { ExportdType } from '../../../settings/models/modes_job.enum' ; import { ExportdJobBaseStepComponent } from '../exportd-job-base-step.component' ; import { ReplaySubject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; @ Component ( { selector : 'cmdb-task-basic-step' , templateUrl : './exportd-job-basic-step.component.html' , styleUrls : [ './exportd-job-basic-step.component.scss' ] } ) export class ExportdJobBasicStepComponent extends ExportdJobBaseStepComponent implements OnInit , OnDestroy { private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; @ Input ( ) set preData ( data ) { if ( data !== undefined ) { this . basicForm . patchValue ( data ) ; } } public basicForm : FormGroup ; public readonly typeSelect : Array < any > = [ { label : 'PUSH' , content : ExportdType . PUSH , description : 'Run job directly.' } , { label : 'PULL' , content : ExportdType . PULL , description : 'Get the output of a job directly via REST.' } ] ; constructor ( private exportdService ) { super ( ) ; this . basicForm = new FormGroup ( { name : new FormControl ( '' , Validators . required ) , label : new FormControl ( '' , Validators . required ) , description : new FormControl ( '' ) , active : new FormControl ( true ) , exportd_type : new FormControl ( ExportdType . PUSH ) } ) ; } public get name ( ) { return this . basicForm . get ( 'name' ) ; } public get label ( ) { return this . basicForm . get ( 'label' ) ; } public ngOnInit ( ) { if ( this . mode === CmdbMode . Create ) { this . basicForm . get ( 'name' ) . setAsyncValidators ( checkJobExistsValidator ( this . exportdService ) ) ; this . basicForm . get ( 'label' ) . valueChanges . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( value => { this . basicForm . get ( 'name' ) . setValue ( value . replace ( / /g , '-' ) . toLowerCase ( ) ) ; this . basicForm . get ( 'name' ) . setValue ( this . basicForm . get ( 'name' ) . value . replace ( /[^a-z0-9 \-]/gi , '' ) . toLowerCase ( ) ) ; this . basicForm . get ( 'name' ) . markAsDirty ( { onlySelf : true } ) ; this . basicForm . get ( 'name' ) . markAsTouched ( { onlySelf : true } ) ; } ) ; } else if ( CmdbMode . Edit ) { this . basicForm . get ( 'name' ) . disable ( ) ; this . basicForm . markAllAsTouched ( ) ; } } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }