import { Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs' ; import { CmdbCategoryTree } from '../../framework/models/cmdb-category' ; import { CategoryService } from '../../framework/services/category.service' ; import { ObjectService } from '../../framework/services/object.service' ; import { SidebarTypeComponent } from '../structure/sidebar/sidebar-type.component' ; @ Injectable ( { providedIn : 'root' } ) export class SidebarService { /**
     * Basic tree observer.
     */ private categoryTreeObserver = new BehaviorSubject < CmdbCategoryTree > ( new CmdbCategoryTree ( ) ) ; private sideBarType : SidebarTypeComponent [ ] = [ ] ; constructor ( private categoryService , private objectService ) { this . loadCategoryTree ( ) ; } /**
     * Get the subject of the current category tree.
     */ public get categoryTree ( ) : BehaviorSubject < CmdbCategoryTree > { return this . categoryTreeObserver ; } /**
     * Load Category Tree  CmdbCategoryTree .
      public loadCategoryTree ( ) { this . categoryService . getCategoryTree ( ) . subscribe ( ( tree ) => { this . categoryTreeObserver . next ( tree ) ; } ) ; } param typeID private async getObjectCount ( typeID ) { return new Promise ( ( resolve ) => { this . objectService . countObjectsByType ( typeID ) . subscribe ( ( data ) => { resolve ( data ) ; } ) ; } ) ; } param typeID public async updateTypeCounter ( typeID ) { const sidebarType = this . sideBarType . filter ( type => type . type . public_id === typeID ) . pop ( ) ; await this . getObjectCount ( sidebarType . type . public_id ) . then ( count => { sidebarType . objectCounter = count ; } ) ; } param sidebarType public initializeCounter ( sidebarType ) { this . sideBarType . push ( sidebarType ) ; this . updateTypeCounter ( sidebarType . type . public_id ) ; } param sidebarType public deleteCounter ( sidebarType ) { this . sideBarType = this . sideBarType . filter ( type => type !== sidebarType ) ; } }