import { Component , EventEmitter , Input , OnDestroy , Output } from '@angular/core' ; import { AccessControlListSection , AccessControlPermission } from '../../../../../acl/acl.types' ; import { Group } from '../../../../../management/models/group' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { ReplaySubject } from 'rxjs' ; @ Component ( { selector : 'cmdb-groups-acl-tabs' , templateUrl : './groups-acl-tabs.component.html' , styleUrls : [ './groups-acl-tabs.component.scss' ] } ) export class GroupsAclTabsComponent implements OnDestroy { private private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; /**
     * Event emitter when a value inside the form changes.
     */ @ Output ( ) public valueChange : EventEmitter < any > = new EventEmitter < any > ( ) ; @ Input ( ) public groups : Array < Group > = [ ] ; @ Input ( ) public form : FormGroup ; public groupsACL : AccessControlListSection < number > = new AccessControlListSection < number > ( ) ; public selectedGroup : Group ; public selectedPermissions : Array < AccessControlPermission > ; @ Input ( 'groupsACL' ) public set GroupsACL ( groups ) { if ( groups ) { this . groupsACL = groups ; for ( const entry in groups . includes ) { this . addControl ( entry ) ; } this . form . patchValue ( groups . includes ) ; } } public readonly operations : Array < string > = Object . keys ( AccessControlPermission ) ; public getGroupLabelByID ( publicID ) { const found = this . groups . find ( g => g . public_id === publicID ) ; if ( found ) { return found . label ; } return `GroupID: ${ publicID } does not exist` ; } /**
     * On html click event.
     */ public onAddControl ( ) { if ( this . selectedGroup ) { const controlName = `${ this . selectedGroup . public_id }` ; this . addControl ( controlName ) ; if ( this . selectedPermissions ) { this . form . get ( controlName ) . setValue ( this . selectedPermissions ) ; } this . selectedGroup = undefined ; this . selectedPermissions = undefined ; } this . valueChange . emit ( [ [ ] , null ] ) ; } /**
     * Add a new acl entry.
     */ public addControl ( name ) { const control = new FormControl ( [ ] ) ; control . setValidators ( [ Validators . required , Validators . minLength ( 1 ) ] ) ; this . form . addControl ( `${ name }` , control ) ; } public get controls ( ) { return this . form . controls ; } param name public removeControl ( name ) { this . form . removeControl ( name ) ; } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }