@@ -45,18 +45,19 @@ export class BreakpointObserver implements OnDestroy {
45
45
* @returns Whether any of the media queries match.
46
46
*/
47
47
isMatched ( value : string | string [ ] ) : boolean {
48
- let queries = coerceArray ( value ) ;
48
+ const queries = splitQueries ( coerceArray ( value ) ) ;
49
49
return queries . some ( mediaQuery => this . _registerQuery ( mediaQuery ) . mql . matches ) ;
50
50
}
51
51
52
52
/**
53
53
* Gets an observable of results for the given queries that will emit new results for any changes
54
54
* in matching of the given queries.
55
+ * @param value One or more media queries to check.
55
56
* @returns A stream of matches for the given queries.
56
57
*/
57
58
observe ( value : string | string [ ] ) : Observable < BreakpointState > {
58
- let queries = coerceArray ( value ) ;
59
- let observables = queries . map ( query => this . _registerQuery ( query ) . observable ) ;
59
+ const queries = splitQueries ( coerceArray ( value ) ) ;
60
+ const observables = queries . map ( query => this . _registerQuery ( query ) . observable ) ;
60
61
61
62
return combineLatest ( observables , ( a : BreakpointState , b : BreakpointState ) => {
62
63
return {
@@ -67,14 +68,15 @@ export class BreakpointObserver implements OnDestroy {
67
68
68
69
/** Registers a specific query to be listened for. */
69
70
private _registerQuery ( query : string ) : Query {
71
+ query = query . trim ( ) ;
70
72
// Only set up a new MediaQueryList if it is not already being listened for.
71
73
if ( this . _queries . has ( query ) ) {
72
74
return this . _queries . get ( query ) ! ;
73
75
}
74
76
75
- let mql : MediaQueryList = this . mediaMatcher . matchMedia ( query ) ;
77
+ const mql : MediaQueryList = this . mediaMatcher . matchMedia ( query ) ;
76
78
// Create callback for match changes and add it is as a listener.
77
- let queryObservable = fromEventPattern (
79
+ const queryObservable = fromEventPattern (
78
80
// Listener callback methods are wrapped to be placed back in ngZone. Callbacks must be placed
79
81
// back into the zone because matchMedia is only included in Zone.js by loading the
80
82
// webapis-media-query.js file alongside the zone.js file. Additionally, some browsers do not
@@ -93,8 +95,17 @@ export class BreakpointObserver implements OnDestroy {
93
95
) ;
94
96
95
97
// Add the MediaQueryList to the set of queries.
96
- let output = { observable : queryObservable , mql : mql } ;
98
+ const output = { observable : queryObservable , mql : mql } ;
97
99
this . _queries . set ( query , output ) ;
98
100
return output ;
99
101
}
100
102
}
103
+
104
+ /**
105
+ * Split each query string into separate query strings if two queries are provided as comma
106
+ * separated.
107
+ */
108
+ function splitQueries ( queries : string [ ] ) : string [ ] {
109
+ return queries . map ( ( query : string ) => query . split ( ',' ) )
110
+ . reduce ( ( a1 : string [ ] , a2 : string [ ] ) => a1 . concat ( a2 ) ) ;
111
+ }
0 commit comments