import { AfterContentInit , ChangeDetectionStrategy , Component , ContentChildren , Input , OnDestroy , QueryList , } from '@angular/core' ; import { Subscription } from 'rxjs' ; import { AccordionPaneComponent } from './accordion-pane.component' ; /**
 * Accordion component. Children should be <m-accordion__pane>
 */ @ Component ( { selector : 'm-accordion' , changeDetection : ChangeDetectionStrategy . OnPush , template : `
    <ng-content></ng-content>
  ` , } ) export class AccordionComponent implements AfterContentInit , OnDestroy { param opened private @ Input ( 'opened' ) set _opened ( opened ) { this . opened = opened ; if ( this . initialized ) { this . sync ( ) ; } } /**
     * Currently opened pane index
     */ protected opened : number = 0 ; /**
     * Initialized flag
     */ protected initialized : boolean = false ; /**
     * Pane list
     */ @ ContentChildren ( AccordionPaneComponent ) protected panes : QueryList < AccordionPaneComponent > ; /**
     * Subscription to changes
     */ protected panesChangeSubscription : Subscription ; /**
     * Subscriptions to pane toggle events
     */ protected paneToggleSubscriptions : Array < Subscription > = [ ] ; /**
     * Sync and subscribe to pane changes
     */ ngAfterContentInit ( ) { this . sync ( ) ; this . panesChangeSubscription = this . panes . changes . subscribe ( ( ) => this . sync ( ) ) ; this . initialized = true ; } /**
     * Unsubscribe from everything
     */ ngOnDestroy ( ) { if ( this . panesChangeSubscription ) { this . panesChangeSubscription . unsubscribe ( ) ; } this . tearDown ( ) ; } todo param pane protected openPane ( pane ) { this . panes . toArray ( ) . forEach ( pane => pane . setOpened ( false ) ) ; pane . setOpened ( true ) ; } /**
     * Sync pane toggle event subscription list
     */ protected sync ( ) { this . tearDown ( ) ; this . panes . toArray ( ) . forEach ( ( pane , i ) => { pane . setOpened ( this . opened === i ) ; this . paneToggleSubscriptions . push ( pane . toggleEmitter . subscribe ( ( ) => { this . openPane ( pane ) ; } ) ) ; } ) ; } /**
     * Remove all pane toggle subscriptions
     */ protected tearDown ( ) { this . paneToggleSubscriptions . forEach ( paneToggleSubscription => paneToggleSubscription . unsubscribe ( ) ) ; this . paneToggleSubscriptions = [ ] ; } }