2016-07-06 14:02:16 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-01-03 19:54:46 -05:00
|
|
|
import {Compiler, ComponentFactoryResolver, InjectionToken, Injector, NgModuleFactory, NgModuleFactoryLoader} from '@angular/core';
|
2016-07-06 14:02:16 -04:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {fromPromise} from 'rxjs/observable/fromPromise';
|
2016-08-16 00:11:09 -04:00
|
|
|
import {of } from 'rxjs/observable/of';
|
2016-08-30 17:25:46 -04:00
|
|
|
import {map} from 'rxjs/operator/map';
|
|
|
|
import {mergeMap} from 'rxjs/operator/mergeMap';
|
2016-08-16 00:11:09 -04:00
|
|
|
import {LoadChildren, Route} from './config';
|
|
|
|
import {flatten, wrapIntoObservable} from './utils/collection';
|
2016-07-06 14:02:16 -04:00
|
|
|
|
2016-08-30 21:07:40 -04:00
|
|
|
/**
|
2017-02-15 16:30:40 -05:00
|
|
|
* @docsNotRequired
|
2016-08-30 21:07:40 -04:00
|
|
|
* @experimental
|
|
|
|
*/
|
2017-01-03 19:54:46 -05:00
|
|
|
export const ROUTES = new InjectionToken<Route[][]>('ROUTES');
|
2016-07-06 14:32:42 -04:00
|
|
|
|
2016-07-06 14:02:16 -04:00
|
|
|
export class LoadedRouterConfig {
|
2016-07-13 21:12:59 -04:00
|
|
|
constructor(
|
|
|
|
public routes: Route[], public injector: Injector,
|
2016-11-11 20:12:00 -05:00
|
|
|
public factoryResolver: ComponentFactoryResolver, public injectorFactory: Function) {}
|
2016-07-06 14:02:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export class RouterConfigLoader {
|
2017-02-01 16:13:57 -05:00
|
|
|
constructor(
|
|
|
|
private loader: NgModuleFactoryLoader, private compiler: Compiler,
|
2017-02-15 13:57:03 -05:00
|
|
|
private onLoadStartListener?: (r: Route) => void,
|
|
|
|
private onLoadEndListener?: (r: Route) => void) {}
|
2017-02-01 16:13:57 -05:00
|
|
|
|
|
|
|
load(parentInjector: Injector, route: Route): Observable<LoadedRouterConfig> {
|
2017-02-15 13:57:03 -05:00
|
|
|
if (this.onLoadStartListener) {
|
|
|
|
this.onLoadStartListener(route);
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:13:57 -05:00
|
|
|
const moduleFactory$ = this.loadModuleFactory(route.loadChildren);
|
2017-02-15 13:57:03 -05:00
|
|
|
|
2017-02-01 16:13:57 -05:00
|
|
|
return map.call(moduleFactory$, (factory: NgModuleFactory<any>) => {
|
2017-02-15 13:57:03 -05:00
|
|
|
if (this.onLoadEndListener) {
|
|
|
|
this.onLoadEndListener(route);
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:13:57 -05:00
|
|
|
const module = factory.create(parentInjector);
|
|
|
|
const injectorFactory = (parent: Injector) => factory.create(parent).injector;
|
2017-02-15 13:57:03 -05:00
|
|
|
|
2016-07-13 21:12:59 -04:00
|
|
|
return new LoadedRouterConfig(
|
2017-02-01 16:13:57 -05:00
|
|
|
flatten(module.injector.get(ROUTES)), module.injector, module.componentFactoryResolver,
|
2016-11-11 20:12:00 -05:00
|
|
|
injectorFactory);
|
2016-08-16 00:11:09 -04:00
|
|
|
});
|
2016-07-06 14:02:16 -04:00
|
|
|
}
|
2016-08-16 00:11:09 -04:00
|
|
|
|
|
|
|
private loadModuleFactory(loadChildren: LoadChildren): Observable<NgModuleFactory<any>> {
|
|
|
|
if (typeof loadChildren === 'string') {
|
|
|
|
return fromPromise(this.loader.load(loadChildren));
|
|
|
|
} else {
|
2017-02-20 22:48:23 -05:00
|
|
|
const offlineMode = this.compiler instanceof Compiler;
|
|
|
|
return mergeMap.call(
|
|
|
|
wrapIntoObservable(loadChildren()),
|
|
|
|
(t: any) => offlineMode ? of (<any>t) : fromPromise(this.compiler.compileModuleAsync(t)));
|
2016-08-16 00:11:09 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 21:07:40 -04:00
|
|
|
}
|