all files / component/sidenav/ sidenav.component.ts

80.39% Statements 41/51
50% Branches 1/2
46.15% Functions 6/13
78.26% Lines 36/46
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                                                                                                                 
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { ChangeDetectorRef, Component, Input, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute, ActivatedRouteSnapshot, NavigationEnd, Router } from '@angular/router';
import { from as observableFrom, Subject } from 'rxjs';
import { filter, map, mergeMap, takeUntil } from 'rxjs/operators';
import { MediaService } from '../../common/core/media/media.service';
import { DejaSidenavService } from './sidenav.service';
 
@Component({
    encapsulation: ViewEncapsulation.None,
    selector: 'deja-sidenav',
    template: require('./sidenav.component.html'),
    styles: [require('./sidenav.component.scss')]
})
export class DejaSidenavComponent implements OnInit, OnDestroy {
    @Input()
    public set showToolbar(value: boolean | string) {
        this._showToolbar = coerceBooleanProperty(value);
    }
 
    @Input()
    public headerText = 'TITLE';
 
    @Input()
    public headerIcon = 'face';
 
    public title: string;
    public mode = 'side';
    public _showToolbar = false;
 
    private ngUnsubscribe: Subject<void> = new Subject<void>();
 
    constructor(
        public sidenavService: DejaSidenavService,
        private mediaService: MediaService,
        private router: Router,
        private activatedRoute: ActivatedRoute,
        private changeDetectorRef: ChangeDetectorRef,
    ) {
 
        observableFrom(this.mediaService.mediaChanged$).pipe(
            takeUntil(this.ngUnsubscribe))
            .subscribe((alias) => {
                this.sidenavService.hidden = alias === 'xs';
                this.sidenavService.opened = alias === 'lg';
                this.sidenavService.mode = alias === 'xs' ? 'over' : 'side';
                this.changeDetectorRef.markForCheck();
            });
    }
 
    public ngOnInit() {
        // Initialize
        this.title = this.getActivatedRouteLastChild().data[`title`];
 
        // Listen for future route changes
        this.router.events.pipe(
            takeUntil(this.ngUnsubscribe),
            filter((event) => event instanceof NavigationEnd),
            map(() => this.activatedRoute),
            map((route) => {
                while (route.firstChild) {
                    route = route.firstChild;
                }
                return route;
            }),
            filter((route) => route.outlet === 'primary'),
            mergeMap((route) => route.data))
            .subscribe((event) => this.title = event[`title`]);
    }
 
    public ngOnDestroy() {
        this.ngUnsubscribe.next();
        this.ngUnsubscribe.complete();
    }
 
    private getActivatedRouteLastChild(): ActivatedRouteSnapshot {
        let route: ActivatedRouteSnapshot = this.activatedRoute.snapshot.root;
        while (route.firstChild) {
            route = route.firstChild;
        }
        return route;
    }
}