import { Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs' ; import { TabComponent } from './tab/tab.component' ; @ Injectable ( ) export class TabsetService { /** Store the list of tabs */ tabs : ReadonlyArray < TabComponent > = [ ] ; activeTab$ = new BehaviorSubject < TabComponent > ( null ) ; /** Store the manual state */ manual : boolean = false ; /** Update the array of tabs - required to preserve order */ update ( tabs ) { this . tabs = [ ... tabs ] ; } /** Select a tab (from user input) */ select ( tab ) { if ( tab . disabled ) { return ; } if ( this . manual ) { this . tabs . forEach ( _tab => _tab === tab ? _tab . activate ( ) : _tab . deactivate ( ) ) ; } else { this . activeTab$ . next ( tab ) ; } } /** Set tab active state */ setTabActive ( tab ) { if ( ! tab . disabled ) { this . activeTab$ . next ( tab ) ; } } /** Determine if there is a selected tab */ isTabActive ( ) { return this . activeTab$ . getValue ( ) !== null ; } /** Select the first non-disabled tab */ selectFirstTab ( ) { const tab = this . tabs . find ( _tab => ! _tab . disabled ) ; if ( tab ) { this . select ( tab ) ; } } }