Files
smartguide/webapp/src/app/shared/ui/app-layout/app-layout.component.ts
T
2026-11-12 23:01:38 +01:00

206 lines
5.9 KiB
TypeScript

import {
afterNextRender,
ChangeDetectorRef,
Component,
DestroyRef,
ElementRef,
OnDestroy,
OnInit,
ViewChild,
computed,
inject,
signal,
viewChild,
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatDrawer, MatSidenavModule } from '@angular/material/sidenav';
import { NavigationEnd, Router } from '@angular/router';
import { filter } from 'rxjs';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { MediaMatcher } from '@angular/cdk/layout';
import { AppMenuComponent } from '../app-menu/app-menu.component';
import { MatTooltipModule } from '@angular/material/tooltip';
import { CredentialService } from '../../services/credential.service';
import { AppSearchbarComponent } from '../app-searchbar/app-searchbar.component';
import { ResolverStateService } from '../../services/resolver-state.service';
import { BackNavigationService } from '../../services/back-navigation.service';
import { PageTitleService } from '../../services/page-title.service';
import { RouteScrollService } from '../../services/route-scroll.service';
import { LoadingComponent } from '../loading/loading.component';
import {
ARROW_BACK_ICON,
INSTITUTION_ICON,
MENU_ICON,
NEWS_ICON,
PATENT_ICON,
PERSONS_ICON,
PERSON_ICON,
PROJECT_ICON,
PUBLICATION_ICON,
SWITCH_ACCOUNT_ICON,
TOPIC_ICON,
HIERARCHY_ICON,
} from '../icons';
@Component({
selector: 'app-layout',
imports: [
CommonModule,
MatToolbarModule,
MatSidenavModule,
MatButtonModule,
MatIconModule,
AppMenuComponent,
MatTooltipModule,
AppSearchbarComponent,
LoadingComponent,
],
templateUrl: './app-layout.component.html',
styleUrl: './app-layout.component.scss',
standalone: true,
})
export class AppLayoutComponent implements OnInit, OnDestroy {
readonly SWITCH_ACCOUNT_ICON = SWITCH_ACCOUNT_ICON;
readonly MENU_ICON = MENU_ICON;
readonly ARROW_BACK_ICON = ARROW_BACK_ICON;
readonly PERSONS_ICON = PERSONS_ICON;
readonly PERSON_ICON = PERSON_ICON;
readonly PUBLICATION_ICON = PUBLICATION_ICON;
readonly PROJECT_ICON = PROJECT_ICON;
readonly INSTITUTION_ICON = INSTITUTION_ICON;
readonly PATENT_ICON = PATENT_ICON;
readonly NEWS_ICON = NEWS_ICON;
readonly HIERARCHY_ICON = HIERARCHY_ICON;
readonly TOPIC_ICON = TOPIC_ICON;
@ViewChild(MatDrawer, { static: false })
drawer?: MatDrawer;
mediaQuery: MediaQueryList;
protected resolverState = inject(ResolverStateService);
mediaQueryListener: () => void;
private readonly destroyRef = inject(DestroyRef);
private readonly backNavigation = inject(BackNavigationService);
private readonly scrollService = inject(RouteScrollService);
// Instantiated for its router subscription; keeps document.title in sync.
private readonly pageTitle = inject(PageTitleService);
private readonly contentScroller =
viewChild<ElementRef<HTMLElement>>('contentScroller');
private readonly currentPath = signal('/');
readonly showBack = computed(
() =>
this.backNavigation.hasLocalHandler() ||
!this.backNavigation.isRoot(this.currentPath()),
);
private readonly sidebarCollapsedStorageKey = 'inse.sidebar.collapsed';
sideNavOpen = false;
sideNavCollapsed = this.readSidebarCollapsedState();
constructor(
public credentialService: CredentialService,
public router: Router,
public changeDetector: ChangeDetectorRef,
media: MediaMatcher,
) {
this.mediaQuery = media.matchMedia('(max-width: 600px)');
this.currentPath.set(this.pathOf(this.router.url));
this.mediaQueryListener = () => {
this.sideNavOpen = !this.mediaQuery.matches;
if (this.mediaQuery.matches) {
this.sideNavCollapsed = false;
}
changeDetector.detectChanges();
};
this.router.events
.pipe(
filter(
(event): event is NavigationEnd => event instanceof NavigationEnd,
),
takeUntilDestroyed(this.destroyRef),
)
.subscribe((event) => {
this.currentPath.set(this.pathOf(event.urlAfterRedirects));
// On phones the drawer is an overlay; keep it from covering the
// freshly opened page.
if (this.mediaQuery.matches && this.sideNavOpen) {
this.sideNavOpen = false;
this.drawer?.close();
}
});
afterNextRender(() => {
const scroller = this.contentScroller()?.nativeElement;
if (scroller) this.scrollService.attach(scroller);
});
}
toggle() {
if (this.drawer) {
this.drawer.toggle();
}
}
private readSidebarCollapsedState(): boolean {
return localStorage.getItem(this.sidebarCollapsedStorageKey) === 'true';
}
isSidebarCollapsed(): boolean {
return this.sideNavCollapsed && !this.mediaQuery.matches;
}
toggleSidebarCollapsed(): void {
if (this.mediaQuery.matches) {
return;
}
this.sideNavCollapsed = !this.sideNavCollapsed;
localStorage.setItem(
this.sidebarCollapsedStorageKey,
String(this.sideNavCollapsed),
);
}
ngOnInit() {
this.sideNavOpen = !this.mediaQuery.matches;
this.mediaQuery.addEventListener('change', this.mediaQueryListener);
}
ngOnDestroy(): void {
this.mediaQuery.removeEventListener('change', this.mediaQueryListener);
}
async logout() {
const isImpersonating = this.credentialService.isImpersonating();
if (isImpersonating) {
await this.credentialService.stopImpersonating();
this.router.navigate(['/accounts']);
} else {
await this.credentialService.logoutAccount();
}
}
async stopImpersonating(): Promise<void> {
await this.credentialService.stopImpersonating();
this.router.navigate(['/accounts']);
}
navigateBack() {
this.backNavigation.back();
}
private pathOf(url: string): string {
return url.split('?')[0].split('#')[0] || '/';
}
}