import { Component , EventEmitter , Input , OnDestroy , OnInit , Output } from '@angular/core' ; import { FormControl , FormGroup } from '@angular/forms' ; import { ReplaySubject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; import { Group } from '../../../../management/models/group' ; import { AccessControlList } from '../../../../acl/acl.types' ; import { TypeBuilderStepComponent } from '../type-builder-step.component' ; import { CmdbType } from '../../../models/cmdb-type' ; @ Component ( { selector : 'cmdb-type-acl-step' , templateUrl : './type-acl-step.component.html' , styleUrls : [ './type-acl-step.component.scss' ] } ) export class TypeAclStepComponent extends TypeBuilderStepComponent implements OnInit , OnDestroy { private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; @ Input ( ) public groups : Array < Group > = [ ] ; private wasEmpty : boolean = true ; @ Input ( 'typeInstance' ) public set TypeInstance ( instance ) { this . typeInstance = instance ; if ( this . typeInstance . acl ) { this . form . patchValue ( this . typeInstance . acl ) ; } } @ Output ( ) public isEmpty : EventEmitter < boolean > = new EventEmitter < boolean > ( ) ; public form : FormGroup ; constructor ( ) { super ( ) ; this . form = new FormGroup ( { activated : new FormControl ( false ) , groups : new FormGroup ( { includes : new FormGroup ( { } ) } ) } ) ; } public get activatedStatus ( ) : boolean { return this . form . get ( 'activated' ) . value ; } public get groupsControl ( ) : FormGroup { return this . form . get ( 'groups' ) as FormGroup ; } public get includesControl ( ) : FormGroup { return this . groupsControl . get ( 'includes' ) as FormGroup ; } public onAddChange ( event ) { this . wasEmpty = ( ! event [ 0 ] || event [ 0 ] . length === 0 ) && ! event [ 1 ] ; this . isEmpty . emit ( this . wasEmpty ) ; } public ngOnInit ( ) { this . form . statusChanges . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( ) => { if ( ! this . form . get ( 'activated' ) . value ) { this . isEmpty . emit ( true ) ; this . validateChange . emit ( true ) ; } else { this . isEmpty . emit ( this . wasEmpty ) ; this . validateChange . emit ( this . form . valid ) ; } } ) ; this . form . valueChanges . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( ) => { this . typeInstance . acl = this . form . getRawValue ( ) as AccessControlList ; } ) ; } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }