11import {
22 Attribute , ComponentFactory , ComponentRef , Directive ,
33 ReflectiveInjector , ResolvedReflectiveProvider , ViewContainerRef ,
4- Inject , ComponentResolver , provide } from '@angular/core' ;
4+ Inject , ComponentResolver , provide , ComponentFactoryResolver ,
5+ NoComponentFactoryError } from '@angular/core' ;
56
67import { isBlank , isPresent } from '@angular/core/src/facade/lang' ;
78
8- import { RouterOutletMap , ActivatedRoute , PRIMARY_OUTLET } from '@angular/router' ;
9- import { RouterOutlet } from '@angular/router/directives/router_outlet' ;
9+ import { RouterOutletMap , ActivatedRoute , RouterOutlet , PRIMARY_OUTLET } from '@angular/router' ;
1010import { NSLocationStrategy } from "./ns-location-strategy" ;
1111import { DEVICE } from "../platform-providers" ;
1212import { Device } from "platform" ;
@@ -40,10 +40,8 @@ class RefCache {
4040 }
4141}
4242
43-
44-
4543@Directive ( { selector : 'page-router-outlet' } )
46- export class PageRouterOutlet extends RouterOutlet {
44+ export class PageRouterOutlet {
4745 private viewUtil : ViewUtil ;
4846 private refCache : RefCache = new RefCache ( ) ;
4947 private isInitalPage : boolean = true ;
@@ -52,6 +50,8 @@ export class PageRouterOutlet extends RouterOutlet {
5250 private currnetActivatedComp : ComponentRef < any > ;
5351 private currentActivatedRoute : ActivatedRoute ;
5452
53+ public outletMap : RouterOutletMap ;
54+
5555 get isActivated ( ) : boolean {
5656 return ! ! this . currnetActivatedComp ;
5757 }
@@ -76,15 +76,17 @@ export class PageRouterOutlet extends RouterOutlet {
7676 private containerRef : ViewContainerRef ,
7777 @Attribute ( 'name' ) name : string ,
7878 private locationStrategy : NSLocationStrategy ,
79+ private componentFactoryResolver : ComponentFactoryResolver ,
7980 compiler : ComponentResolver ,
8081 @Inject ( DEVICE ) device : Device ) {
81- super ( parentOutletMap , containerRef , name )
82+
83+ parentOutletMap . registerOutlet ( name ? name : PRIMARY_OUTLET , < any > this ) ;
8284
8385 this . viewUtil = new ViewUtil ( device ) ;
8486 compiler . resolveComponent ( DetachedLoader ) . then ( ( detachedLoaderFactory ) => {
8587 log ( "DetachedLoaderFactory leaded" ) ;
8688 this . detachedLoaderFactory = detachedLoaderFactory ;
87- } )
89+ } ) ;
8890 }
8991
9092 deactivate ( ) : void {
@@ -115,7 +117,6 @@ export class PageRouterOutlet extends RouterOutlet {
115117 * This method in turn is responsible for calling the `routerOnActivate` hook of its child.
116118 */
117119 activate (
118- factory : ComponentFactory < any > ,
119120 activatedRoute : ActivatedRoute ,
120121 providers : ResolvedReflectiveProvider [ ] ,
121122 outletMap : RouterOutletMap ) : void {
@@ -124,16 +125,16 @@ export class PageRouterOutlet extends RouterOutlet {
124125 this . currentActivatedRoute = activatedRoute ;
125126
126127 if ( this . locationStrategy . isPageNavigatingBack ( ) ) {
127- this . activateOnGoBack ( factory , activatedRoute , providers ) ;
128+ this . activateOnGoBack ( activatedRoute , providers ) ;
128129 } else {
129- this . activateOnGoForward ( factory , activatedRoute , providers ) ;
130+ this . activateOnGoForward ( activatedRoute , providers ) ;
130131 }
131132 }
132133
133134 private activateOnGoForward (
134- factory : ComponentFactory < any > ,
135135 activatedRoute : ActivatedRoute ,
136136 providers : ResolvedReflectiveProvider [ ] ) : void {
137+ const factory = this . getComponentFactory ( activatedRoute ) ;
137138
138139 if ( this . isInitalPage ) {
139140 log ( "PageRouterOutlet.activate() inital page - just load component: " + activatedRoute . component ) ;
@@ -146,7 +147,7 @@ export class PageRouterOutlet extends RouterOutlet {
146147 log ( "PageRouterOutlet.activate() forward navigation - create detached loader in the loader container: " + activatedRoute . component ) ;
147148
148149 const page = new Page ( ) ;
149- const pageResolvedProvider = ReflectiveInjector . resolve ( [ provide ( Page , { useValue : page } ) ] )
150+ const pageResolvedProvider = ReflectiveInjector . resolve ( [ provide ( Page , { useValue : page } ) ] ) ;
150151 const childInjector = ReflectiveInjector . fromResolvedProviders ( [ ...providers , ...pageResolvedProvider ] , this . containerRef . parentInjector ) ;
151152 const loaderRef = this . containerRef . createComponent ( this . detachedLoaderFactory , this . containerRef . length , childInjector , [ ] ) ;
152153
@@ -156,7 +157,7 @@ export class PageRouterOutlet extends RouterOutlet {
156157 }
157158 }
158159
159- private activateOnGoBack ( factory : ComponentFactory < any > ,
160+ private activateOnGoBack (
160161 activatedRoute : ActivatedRoute ,
161162 providers : ResolvedReflectiveProvider [ ] ) : void {
162163 log ( "PageRouterOutlet.activate() - Back naviation, so load from cache: " + activatedRoute . component ) ;
@@ -189,11 +190,35 @@ export class PageRouterOutlet extends RouterOutlet {
189190 create : ( ) => { return page ; }
190191 } ) ;
191192 }
193+
194+ // NOTE: Using private APIs - potential break point!
195+ private getComponentFactory ( activatedRoute : any ) : ComponentFactory < any > {
196+ const snapshot = activatedRoute . _futureSnapshot ;
197+ const component : any = < any > snapshot . _routeConfig . component ;
198+ let factory : ComponentFactory < any > ;
199+ try {
200+ factory = typeof component === 'string' ?
201+ snapshot . _resolvedComponentFactory :
202+ this . componentFactoryResolver . resolveComponentFactory ( component ) ;
203+ } catch ( e ) {
204+ if ( ! ( e instanceof NoComponentFactoryError ) ) {
205+ throw e ;
206+ }
207+ // TODO: vsavkin uncomment this once ComponentResolver is deprecated
208+ // const componentName = component ? component.name : null;
209+ // console.warn(
210+ // `'${componentName}' not found in precompile array. To ensure all components referred
211+ // to by the RouterConfig are compiled, you must add '${componentName}' to the
212+ // 'precompile' array of your application component. This will be required in a future
213+ // release of the router.`);
214+
215+ factory = snapshot . _resolvedComponentFactory ;
216+ }
217+ return factory ;
218+ }
219+
192220}
193221
194222function log ( msg : string ) {
195223 routerLog ( msg ) ;
196- }
197-
198-
199-
224+ }
0 commit comments