import { Injectable , OnDestroy } from '@angular/core' ; import { BehaviorSubject , Subscription } from 'rxjs' ; import { filter } from 'rxjs/operators' ; import { CardTabComponent } from './card-tab/card-tab.component' ; @ Injectable ( ) export class CardTabsService implements OnDestroy { tab$ = new BehaviorSubject < CardTabComponent > ( null ) ; tabs$ = new BehaviorSubject < CardTabComponent [ ] > ( [ ] ) ; position$ = new BehaviorSubject < string > ( 'top' ) ; private _subscription : Subscription ; constructor ( ) { this . _subscription = this . tabs$ . pipe ( filter ( tabs => ! this . tab$ . value || ! tabs . find ( tab => tab === this . tab$ . value ) ) ) . subscribe ( tabs => this . tab$ . next ( tabs . length > 0 ? tabs [ 0 ] : null ) ) ; } ngOnDestroy ( ) { this . _subscription . unsubscribe ( ) ; } /**
     * Add a tab to the list of tabs
     */ addTab ( tab ) { this . tabs$ . next ( [ ... this . tabs$ . value , tab ] ) ; } /**
     * Remove a tab from the list
     */ removeTab ( tab ) { this . tabs$ . next ( this . tabs$ . value . filter ( _tab => _tab !== tab ) ) ; } /**
     * Select the tab
     */ select ( tab ) { this . tab$ . next ( tab ) ; } /**
     * Set the position of the tab content
     */ setPosition ( position ) { this . position$ . next ( position ) ; } }