import { isPlatformBrowser } from '@angular/common' ; import { ClassProvider , FactoryProvider , InjectionToken , PLATFORM_ID } from '@angular/core' ; export const WINDOW = new InjectionToken ( 'WindowToken' ) ; export abstract class WindowRef { get nativeWindow ( ) : Window | Object { throw new Error ( 'Not implemented.' ) ; } } export class BrowserWindowRef extends WindowRef { constructor ( ) { super ( ) ; } get nativeWindow ( ) : Window | Object { return window ; } } export function windowFactory ( browserWindowRef , platformId ) { if ( isPlatformBrowser ( platformId ) ) { return browserWindowRef . nativeWindow ; } return { } ; } const browserWindowProvider = { provide : WindowRef , useClass : BrowserWindowRef , } ; /**
 * Create an injectable provider that uses the windowFactory
 * function for returning the native window object.
 */ const windowProvider = { provide : WINDOW , useFactory : windowFactory , deps : [ WindowRef , PLATFORM_ID ] , } ; export const WINDOW_PROVIDERS = [ browserWindowProvider , windowProvider ] ;