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-03-14 19:26:17 -04:00
|
|
|
import {Compiler, InjectionToken, Injector, NgModuleFactory, NgModuleFactoryLoader, NgModuleRef} 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 {
|
2017-03-14 19:26:17 -04:00
|
|
|
constructor(public routes: Route[], public module: NgModuleRef<any>) {}
|
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);
|
2017-02-15 13:57:03 -05:00
|
|
|
|
2017-03-14 19:26:17 -04:00
|
|
|
return new LoadedRouterConfig(flatten(module.injector.get(ROUTES)), module);
|
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-03-07 17:27:20 -05:00
|
|
|
return mergeMap.call(wrapIntoObservable(loadChildren()), (t: any) => {
|
|
|
|
if (t instanceof NgModuleFactory) {
|
|
|
|
return of (t);
|
|
|
|
} else {
|
|
|
|
return fromPromise(this.compiler.compileModuleAsync(t));
|
|
|
|
}
|
|
|
|
});
|
2016-08-16 00:11:09 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 21:07:40 -04:00
|
|
|
}
|