83
83
* ```
84
84
*
85
85
* @preferred @module ng2
86
- */ /** */
87
- import { Injector , Provider } from "@angular/core" ;
88
- import { UIRouter } from "ui-router-core" ;
89
- import { PathNode } from "ui-router-core" ;
90
- import { StateRegistry } from "ui-router-core" ;
91
- import { StateService } from "ui-router-core" ;
92
- import { TransitionService } from "ui-router-core" ;
93
- import { UrlMatcherFactory } from "ui-router-core" ;
94
- import { UrlRouter } from "ui-router-core" ;
95
- import { ViewService } from "ui-router-core" ;
96
- import { UIView , ParentUIViewInject } from "./directives/uiView" ;
97
- import { ng2ViewsBuilder , Ng2ViewConfig } from "./statebuilders/views" ;
98
- import { Ng2ViewDeclaration } from "./interface" ;
99
- import { applyRootModuleConfig , applyModuleConfig } from "./uiRouterConfig" ;
100
- import { Globals } from "ui-router-core" ;
101
- import { UIRouterLocation } from "./location" ;
102
- import { services } from "ui-router-core" ;
103
- import { Resolvable } from "ui-router-core" ;
104
- import { RootModule , StatesModule , UIROUTER_ROOT_MODULE , UIROUTER_MODULE_TOKEN } from "./uiRouterNgModule" ;
105
- import { UIRouterRx } from "./rx" ;
106
- import { NATIVE_INJECTOR_TOKEN } from "ui-router-core" ;
86
+ */
87
+ /** */
88
+ import { Injector , Provider } from "@angular/core" ;
89
+ import {
90
+ UIRouter , PathNode , StateRegistry , StateService , TransitionService , UrlMatcherFactory , UrlRouter , ViewService ,
91
+ UrlService , Globals , services , Resolvable , NATIVE_INJECTOR_TOKEN
92
+ } from "ui-router-core" ;
93
+ import { UIView , ParentUIViewInject } from "./directives/uiView" ;
94
+ import { ng2ViewsBuilder , Ng2ViewConfig } from "./statebuilders/views" ;
95
+ import { Ng2ViewDeclaration } from "./interface" ;
96
+ import { applyRootModuleConfig , applyModuleConfig } from "./uiRouterConfig" ;
97
+ import { UIRouterLocation } from "./location/uiRouterLocation" ;
98
+ import { RootModule , StatesModule , UIROUTER_ROOT_MODULE , UIROUTER_MODULE_TOKEN } from "./uiRouterNgModule" ;
99
+ import { UIRouterRx } from "./rx" ;
100
+ import { servicesPlugin } from "ui-router-core/lib/vanilla" ;
107
101
108
102
/**
109
103
* This is a factory function for a UIRouter instance
@@ -120,50 +114,53 @@ export function uiRouterFactory(location: UIRouterLocation, injector: Injector)
120
114
throw new Error ( "Exactly one UIRouterModule.forRoot() should be in the bootstrapped app module's imports: []" ) ;
121
115
}
122
116
123
- // ----------------- Monkey Patches ----------------
124
- // Monkey patch the services.$injector to the ng2 Injector
125
- services . $injector . get = injector . get . bind ( injector ) ;
126
-
127
- // Monkey patch the services.$location with ng2 Location implementation
128
- location . init ( ) ;
129
-
130
117
131
118
// ----------------- Create router -----------------
132
119
// Create a new ng2 UIRouter and configure it for ng2
133
120
let router = new UIRouter ( ) ;
134
- new UIRouterRx ( router ) ;
135
- let registry = router . stateRegistry ;
121
+
122
+ // Add RxJS plugin
123
+ router . plugin ( UIRouterRx ) ;
124
+
125
+ // Add $q-like and $injector-like service APIs
126
+ router . plugin ( servicesPlugin ) ;
127
+
128
+
129
+ // ----------------- Monkey Patches ----------------
130
+ // Monkey patch the services.$injector to use the root ng2 Injector
131
+ services . $injector . get = injector . get . bind ( injector ) ;
132
+
136
133
137
134
// ----------------- Configure for ng2 -------------
135
+ location . init ( router ) ;
136
+
138
137
// Apply ng2 ui-view handling code
139
- router . viewService . viewConfigFactory ( "ng2" , ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => new Ng2ViewConfig ( path , config ) ) ;
140
- registry . decorator ( 'views' , ng2ViewsBuilder ) ;
138
+ let viewConfigFactory = ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => new Ng2ViewConfig ( path , config ) ;
139
+ router . viewService . _pluginapi . _viewConfigFactory ( "ng2" , viewConfigFactory ) ;
141
140
142
141
// Apply statebuilder decorator for ng2 NgModule registration
143
- registry . stateQueue . flush ( router . stateService ) ;
142
+ let registry = router . stateRegistry ;
143
+ registry . decorator ( 'views' , ng2ViewsBuilder ) ;
144
144
145
145
// Prep the tree of NgModule by placing the root NgModule's Injector on the root state.
146
146
let ng2InjectorResolvable = Resolvable . fromData ( NATIVE_INJECTOR_TOKEN , injector ) ;
147
147
registry . root ( ) . resolvables . push ( ng2InjectorResolvable ) ;
148
148
149
149
150
150
// ----------------- Initialize router -------------
151
- // Allow states to be registered
152
- registry . stateQueue . autoFlush ( router . stateService ) ;
153
-
154
151
setTimeout ( ( ) => {
155
152
rootModules . forEach ( moduleConfig => applyRootModuleConfig ( router , injector , moduleConfig ) ) ;
156
153
modules . forEach ( moduleConfig => applyModuleConfig ( router , injector , moduleConfig ) ) ;
157
154
158
155
// Start monitoring the URL
159
- if ( ! router . urlRouterProvider . interceptDeferred ) {
160
- router . urlRouter . listen ( ) ;
161
- router . urlRouter . sync ( ) ;
156
+ if ( ! router . urlRouter . interceptDeferred ) {
157
+ router . urlService . listen ( ) ;
158
+ router . urlService . sync ( ) ;
162
159
}
163
160
} ) ;
164
161
165
162
return router ;
166
- } ;
163
+ }
167
164
168
165
export function parentUIViewInjectFactory ( r : StateRegistry ) { return { fqn : null , context : r . root ( ) } as ParentUIViewInject ; }
169
166
@@ -177,18 +174,20 @@ export function fnStateService(r: UIRouter) { return r.stateService; }
177
174
export function fnTransitionService ( r : UIRouter ) { return r . transitionService ; }
178
175
export function fnUrlMatcherFactory ( r : UIRouter ) { return r . urlMatcherFactory ; }
179
176
export function fnUrlRouter ( r : UIRouter ) { return r . urlRouter ; }
177
+ export function fnUrlService ( r : UIRouter ) { return r . urlService ; }
180
178
export function fnViewService ( r : UIRouter ) { return r . viewService ; }
181
179
export function fnStateRegistry ( r : UIRouter ) { return r . stateRegistry ; }
182
180
export function fnGlobals ( r : any ) { return r . globals ; }
183
181
184
182
export const _UIROUTER_SERVICE_PROVIDERS : Provider [ ] = [
185
- { provide : StateService , useFactory : fnStateService , deps : [ UIRouter ] } ,
186
- { provide : TransitionService , useFactory : fnTransitionService , deps : [ UIRouter ] } ,
187
- { provide : UrlMatcherFactory , useFactory : fnUrlMatcherFactory , deps : [ UIRouter ] } ,
188
- { provide : UrlRouter , useFactory : fnUrlRouter , deps : [ UIRouter ] } ,
189
- { provide : ViewService , useFactory : fnViewService , deps : [ UIRouter ] } ,
190
- { provide : StateRegistry , useFactory : fnStateRegistry , deps : [ UIRouter ] } ,
191
- { provide : Globals , useFactory : fnGlobals , deps : [ UIRouter ] } ,
183
+ { provide : StateService , useFactory : fnStateService , deps : [ UIRouter ] } ,
184
+ { provide : TransitionService , useFactory : fnTransitionService , deps : [ UIRouter ] } ,
185
+ { provide : UrlMatcherFactory , useFactory : fnUrlMatcherFactory , deps : [ UIRouter ] } ,
186
+ { provide : UrlRouter , useFactory : fnUrlRouter , deps : [ UIRouter ] } ,
187
+ { provide : UrlService , useFactory : fnUrlService , deps : [ UIRouter ] } ,
188
+ { provide : ViewService , useFactory : fnViewService , deps : [ UIRouter ] } ,
189
+ { provide : StateRegistry , useFactory : fnStateRegistry , deps : [ UIRouter ] } ,
190
+ { provide : Globals , useFactory : fnGlobals , deps : [ UIRouter ] } ,
192
191
] ;
193
192
194
193
/**
0 commit comments