import { Component , EventEmitter , Input , OnChanges , OnDestroy , OnInit , Output , SimpleChanges } from '@angular/core' ; import { Group } from '../../../models/group' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { Subject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; import { groupNameExistsValidator , GroupService } from '../../../services/group.service' ; import { RightService } from '../../../services/right.service' ; import { Right } from '../../../models/right' ; import { APIGetMultiResponse } from '../../../../services/models/api-response' ; import { CollectionParameters } from '../../../../services/models/api-parameter' ; @ Component ( { selector : 'cmdb-group-form' , templateUrl : './group-form.component.html' , styleUrls : [ './group-form.component.scss' ] } ) export class GroupFormComponent implements OnInit , OnChanges , OnDestroy { private subscriber : Subject < void > ; public form : FormGroup ; /**
     * Passable group data if there are pre data.
     */ @ Input ( ) public group : Group ; /**
     * List of complete rights.
     */ @ Input ( ) public rights : Array < Right > = [ ] ; /**
     * Are rights loading.
     */ public rightLoading : boolean = false ; /**
     * Validation status event emitter.
     */ @ Output ( ) public validation : EventEmitter < boolean > ; /**
     * Form submit event emitter.
     */ @ Output ( ) public submit : EventEmitter < Group > ; constructor ( private groupService ) { this . subscriber = new Subject < void > ( ) ; this . validation = new EventEmitter < boolean > ( ) ; this . submit = new EventEmitter < Group > ( ) ; this . form = new FormGroup ( { name : new FormControl ( '' , [ Validators . required ] , [ groupNameExistsValidator ( this . groupService ) ] ) , label : new FormControl ( '' , [ Validators . required ] ) , rights : new FormControl ( [ ] ) } ) ; } /**
     * Init for group form component.
     * Subscribes to status changes.
     */ public ngOnInit ( ) { this . form . statusChanges . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( status => { this . validation . emit ( status ) ; } ) ; } param changes public ngOnChanges ( changes ) { if ( changes . group && changes . group . currentValue !== changes . group . previousValue ) { const groupRights = this . group . rights ; this . group . rights = [ ] ; for ( const r of groupRights ) { this . group . rights . push ( r . name ) ; } this . form . patchValue ( this . group ) ; this . form . markAllAsTouched ( ) ; } } /**
     * Get the form control for name.
     */ public get nameControl ( ) : FormControl { return this . form . get ( 'name' ) as FormControl ; } /**
     * Get the form control for label.
     */ public get labelControl ( ) : FormControl { return this . form . get ( 'label' ) as FormControl ; } /**
     * Get the rights array control.
     */ public get rightsControl ( ) : FormControl { return this . form . get ( 'rights' ) as FormControl ; } /**
     * Calls on ngSubmit of the form.
     * Triggers the submit event emitter.
     */ public onSubmit ( ) { this . form . markAllAsTouched ( ) ; const formGroup = this . form . getRawValue ( ) as Group ; this . group = { ... this . group , ... formGroup } ; return this . submit . emit ( formGroup ) ; } /**
     * Destroys the component.
     * Calls the auto unsubscriber.
     */ public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } param item public groupByFn ( item ) { const baseData = item . name . split ( '.' ) ; return `${ baseData [ 0 ] }.${ baseData [ 1 ] }.*` ; } }