import { Component , Directive , Input , OnChanges , OnInit } from '@angular/core' ; import { RenderResult } from '../../../models/cmdb-render' ; import { CollectionParameters } from '../../../../services/models/api-parameter' ; import { APIGetMultiResponse } from '../../../../services/models/api-response' ; import { ObjectService } from '../../../services/object.service' ; import { Subject } from 'rxjs' ; interface TypeRef { typeID : number ; typeLabel : string ; typeName : string ; occurences : number ; hidden : boolean ; } @ Component ( { selector : 'cmdb-object-references' , templateUrl : './object-references.component.html' , styleUrls : [ './object-references.component.scss' ] } ) export class ObjectReferencesComponent implements OnChanges { /**
     * The public id of the current object
     */ @ Input ( ) publicID : number ; /**
     * The total number of references to this object
     */ totalReferences : number = 0 ; /**
     * Serves as event emitter for the references by type tabs
     */ clickSubject : Subject < number > = new Subject < number > ( ) ; /**
     * List of referenced Types
     */ referencedTypes : Array < TypeRef > = [ ] ; constructor ( public objectService ) { } private public ngOnChanges ( ) { const params = { filter : undefined , limit : 0 , sort : 'public_id' , order : 1 , page : 1 } ; const referenceSubscription = this . objectService . getObjectReferences ( this . publicID , params ) . subscribe ( ( apiResponse ) => { this . sortReferencesByType ( apiResponse . results as Array < RenderResult > ) ; referenceSubscription . unsubscribe ( ) ; } ) ; } param type onSelect ( type ) { type . hidden = ! type . hidden ; } /**
     * Resets the hidden Tabs
     */ onReset ( ) { this . referencedTypes . forEach ( ( type ) => { type . hidden = false ; } ) ; } param event onClick ( event ) { this . clickSubject . next ( event ) ; } param event sortReferencesByType ( event ) { this . referencedTypes = [ ] ; let objectList = Array . from ( event ) ; this . totalReferences = objectList . length ; while ( objectList . length > 0 ) { const typeID = objectList [ 0 ] . type_information . type_id ; const typeLabel = objectList [ 0 ] . type_information . type_label ; const typeName = objectList [ 0 ] . type_information . type_name ; const occurences = objectList . filter ( object => object . type_information . type_id === typeID ) . length ; this . referencedTypes . push ( { typeID , typeLabel , typeName , occurences , hidden : false } ) ; objectList = objectList . filter ( object => object . type_information . type_id !== typeID ) ; this . sortRefTabs ( ) ; } } /**
     * Sorts the referenced types by occurence or alphabetical if occurences are the same
     */ sortRefTabs ( ) { this . referencedTypes = this . referencedTypes . sort ( ( a , b ) => { if ( a . occurences > b . occurences ) { return - 1 ; } else if ( a . occurences < b . occurences ) { return 1 ; } else { if ( a . typeLabel > b . typeLabel ) { return 1 ; } else if ( a . typeLabel < b . typeLabel ) { return - 1 ; } } return 0 ; } ) ; } }