import { Component , EventEmitter , Input , OnDestroy , Output } from '@angular/core' ; import { CmdbCategory , CmdbCategoryNode } from '../../../models/cmdb-category' ; import { CmdbMode } from '../../../modes.enum' ; import { NgbModal } from '@ng-bootstrap/ng-bootstrap' ; import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' ; import { CategoryService } from '../../../services/category.service' ; import { Router } from '@angular/router' ; import { DeleteCategoryModalComponent } from '../modals/delete-category-modal/delete-category-modal.component' ; import { ReplaySubject } from "rxjs" ; import { takeUntil } from "rxjs/operators" ; @ Component ( { selector : 'cmdb-category-node' , templateUrl : './category-node.component.html' , styleUrls : [ './category-node.component.scss' ] } ) export class CategoryNodeComponent implements OnDestroy { /**
     * Edit mode of tree
     */ @ Input ( ) public mode : CmdbMode = CmdbMode . View ; /**
     * Current node element
     */ @ Input ( ) public node : CmdbCategoryNode ; /**
     * Node change emitter
     */ @ Output ( ) public change : EventEmitter < { type : string ; value : any ; } > ; /**
     * Global unsubscriber for http calls to the rest backend.
     */ private unSubscribe : ReplaySubject < void > = new ReplaySubject ( ) ; private deleteRef : NgbModalRef ; public constructor ( private deleteModal , private router , private categoryService ) { this . change = new EventEmitter < { type : string ; value : any ; } > ( ) ; } public onDelete ( category ) { this . deleteRef = this . deleteModal . open ( DeleteCategoryModalComponent ) ; this . deleteRef . componentInstance . category = category ; this . deleteRef . result . then ( ( result ) => { if ( result === 'delete' ) { this . categoryService . deleteCategory ( category . public_id ) . pipe ( takeUntil ( this . unSubscribe ) ) . subscribe ( ( ) => { this . change . emit ( ) ; } ) ; } } ) ; } public ngOnDestroy ( ) { this . unSubscribe . next ( ) ; this . unSubscribe . complete ( ) ; } }