import { Component , OnDestroy , OnInit , Renderer2 } from '@angular/core' ; import { FormControl } from '@angular/forms' ; import { CmdbCategoryTree } from '../../../framework/models/cmdb-category' ; import { CategoryService } from '../../../framework/services/category.service' ; import { ReplaySubject , Subscription } from 'rxjs' ; import { CmdbType } from '../../../framework/models/cmdb-type' ; import { TypeService } from '../../../framework/services/type.service' ; import { SidebarService } from '../../services/sidebar.service' ; import { APIGetMultiResponse } from '../../../services/models/api-response' ; import { CollectionParameters } from '../../../services/models/api-parameter' ; import { takeUntil } from 'rxjs/operators' ; import { AccessControlPermission } from "../../../acl/acl.types" ; @ Component ( { selector : 'cmdb-sidebar' , templateUrl : './sidebar.component.html' , styleUrls : [ './sidebar.component.scss' ] , } ) export class SidebarComponent implements OnInit , OnDestroy { /**
     * Global un-subscriber for http calls to the rest backend.
     */ private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; /**
     * Category data
     */ public categoryTree : CmdbCategoryTree ; private categoryTreeSubscription : Subscription ; /**
     * Types params
     */ public typesParams : CollectionParameters = { filter : undefined , limit : 0 , sort : 'public_id' , order : 1 , page : 1 } ; /**
     * Type data
     */ public typeList : CmdbType [ ] = [ ] ; public unCategorizedTypes : CmdbType [ ] = [ ] ; private unCategorizedTypesSubscription : Subscription ; /**
     * Filter
     */ public filterTerm : FormControl = new FormControl ( '' ) ; private filterTermSubscription : Subscription ; constructor ( private sidebarService , private categoryService , private typeService , private renderer ) { this . categoryTreeSubscription = new Subscription ( ) ; this . unCategorizedTypesSubscription = new Subscription ( ) ; this . filterTermSubscription = new Subscription ( ) ; } public ngOnInit ( ) { this . renderer . addClass ( document . body , 'sidebar-fixed' ) ; this . sidebarService . loadCategoryTree ( ) ; this . categoryTreeSubscription = this . sidebarService . categoryTree . asObservable ( ) . subscribe ( ( categoryTree ) => { this . categoryTree = categoryTree ; this . unCategorizedTypesSubscription = this . typeService . getUncategorizedTypes ( AccessControlPermission . READ , false ) . subscribe ( ( apiResponse ) => { this . unCategorizedTypes = apiResponse . results as Array < CmdbType > ; } ) ; this . typeService . getTypes ( this . typesParams ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( apiResponse ) => { this . typeList = apiResponse . results as Array < CmdbType > ; } ) ; } ) ; } public ngOnDestroy ( ) { this . categoryTreeSubscription . unsubscribe ( ) ; this . unCategorizedTypesSubscription . unsubscribe ( ) ; this . filterTermSubscription . unsubscribe ( ) ; this . renderer . removeClass ( document . body , 'sidebar-fixed' ) ; } }