mirror of
https://github.com/richard-loafle/fuse-angular.git
synced 2025-04-26 18:13:11 +00:00
161 lines
5.1 KiB
TypeScript
161 lines
5.1 KiB
TypeScript
import { Component, Input, OnDestroy, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
|
|
import { NavigationEnd, Router } from '@angular/router';
|
|
import { Subject } from 'rxjs';
|
|
import { filter, take, takeUntil } from 'rxjs/operators';
|
|
|
|
import { FuseNavigationService } from '@fuse/components/navigation/navigation.service';
|
|
import { FusePerfectScrollbarDirective } from '@fuse/directives/fuse-perfect-scrollbar/fuse-perfect-scrollbar.directive';
|
|
import { FuseSidebarService } from '@fuse/components/sidebar/sidebar.service';
|
|
|
|
@Component({
|
|
selector : 'navbar',
|
|
templateUrl : './navbar.component.html',
|
|
styleUrls : ['./navbar.component.scss'],
|
|
encapsulation: ViewEncapsulation.None
|
|
})
|
|
export class NavbarComponent implements OnInit, OnDestroy
|
|
{
|
|
// Layout
|
|
@Input()
|
|
layout;
|
|
|
|
fusePerfectScrollbarUpdateTimeout: any;
|
|
navigation: any;
|
|
|
|
// Private
|
|
private _fusePerfectScrollbar: FusePerfectScrollbarDirective;
|
|
private _unsubscribeAll: Subject<any>;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param {FuseNavigationService} _fuseNavigationService
|
|
* @param {FuseSidebarService} _fuseSidebarService
|
|
* @param {Router} _router
|
|
*/
|
|
constructor(
|
|
private _fuseNavigationService: FuseNavigationService,
|
|
private _fuseSidebarService: FuseSidebarService,
|
|
private _router: Router
|
|
)
|
|
{
|
|
// Set the defaults
|
|
this.layout = 'vertical';
|
|
|
|
// Set the private defaults
|
|
this._unsubscribeAll = new Subject();
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Accessors
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
// Directive
|
|
@ViewChild(FusePerfectScrollbarDirective)
|
|
set directive(theDirective: FusePerfectScrollbarDirective)
|
|
{
|
|
if ( !theDirective )
|
|
{
|
|
return;
|
|
}
|
|
|
|
this._fusePerfectScrollbar = theDirective;
|
|
|
|
// Update the scrollbar on collapsable item toggle
|
|
this._fuseNavigationService.onItemCollapseToggled
|
|
.pipe(takeUntil(this._unsubscribeAll))
|
|
.subscribe(() => {
|
|
this.fusePerfectScrollbarUpdateTimeout = setTimeout(() => {
|
|
this._fusePerfectScrollbar.update();
|
|
}, 310);
|
|
});
|
|
|
|
// Scroll to the active item position
|
|
this._router.events
|
|
.pipe(
|
|
filter((event) => event instanceof NavigationEnd),
|
|
take(1)
|
|
)
|
|
.subscribe(() => {
|
|
setTimeout(() => {
|
|
const activeNavItem = document.querySelector('navbar .nav-link.active');
|
|
|
|
if ( activeNavItem )
|
|
{
|
|
const activeItemOffsetTop = activeNavItem.offsetTop,
|
|
activeItemOffsetParentTop = activeNavItem.offsetParent.offsetTop,
|
|
scrollDistance = activeItemOffsetTop - activeItemOffsetParentTop - (48 * 3);
|
|
|
|
this._fusePerfectScrollbar.scrollToTop(scrollDistance);
|
|
}
|
|
});
|
|
}
|
|
);
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Lifecycle hooks
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* On init
|
|
*/
|
|
ngOnInit(): void
|
|
{
|
|
this._router.events
|
|
.pipe(
|
|
filter((event) => event instanceof NavigationEnd),
|
|
takeUntil(this._unsubscribeAll)
|
|
)
|
|
.subscribe(() => {
|
|
if ( this._fuseSidebarService.getSidebar('navbar') )
|
|
{
|
|
this._fuseSidebarService.getSidebar('navbar').close();
|
|
}
|
|
}
|
|
);
|
|
|
|
// Get current navigation
|
|
this._fuseNavigationService.onNavigationChanged
|
|
.pipe(filter(value => value !== null))
|
|
.subscribe(() => {
|
|
this.navigation = this._fuseNavigationService.getCurrentNavigation();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* On destroy
|
|
*/
|
|
ngOnDestroy(): void
|
|
{
|
|
if ( this.fusePerfectScrollbarUpdateTimeout )
|
|
{
|
|
clearTimeout(this.fusePerfectScrollbarUpdateTimeout);
|
|
}
|
|
|
|
// Unsubscribe from all subscriptions
|
|
this._unsubscribeAll.next();
|
|
this._unsubscribeAll.complete();
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Public methods
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Toggle sidebar opened status
|
|
*/
|
|
toggleSidebarOpened(): void
|
|
{
|
|
this._fuseSidebarService.getSidebar('navbar').toggleOpen();
|
|
}
|
|
|
|
/**
|
|
* Toggle sidebar folded status
|
|
*/
|
|
toggleSidebarFolded(): void
|
|
{
|
|
this._fuseSidebarService.getSidebar('navbar').toggleFold();
|
|
}
|
|
}
|