import { Router , UrlSerializer , ChildrenOutletContexts , Routes , Route , ExtraOptions , UrlHandlingStrategy , RouteReuseStrategy , RouterEvent , LoadChildren , ROUTES } from '@angular/router' ; import { Type , Injector , Compiler , ApplicationRef , NgModuleFactory , PLATFORM_ID } from '@angular/core' ; import { Location , isPlatformBrowser } from '@angular/common' ; import { from , of , isObservable , Observable } from 'rxjs' ; import { mergeMap , map } from 'rxjs/operators' ; import { flatten , isPromise } from './util' ; import { LocalizeParser } from './localize-router.parser' ; export class LocalizedRouter extends Router { constructor ( _rootComponentType , _urlSerializer , _rootContexts , _location , injector , compiler , public config , localize ) { super ( _rootComponentType , _urlSerializer , _rootContexts , _location , injector , compiler , config ) ; const platformId = injector . get ( PLATFORM_ID ) ; const isBrowser = isPlatformBrowser ( platformId ) ; const configLoader = isBrowser ? this . configLoader . __proto__ : this . configLoader ; configLoader . loadModuleFactory = ( loadChildren ) => { return wrapIntoObservable ( loadChildren ( ) ) . pipe ( mergeMap ( ( t ) => { let compiled ; if ( t instanceof NgModuleFactory ) { compiled = of ( t ) ; } else { compiled = from ( compiler . compileModuleAsync ( t ) ) as Observable < NgModuleFactory < any > > ; } return compiled . pipe ( map ( factory => { return { moduleType : factory . moduleType , create : ( parentInjector ) => { const module = factory . create ( parentInjector ) ; const getMethod = module . injector . get . bind ( module . injector ) ; module . injector [ 'get' ] = ( token , notFoundValue ) => { const getResult = getMethod ( token , notFoundValue ) ; if ( token === ROUTES ) { return localize . initChildRoutes ( [ ] . concat ( ... getResult ) ) ; } else { return getResult ; } } ; return module ; } } ; } ) ) ; } ) ) ; } ; } } export function setupRouter ( ref , urlSerializer , contexts , location , injector , compiler , config , localize , opts = { } , urlHandlingStrategy ? , routeReuseStrategy ? ) { const router = new LocalizedRouter ( null , urlSerializer , contexts , location , injector , compiler , flatten ( config ) , localize ) ; if ( urlHandlingStrategy ) { router . urlHandlingStrategy = urlHandlingStrategy ; } if ( routeReuseStrategy ) { router . routeReuseStrategy = routeReuseStrategy ; } if ( opts . errorHandler ) { router . errorHandler = opts . errorHandler ; } if ( opts . malformedUriErrorHandler ) { router . malformedUriErrorHandler = opts . malformedUriErrorHandler ; } if ( opts . enableTracing ) { router . events . subscribe ( ( e ) => { console . group ( `Router Event: ${ ( < any > e . constructor ) . name }` ) ; console . log ( e . toString ( ) ) ; console . log ( e ) ; console . groupEnd ( ) ; } ) ; } if ( opts . onSameUrlNavigation ) { router . onSameUrlNavigation = opts . onSameUrlNavigation ; } if ( opts . paramsInheritanceStrategy ) { router . paramsInheritanceStrategy = opts . paramsInheritanceStrategy ; } if ( opts . urlUpdateStrategy ) { router . urlUpdateStrategy = opts . urlUpdateStrategy ; } if ( opts . relativeLinkResolution ) { router . relativeLinkResolution = opts . relativeLinkResolution ; } return router ; } export function wrapIntoObservable < T > ( value ) { if ( isObservable ( value ) ) { return value ; } if ( isPromise ( value ) ) { return from ( Promise . resolve ( value ) ) ; } return of ( value ) ; }