import { Component , Inject , OnInit , OnDestroy } from '@angular/core' ; import { ActivatedRoute , Router , NavigationEnd } from '@angular/router' ; import { Subscription } from 'rxjs' ; import { AppData } from '../app.model' ; import { ExampleProperties } from './example-item/example-item.component' ; @ Component ( { selector : 'ui-jar-examples' , template : `
        <ng-container *ngFor="let example of examples">
            <ui-jar-example-item [example]="example" [attr.data-example-title]="example.title"></ui-jar-example-item>
        </ng-container>
    ` } ) export class ExamplesComponent implements OnInit , OnDestroy { examples : ExampleProperties [ ] = [ ] ; private routerSub : Subscription ; constructor ( private activatedRoute , private router , @ Inject ( 'AppData' ) private appData ) { } ngOnInit ( ) { this . routerSub = this . router . events . subscribe ( ( event ) => { if ( event instanceof NavigationEnd ) { this . createExamples ( ) ; } } ) ; this . createExamples ( ) ; } ngOnDestroy ( ) { if ( this . routerSub ) { this . routerSub . unsubscribe ( ) ; } } createExamples ( ) { const componentName = this . getCurrentComponentName ( ) ; this . examples = this . getComponentExamples ( componentName ) ; } private getComponentExamples ( componentKey ) { let moduleDependencyName = this . appData . components [ decodeURI ( componentKey ) ] . moduleDependencies [ 0 ] ; return this . appData . examples [ moduleDependencyName ] ; } private getCurrentComponentName ( ) { const lastUrlSegmentIndex = this . activatedRoute . snapshot . pathFromRoot [ 0 ] . firstChild . url . length - 1 ; return this . activatedRoute . snapshot . pathFromRoot [ 0 ] . firstChild . url [ lastUrlSegmentIndex ] . path ; } }