All files / app/layout layout.component.ts

98.3% Statements 58/59
91.66% Branches 22/24
100% Functions 15/15
98.18% Lines 54/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 1302x 2x 2x 2x 2x 2x                     2x 2x 2x 2x 2x   2x                               2x 27x 27x 27x 27x 27x 27x   27x   27x       53x         17x 17x         27x 27x 27x     27x 27x       1x       27x       27x   27x   27x 23x     27x   27x 3x 3x   3x 2x   1x     3x 3x     27x 27x 25x 25x         27x   53x       6x 1x     6x 5x          
import { HeaderComponent } from './header/header.component';
import { PictureLightboxComponent } from './picture-lightbox/picture-lightbox.component';
import { RightSidebarComponent } from './right-sidebar/right-sidebar.component';
import { SidebarNavComponent } from './sidebar-nav/sidebar-nav.component';
import { VersionDisplayComponent } from './version-display/version-display.component';
import {
    ChangeDetectionStrategy,
    Component,
    computed,
    DestroyRef,
    ElementRef,
    inject,
    OnInit,
    signal,
    viewChild,
} from '@angular/core';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router } from '@angular/router';
import { DrawerService, SidebarService, WINDOW } from '@drevo-web/core';
import { BREAKPOINT_TABLET, NavigationProgressComponent } from '@drevo-web/ui';
import { filter, map, of, switchMap, timer } from 'rxjs';
 
const NAVIGATION_DEBOUNCE_MS = 100;
 
@Component({
    selector: 'app-layout',
    imports: [
        HeaderComponent,
        PictureLightboxComponent,
        SidebarNavComponent,
        VersionDisplayComponent,
        RightSidebarComponent,
        NavigationProgressComponent,
    ],
    templateUrl: './layout.component.html',
    styleUrl: './layout.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LayoutComponent implements OnInit {
    private readonly sidebarService = inject(SidebarService);
    private readonly drawerService = inject(DrawerService);
    private readonly router = inject(Router);
    private readonly destroyRef = inject(DestroyRef);
    private readonly window = inject(WINDOW);
    private readonly contentEl = viewChild<ElementRef<HTMLElement>>('contentEl');
 
    readonly hasActions = computed(() => this.sidebarService.actions().length > 0);
 
    readonly isNavigating = toSignal(
        this.router.events.pipe(
            filter(
                e =>
                    e instanceof NavigationStart ||
                    e instanceof NavigationEnd ||
                    e instanceof NavigationCancel ||
                    e instanceof NavigationError
            ),
            map(e => e instanceof NavigationStart),
            switchMap(navigating => (navigating ? timer(NAVIGATION_DEBOUNCE_MS).pipe(map(() => true)) : of(false)))
        ),
        { initialValue: false }
    );
 
    readonly isDrawerOpen = this.drawerService.isOpen;
    readonly isMobile = signal(false);
    readonly skipTransition = signal(false);
 
    ngOnInit(): void {
        this.trackMobileBreakpoint();
        this.handleNavigationEnd();
    }
 
    closeDrawer(): void {
        this.drawerService.close();
    }
 
    private trackMobileBreakpoint(): void {
        Iif (!this.window) {
            return;
        }
 
        const mediaQuery = this.window.matchMedia(`(max-width: ${BREAKPOINT_TABLET - 1}px)`);
 
        this.isMobile.set(mediaQuery.matches);
 
        if (!mediaQuery.matches) {
            this.drawerService.restoreSaved();
        }
 
        let rafId = 0;
 
        const handler = (e: MediaQueryListEvent): void => {
            this.skipTransition.set(true);
            this.isMobile.set(e.matches);
 
            if (e.matches) {
                this.drawerService.close();
            } else {
                this.drawerService.restoreSaved();
            }
 
            cancelAnimationFrame(rafId);
            rafId = requestAnimationFrame(() => this.skipTransition.set(false));
        };
 
        mediaQuery.addEventListener('change', handler);
        this.destroyRef.onDestroy(() => {
            cancelAnimationFrame(rafId);
            mediaQuery.removeEventListener('change', handler);
        });
    }
 
    private handleNavigationEnd(): void {
        this.router.events
            .pipe(
                filter((event): event is NavigationEnd => event instanceof NavigationEnd),
                takeUntilDestroyed(this.destroyRef)
            )
            .subscribe(event => {
                if (this.isMobile()) {
                    this.drawerService.close();
                }
 
                if (!new URL(event.urlAfterRedirects, 'http://_').hash) {
                    this.contentEl()?.nativeElement.scrollTo?.(0, 0);
                }
            });
    }
}