Files
psp_panel/src/app/layout/app.layout.component.ts
T

122 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-07-22 13:13:50 +03:00
import { Component, OnDestroy, Renderer2, ViewChild } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter, Subscription } from 'rxjs';
import { LayoutService } from "./service/app.layout.service";
import { AppSidebarComponent } from "./app.sidebar.component";
import { AppTopBarComponent } from './app.topbar.component';
@Component({
selector: 'app-layout',
templateUrl: './app.layout.component.html'
})
export class AppLayoutComponent implements OnDestroy {
overlayMenuOpenSubscription: Subscription;
menuOutsideClickListener: any;
2022-08-23 11:48:14 +03:00
profileMenuOutsideClickListener: any;
2022-07-22 13:13:50 +03:00
@ViewChild(AppSidebarComponent) appSidebar!: AppSidebarComponent;
2022-08-23 11:48:14 +03:00
@ViewChild(AppTopBarComponent) appTopbar!: AppTopBarComponent;
2022-08-24 01:01:38 +03:00
constructor(public layoutService: LayoutService, public renderer: Renderer2, public router: Router) {
2022-07-22 13:13:50 +03:00
this.overlayMenuOpenSubscription = this.layoutService.overlayOpen$.subscribe(() => {
if (!this.menuOutsideClickListener) {
this.menuOutsideClickListener = this.renderer.listen('document', 'click', event => {
const isOutsideClicked = !(this.appSidebar.el.nativeElement.isSameNode(event.target) || this.appSidebar.el.nativeElement.contains(event.target)
2022-08-24 00:52:27 +03:00
|| this.appTopbar.menuButton.nativeElement.isSameNode(event.target) || this.appTopbar.menuButton.nativeElement.contains(event.target));
2022-08-23 12:36:54 +03:00
2022-07-22 13:13:50 +03:00
if (isOutsideClicked) {
2022-08-23 12:36:54 +03:00
this.hideMenu();
2022-07-22 13:13:50 +03:00
}
});
2022-08-23 11:48:14 +03:00
}
if (!this.profileMenuOutsideClickListener) {
this.profileMenuOutsideClickListener = this.renderer.listen('document', 'click', event => {
2022-08-23 12:36:54 +03:00
const isOutsideClicked = !(this.appTopbar.menu.nativeElement.isSameNode(event.target) || this.appTopbar.menu.nativeElement.contains(event.target)
2022-08-24 00:52:27 +03:00
|| this.appTopbar.topbarMenuButton.nativeElement.isSameNode(event.target) || this.appTopbar.topbarMenuButton.nativeElement.contains(event.target));
2022-08-23 11:48:14 +03:00
2022-08-23 12:36:54 +03:00
if (isOutsideClicked) {
this.hideProfileMenu();
}
2022-08-23 11:48:14 +03:00
});
2022-07-22 13:13:50 +03:00
}
2022-08-23 12:36:54 +03:00
if (this.layoutService.state.staticMenuMobileActive) {
this.blockBodyScroll();
}
2022-07-22 13:13:50 +03:00
});
this.router.events.pipe(filter(event => event instanceof NavigationEnd))
.subscribe(() => {
2022-08-23 12:36:54 +03:00
this.hideMenu();
this.hideProfileMenu();
2022-07-22 13:13:50 +03:00
});
}
2022-08-23 12:36:54 +03:00
hideMenu() {
this.layoutService.state.overlayMenuActive = false;
this.layoutService.state.staticMenuMobileActive = false;
this.layoutService.state.menuHoverActive = false;
if (this.menuOutsideClickListener) {
this.menuOutsideClickListener();
this.menuOutsideClickListener = null;
}
this.unblockBodyScroll();
}
hideProfileMenu() {
this.layoutService.state.profileSidebarVisible = false;
if (this.profileMenuOutsideClickListener) {
this.profileMenuOutsideClickListener();
this.profileMenuOutsideClickListener = null;
}
}
2022-07-22 13:13:50 +03:00
blockBodyScroll(): void {
if (document.body.classList) {
document.body.classList.add('blocked-scroll');
}
else {
document.body.className += ' blocked-scroll';
}
}
unblockBodyScroll(): void {
if (document.body.classList) {
document.body.classList.remove('blocked-scroll');
}
else {
document.body.className = document.body.className.replace(new RegExp('(^|\\b)' +
'blocked-scroll'.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
}
get containerClass() {
return {
'layout-theme-light': this.layoutService.config().colorScheme === 'light',
'layout-theme-dark': this.layoutService.config().colorScheme === 'dark',
'layout-overlay': this.layoutService.config().menuMode === 'overlay',
'layout-static': this.layoutService.config().menuMode === 'static',
'layout-static-inactive': this.layoutService.state.staticMenuDesktopInactive && this.layoutService.config().menuMode === 'static',
2022-07-22 13:13:50 +03:00
'layout-overlay-active': this.layoutService.state.overlayMenuActive,
'layout-mobile-active': this.layoutService.state.staticMenuMobileActive,
'p-input-filled': this.layoutService.config().inputStyle === 'filled',
'p-ripple-disabled': !this.layoutService.config().ripple
2022-07-22 13:13:50 +03:00
}
}
ngOnDestroy() {
if (this.overlayMenuOpenSubscription) {
this.overlayMenuOpenSubscription.unsubscribe();
}
if (this.menuOutsideClickListener) {
this.menuOutsideClickListener();
}
}
}