import { Component , OnInit } from '@angular/core' ; import { ConfigEditBaseComponent } from '../config.edit' ; import { ReplaySubject } from 'rxjs' ; import { FormControl , Validators } from '@angular/forms' ; import { ValidRegexValidator } from '../../../../../layout/validators/valid-regex-validator' ; @ Component ( { selector : 'cmdb-choice-field-edit' , templateUrl : './choice-field-edit.component.html' } ) export class ChoiceFieldEditComponent extends ConfigEditBaseComponent implements OnInit { protected protected subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; /**
     * Required form control.
     */ public requiredControl : FormControl = new FormControl ( false ) ; /**
     * Name form control.
     */ public nameControl : FormControl = new FormControl ( '' , Validators . required ) ; /**
     * Label form control.
     */ public labelControl : FormControl = new FormControl ( '' , Validators . required ) ; /**
     * Description form control.
     */ public descriptionControl : FormControl = new FormControl ( '' ) ; /**
     * Helper form control.
     */ public helperTextControl : FormControl = new FormControl ( '' ) ; /**
     * Options form control.
     */ public optionsControl : FormControl = new FormControl ( [ ] ) ; /**
     * Helper form control.
     */ public valueControl : FormControl = new FormControl ( ) ; /**
     * Add able options for choice selection
     */ public options : Array < any > = [ ] ; constructor ( ) { super ( ) ; } public ngOnInit ( ) { this . options = this . data . options ; if ( this . options === undefined || ! Array . isArray ( this . options ) ) { this . options = [ ] ; this . options . push ( { name : `option-${ ( this . options . length + 1 ) }` , label : `Option ${ ( this . options . length + 1 ) }` } ) ; this . data . options = this . options ; } this . form . addControl ( 'required' , this . requiredControl ) ; this . form . addControl ( 'name' , this . nameControl ) ; this . form . addControl ( 'label' , this . labelControl ) ; this . form . addControl ( 'description' , this . descriptionControl ) ; this . form . addControl ( 'helperText' , this . helperTextControl ) ; this . form . addControl ( 'value' , this . valueControl ) ; this . form . addControl ( 'options' , this . optionsControl ) ; this . disableControlOnEdit ( this . nameControl ) ; this . patchData ( this . data , this . form ) ; } /**
     * Adds a new option with default prefix
     */ public addOption ( ) { this . options . push ( { name : `option-${ ( this . options . length + 1 ) }` , label : `Option ${ ( this . options . length + 1 ) }` } ) ; } param value public delOption ( value ) { if ( this . options . length > 1 ) { const index = this . options . indexOf ( value , 0 ) ; if ( index > - 1 ) { this . options . splice ( index , 1 ) ; } } } }