import { AfterViewInit , ChangeDetectorRef , Component , HostListener , OnInit } from '@angular/core' ; import { ActivatedRoute } from '@angular/router' ; import { ICategory } from '../../interfaces/ICategory' ; import { NavigationService } from '../../services/navigation/navigation.service' ; @ Component ( { selector : 'uxd-documentation-category' , templateUrl : './documentation-category.component.html' } ) export class DocumentationCategoryComponent implements OnInit , AfterViewInit { category : ICategory ; private trackScroll : boolean = false ; constructor ( private _activatedRoute , private _navigation , private _changeDetectorRef ) { } ngOnInit ( ) { this . category = this . _activatedRoute . snapshot . data [ 'category' ] ; this . _navigation . setSectionIds ( this . category . sections ) ; } ngAfterViewInit ( ) { this . trackScroll = true ; } @ HostListener ( 'window:scroll' ) onWindowScroll ( ) { if ( this . trackScroll ) { setTimeout ( ( ) => this . updateActiveSection ( ) ) ; } } private updateActiveSection ( ) { let activeSection = null ; let replaceOnly = false ; let updateUrl = true ; for ( let section of this . category . sections ) { if ( this . _navigation . isFragmentActive ( section . id ) ) { activeSection = section ; updateUrl = ! this . _navigation . isScrolledToBottom ( ) ; break ; } } if ( activeSection === null ) { activeSection = this . category . sections [ 0 ] ; replaceOnly = true ; } this . _navigation . setActiveFragment ( activeSection . id , replaceOnly , updateUrl ) ; this . _changeDetectorRef . markForCheck ( ) ; } }