import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'; import { MatMenu } from '@angular/material/menu'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { TreoHorizontalNavigationComponent } from '@treo/components/navigation/horizontal/horizontal.component'; import { TreoNavigationService } from '@treo/components/navigation/navigation.service'; import { TreoNavigationItem } from '@treo/components/navigation/navigation.types'; @Component({ selector : 'treo-horizontal-navigation-branch-item', templateUrl : './branch.component.html', styles : [], changeDetection: ChangeDetectionStrategy.OnPush }) export class TreoHorizontalNavigationBranchItemComponent implements OnInit, OnDestroy { // Child @Input() child: boolean; // Item @Input() item: TreoNavigationItem; // Mat menu @ViewChild('matMenu', {static: true}) matMenu: MatMenu; // Name @Input() name: string; // Private private _treoHorizontalNavigationComponent: TreoHorizontalNavigationComponent; private _unsubscribeAll: Subject; /** * Constructor * * @param {TreoNavigationService} _treoNavigationService * @param {ChangeDetectorRef} _changeDetectorRef */ constructor( private _treoNavigationService: TreoNavigationService, private _changeDetectorRef: ChangeDetectorRef ) { // Set the private defaults this._unsubscribeAll = new Subject(); // Set the defaults this.child = false; } // ----------------------------------------------------------------------------------------------------- // @ Lifecycle hooks // ----------------------------------------------------------------------------------------------------- /** * On init */ ngOnInit(): void { // Get the parent navigation component this._treoHorizontalNavigationComponent = this._treoNavigationService.getComponent(this.name); // Subscribe to onRefreshed on the navigation component this._treoHorizontalNavigationComponent.onRefreshed.pipe( takeUntil(this._unsubscribeAll) ).subscribe(() => { // Mark for check this._changeDetectorRef.markForCheck(); }); } /** * On destroy */ ngOnDestroy(): void { // Unsubscribe from all subscriptions this._unsubscribeAll.next(); this._unsubscribeAll.complete(); } // ----------------------------------------------------------------------------------------------------- // @ Public methods // ----------------------------------------------------------------------------------------------------- /** * Trigger the change detection */ triggerChangeDetection(): void { // Mark for check this._changeDetectorRef.markForCheck(); } }