|
1 | | -import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; |
| 1 | +import { Component, NgZone, OnInit, OnDestroy, ViewChild } from '@angular/core'; |
2 | 2 | import { Router, NavigationEnd } from '@angular/router'; |
| 3 | +import { filter, takeUntil } from 'rxjs/operators'; |
| 4 | +import { Subject } from 'rxjs'; |
| 5 | + |
| 6 | +const SMALL_WIDTH_BREAKPOINT = 1240; |
3 | 7 |
|
4 | 8 | import { MdcAppBar } from '@angular-mdc/web'; |
5 | 9 |
|
6 | 10 | @Component({ |
7 | 11 | selector: 'app-layout', |
8 | 12 | templateUrl: './app-layout.html' |
9 | 13 | }) |
10 | | -export class AppLayout implements OnInit { |
| 14 | +export class AppLayout implements OnInit, OnDestroy { |
| 15 | + /** Emits whenever the component is destroyed. */ |
| 16 | + private _destroy = new Subject<void>(); |
| 17 | + |
| 18 | + private _mediaMatcher: MediaQueryList = matchMedia(`(max-width: ${SMALL_WIDTH_BREAKPOINT}px)`); |
| 19 | + |
| 20 | + @ViewChild('demoAppBarControls') demoAppBarControls; |
| 21 | + @ViewChild(MdcAppBar) appBar: MdcAppBar; |
| 22 | + |
11 | 23 | startVisible: boolean; |
12 | 24 | buttonVisible: boolean; |
13 | 25 | inputVisible: boolean; |
14 | 26 | listVisible: boolean; |
15 | 27 |
|
16 | | - mobile: boolean; |
17 | | - |
18 | 28 | navigationLinks = [ |
19 | 29 | { name: 'App Bar', route: 'app-bar-demo', icon: 'remove' }, |
20 | 30 | { name: 'Card', route: 'card-demo', icon: 'credit_card' }, |
@@ -59,25 +69,32 @@ export class AppLayout implements OnInit { |
59 | 69 | { name: 'Angular CLI', route: 'cli-guide' } |
60 | 70 | ]; |
61 | 71 |
|
62 | | - @ViewChild('appbar') appbar: MdcAppBar; |
63 | | - @ViewChild('demoAppBar') demoAppBar: ElementRef; |
| 72 | + constructor( |
| 73 | + private _router: Router, |
| 74 | + private _ngZone: NgZone) { |
| 75 | + this._mediaMatcher.addListener(mql => this._ngZone.run(() => this._mediaMatcher = mql)); |
| 76 | + } |
64 | 77 |
|
65 | | - constructor(private _router: Router) { } |
| 78 | + isScreenSmall(): boolean { |
| 79 | + return this._mediaMatcher.matches; |
| 80 | + } |
66 | 81 |
|
67 | | - ngOnInit() { |
68 | | - this._router.events.subscribe(event => { |
69 | | - if (this._router.url.includes('/app-bar-demo')) { |
70 | | - if (event instanceof NavigationEnd) { |
71 | | - this.demoAppBar.nativeElement.style.display = 'block'; |
| 82 | + ngOnInit(): void { |
| 83 | + this._router.events |
| 84 | + .pipe(takeUntil(this._destroy), |
| 85 | + filter(event => event instanceof NavigationEnd)) |
| 86 | + .subscribe(_ => { |
| 87 | + if (this._router.url.includes('/app-bar-demo')) { |
| 88 | + this.demoAppBarControls.nativeElement.style.display = 'block'; |
72 | 89 | } else { |
73 | | - this.demoAppBar.nativeElement.style.display = 'none'; |
74 | | - this.appbar.fixed = true; |
| 90 | + this.demoAppBarControls.nativeElement.style.display = 'none'; |
| 91 | + this.appBar.fixed = true; // reset to fixed after navigation off app-bar demo |
75 | 92 | } |
76 | | - } |
77 | | - }); |
| 93 | + }); |
| 94 | + } |
78 | 95 |
|
79 | | - if (window.screen.width <= 1200) { |
80 | | - this.mobile = true; |
81 | | - } |
| 96 | + ngOnDestroy(): void { |
| 97 | + this._destroy.next(); |
| 98 | + this._destroy.complete(); |
82 | 99 | } |
83 | 100 | } |
0 commit comments