license import { Injectable } from '@angular/core' ; import { Platform } from '@angular/cdk/platform' ; /** Global registry for all dynamically-created, injected media queries. */ const mediaQueriesForWebkitCompatibility = new Set < string > ( ) ; /** Style tag that holds all of the dynamically-created media queries. */ let mediaQueryStyleNode ; /** A utility for calling matchMedia queries. */ @ Injectable ( { providedIn : 'root' } ) export class MediaMatcher { /** The internal matchMedia method to return back a MediaQueryList like object. */ private _matchMedia : ( query : string ) => MediaQueryList ; constructor ( private platform ) { this . _matchMedia = this . platform . isBrowser && window . matchMedia ? window . matchMedia . bind ( window ) : noopMatchMedia ; } /**
     * Evaluates the given media query and returns the native MediaQueryList from which results
     * can be retrieved.
     * Confirms the layout engine will trigger for the selector query provided and returns the
     * MediaQueryList for the query provided.
     */ matchMedia ( query ) { if ( this . platform . WEBKIT ) { createEmptyStyleRule ( query ) ; } return this . _matchMedia ( query ) ; } } /**
 * For Webkit engines that only trigger the MediaQueryListListener when
 * there is at least one CSS selector for the respective media query.
 */ function createEmptyStyleRule ( query ) { if ( mediaQueriesForWebkitCompatibility . has ( query ) ) { return ; } try { if ( ! mediaQueryStyleNode ) { mediaQueryStyleNode = document . createElement ( 'style' ) ; mediaQueryStyleNode . setAttribute ( 'type' , 'text/css' ) ; document . head ! . appendChild ( mediaQueryStyleNode ) ; } if ( mediaQueryStyleNode . sheet ) { ( mediaQueryStyleNode . sheet as CSSStyleSheet ) . insertRule ( `@media ${ query } {.fx-query-test{ }}` , 0 ) ; mediaQueriesForWebkitCompatibility . add ( query ) ; } } catch ( e ) { console . error ( e ) ; } } /** No-op matchMedia replacement for non-browser platforms. */ function noopMatchMedia ( query ) { return { matches : query === 'all' || query === '' , media : query , addListener : ( ) => { } , removeListener : ( ) => { } } as any ; }