UNPKG

4.27 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google LLC All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { OnDestroy } from '@angular/core';
9import { Observable } from 'rxjs';
10import { MediaChange } from '../media-change';
11import { MatchMedia } from '../match-media/match-media';
12import { PrintHook } from '../media-marshaller/print-hook';
13import { BreakPointRegistry } from '../breakpoints/break-point-registry';
14import * as i0 from "@angular/core";
15/**
16 * MediaObserver enables applications to listen for 1..n mediaQuery activations and to determine
17 * if a mediaQuery is currently activated.
18 *
19 * Since a breakpoint change will first deactivate 1...n mediaQueries and then possibly activate
20 * 1..n mediaQueries, the MediaObserver will debounce notifications and report ALL *activations*
21 * in 1 event notification. The reported activations will be sorted in descending priority order.
22 *
23 * This class uses the BreakPoint Registry to inject alias information into the raw MediaChange
24 * notification. For custom mediaQuery notifications, alias information will not be injected and
25 * those fields will be ''.
26 *
27 * Note: Developers should note that only mediaChange activations (not de-activations)
28 * are announced by the MediaObserver.
29 *
30 * @usage
31 *
32 * // RxJS
33 * import { filter } from 'rxjs/operators';
34 * import { MediaObserver } from '@angular/flex-layout';
35 *
36 * @Component({ ... })
37 * export class AppComponent {
38 * status: string = '';
39 *
40 * constructor(mediaObserver: MediaObserver) {
41 * const media$ = mediaObserver.asObservable().pipe(
42 * filter((changes: MediaChange[]) => true) // silly noop filter
43 * );
44 *
45 * media$.subscribe((changes: MediaChange[]) => {
46 * let status = '';
47 * changes.forEach( change => {
48 * status += `'${change.mqAlias}' = (${change.mediaQuery}) <br/>` ;
49 * });
50 * this.status = status;
51 * });
52 *
53 * }
54 * }
55 */
56export declare class MediaObserver implements OnDestroy {
57 protected breakpoints: BreakPointRegistry;
58 protected matchMedia: MatchMedia;
59 protected hook: PrintHook;
60 /** Filter MediaChange notifications for overlapping breakpoints */
61 filterOverlaps: boolean;
62 constructor(breakpoints: BreakPointRegistry, matchMedia: MatchMedia, hook: PrintHook);
63 /**
64 * Completes the active subject, signalling to all complete for all
65 * MediaObserver subscribers
66 */
67 ngOnDestroy(): void;
68 /**
69 * Observe changes to current activation 'list'
70 */
71 asObservable(): Observable<MediaChange[]>;
72 /**
73 * Allow programmatic query to determine if one or more media query/alias match
74 * the current viewport size.
75 * @param value One or more media queries (or aliases) to check.
76 * @returns Whether any of the media queries match.
77 */
78 isActive(value: string | string[]): boolean;
79 /**
80 * Register all the mediaQueries registered in the BreakPointRegistry
81 * This is needed so subscribers can be auto-notified of all standard, registered
82 * mediaQuery activations
83 */
84 private watchActivations;
85 /**
86 * Only pass/announce activations (not de-activations)
87 *
88 * Since multiple-mediaQueries can be activation in a cycle,
89 * gather all current activations into a single list of changes to observers
90 *
91 * Inject associated (if any) alias information into the MediaChange event
92 * - Exclude mediaQuery activations for overlapping mQs. List bounded mQ ranges only
93 * - Exclude print activations that do not have an associated mediaQuery
94 *
95 * NOTE: the raw MediaChange events [from MatchMedia] do not
96 * contain important alias information; as such this info
97 * must be injected into the MediaChange
98 */
99 private buildObservable;
100 /**
101 * Find all current activations and prepare single list of activations
102 * sorted by descending priority.
103 */
104 private findAllActivations;
105 private readonly _media$;
106 private readonly destroyed$;
107 static ɵfac: i0.ɵɵFactoryDeclaration<MediaObserver, never>;
108 static ɵprov: i0.ɵɵInjectableDeclaration<MediaObserver>;
109}