import { Component , OnDestroy , OnInit , TemplateRef , ViewChild } from '@angular/core' ; import { Right , SecurityLevel } from '../models/right' ; import { ActivatedRoute } from '@angular/router' ; import { ReplaySubject } from 'rxjs' ; import { Column , Sort } from '../../layout/table/table.types' ; import { takeUntil } from 'rxjs/operators' ; import { APIGetMultiResponse } from '../../services/models/api-response' ; import { RightService } from '../services/right.service' ; import { CollectionParameters } from '../../services/models/api-parameter' ; @ Component ( { selector : 'cmdb-rights' , templateUrl : './rights.component.html' , styleUrls : [ './rights.component.scss' ] } ) export class RightsComponent implements OnInit , OnDestroy { private private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; /**
     * Component title h1.
     */ public readonly title : string = 'Right' ; /**
     * Title small.
     */ public readonly description : string = 'Management' ; /**
     * List of all rights, loaded from backend.
     */ public rights : Array < Right > = [ ] ; /**
     * Defined security levels
     */ public securityLevels : SecurityLevel ; /**
     * Table columns.
     */ public columns : Array < Column > = [ ] ; private private params : CollectionParameters = { filter : undefined , limit : 0 , sort : 'name' , order : 1 , page : 1 } ; /**
     * Table Template: level column.
     */ @ ViewChild ( 'levelTemplate' , { static : true } ) public levelTemplate : TemplateRef < any > ; /**
     * Table Template: groups with rights column.
     */ @ ViewChild ( 'groupsRightTemplate' , { static : true } ) public groupsRightTemplate : TemplateRef < any > ; constructor ( private route , private rightService ) { this . rights = this . route . snapshot . data . rights as Array < Right > ; } public ngOnInit ( ) { this . columns = [ { display : 'Name' , name : 'name' , data : 'name' , sortable : true } , { display : 'Label' , name : 'label' , data : 'label' , sortable : true } , { display : 'Description' , name : 'description' , data : 'description' , sortable : true } , { display : 'Level' , name : 'level' , data : 'level' , sortable : true , render ( data ) { if ( ! data ) { return '0' ; } return data ; } , template : this . levelTemplate } , { display : 'Groups' , name : 'groups' , data : 'name' , sortable : false , template : this . groupsRightTemplate } ] as Array < Column > ; } /**
     * Loads the rights from the api based on the params.
     */ public loadRightsFromAPI ( ) { this . rightService . getRights ( this . params ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( response ) => { this . rights = response . results as Array < Right > ; } ) ; } param sort public onSortChange ( sort ) { this . params . sort = sort . name ; this . params . order = sort . order ; this . loadRightsFromAPI ( ) ; } /**
     * Un-subscribe on component close.
     */ public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }