Skip to content

Commit

Permalink
Aot support
Browse files Browse the repository at this point in the history
  • Loading branch information
aitboudad committed Dec 12, 2016
1 parent 9647049 commit 2fb0a79
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/ng2/directives/uiSrefStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class UISrefStatus {
this._srefs$ = new BehaviorSubject(this.srefs.toArray());
this._srefChangesSub = this.srefs.changes.subscribe(srefs => this._srefs$.next(srefs));

let targetStates$: Observable<TargetState[]> =
let targetStates$: Observable<any> =
this._srefs$.switchMap((srefs: UISref[]) =>
Observable.combineLatest<TargetState[]>(srefs.map(sref => sref.targetState$)));

Expand Down
33 changes: 20 additions & 13 deletions src/ng2/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - [quick start repository](https://github.com/ui-router/quickstart-ng2)
*
* Getting started:
*
*
* - Use npm. Add a dependency on latest `ui-router-ng2`
* - Import UI-Router classes directly from `"ui-router-ng2"`
*
Expand Down Expand Up @@ -111,9 +111,7 @@ import {NATIVE_INJECTOR_TOKEN} from "ui-router-core";
* Creates a UIRouter instance and configures it for Angular 2, then invokes router bootstrap.
* This function is used as an Angular 2 `useFactory` Provider.
*/
let uiRouterFactory = (
location: UIRouterLocation,
injector: Injector) => {
export function uiRouterFactory(location: UIRouterLocation, injector: Injector) {

let rootModules: RootModule[] = injector.get(UIROUTER_ROOT_MODULE);
let modules: StatesModule[] = injector.get(UIROUTER_MODULE_TOKEN);
Expand Down Expand Up @@ -167,20 +165,30 @@ let uiRouterFactory = (
return router;
};

export function parentUIViewInjectFactory(r: StateRegistry) { return { fqn: null, context: r.root() } as ParentUIViewInject; }

export const _UIROUTER_INSTANCE_PROVIDERS: Provider[] = [
{ provide: UIRouter, useFactory: uiRouterFactory, deps: [UIRouterLocation, Injector] },
{ provide: UIRouterLocation, useClass: UIRouterLocation },
{ provide: UIView.PARENT_INJECT, useFactory: (r: StateRegistry) => { return { fqn: null, context: r.root() } as ParentUIViewInject }, deps: [StateRegistry]},
{ provide: UIView.PARENT_INJECT, useFactory: parentUIViewInjectFactory, deps: [StateRegistry]},
];

export function fnStateService(r: UIRouter) { return r.stateService; }
export function fnTransitionService(r: UIRouter) { return r.transitionService; }
export function fnUrlMatcherFactory(r: UIRouter) { return r.urlMatcherFactory; }
export function fnUrlRouter(r: UIRouter) { return r.urlRouter; }
export function fnViewService(r: UIRouter) { return r.viewService; }
export function fnStateRegistry(r: UIRouter) { return r.stateRegistry; }
export function fnGlobals(r: any) { return r.globals; }

export const _UIROUTER_SERVICE_PROVIDERS: Provider[] = [
{ provide: StateService, useFactory: (r: UIRouter) => r.stateService , deps: [UIRouter]},
{ provide: TransitionService, useFactory: (r: UIRouter) => r.transitionService, deps: [UIRouter]},
{ provide: UrlMatcherFactory, useFactory: (r: UIRouter) => r.urlMatcherFactory, deps: [UIRouter]},
{ provide: UrlRouter, useFactory: (r: UIRouter) => r.urlRouter , deps: [UIRouter]},
{ provide: ViewService, useFactory: (r: UIRouter) => r.viewService , deps: [UIRouter]},
{ provide: StateRegistry, useFactory: (r: UIRouter) => r.stateRegistry , deps: [UIRouter]},
{ provide: Globals, useFactory: (r: UIRouter) => r.globals , deps: [UIRouter]},
{ provide: StateService, useFactory: fnStateService, deps: [UIRouter]},
{ provide: TransitionService, useFactory: fnTransitionService, deps: [UIRouter]},
{ provide: UrlMatcherFactory, useFactory: fnUrlMatcherFactory, deps: [UIRouter]},
{ provide: UrlRouter, useFactory: fnUrlRouter, deps: [UIRouter]},
{ provide: ViewService, useFactory: fnViewService, deps: [UIRouter]},
{ provide: StateRegistry, useFactory: fnStateRegistry, deps: [UIRouter]},
{ provide: Globals, useFactory: fnGlobals, deps: [UIRouter]},
];

/**
Expand All @@ -189,4 +197,3 @@ export const _UIROUTER_SERVICE_PROVIDERS: Provider[] = [
* @deprecated use [[UIRouterModule.forRoot]]
*/
export const UIROUTER_PROVIDERS: Provider[] = _UIROUTER_INSTANCE_PROVIDERS.concat(_UIROUTER_SERVICE_PROVIDERS);

38 changes: 21 additions & 17 deletions src/ng2/uiRouterNgModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ import {identity} from "ui-router-core";
import {LocationStrategy, HashLocationStrategy, PathLocationStrategy} from "@angular/common";
import {_UIROUTER_INSTANCE_PROVIDERS, _UIROUTER_SERVICE_PROVIDERS} from "./providers";

export function makeRootProviders(module: StatesModule): Provider[] {
return [
{ provide: UIROUTER_ROOT_MODULE, useValue: module, multi: true},
{ provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true },
{ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true },
];
}

export function makeChildProviders(module: StatesModule): Provider[] {
return [
{ provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true },
{ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true },
];
}

export function locationStrategy(useHash) {
return { provide: LocationStrategy, useClass: useHash ? HashLocationStrategy : PathLocationStrategy };
}

/**
* Creates UI-Router Modules
Expand Down Expand Up @@ -75,14 +93,13 @@ export class UIRouterModule {
* @returns an `NgModule` which provides the [[UIRouter]] singleton instance
*/
static forRoot(config: RootModule = {}): ModuleWithProviders {
let locationStrategy = config.useHash ? HashLocationStrategy : PathLocationStrategy;
return {
ngModule: UIRouterModule,
providers: [
_UIROUTER_INSTANCE_PROVIDERS,
_UIROUTER_SERVICE_PROVIDERS,
{ provide: LocationStrategy, useClass: locationStrategy },
...makeProviders(config, true),
locationStrategy(config.useHash),
...makeRootProviders(config),
]
}
}
Expand Down Expand Up @@ -114,25 +131,12 @@ export class UIRouterModule {
static forChild(module: StatesModule = {}): ModuleWithProviders {
return {
ngModule: UIRouterModule,
providers: makeProviders(module, false),
providers: makeChildProviders(module),
}
}

}

/** @hidden */
function makeProviders(module: StatesModule, forRoot: boolean): Provider[] {
let providers: Provider[] = [module.configClass]
.filter(identity)
.map(configClass => ({ provide: configClass, useClass: configClass }));

if (forRoot) providers.push({ provide: UIROUTER_ROOT_MODULE, useValue: module, multi: true});
providers.push({ provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true });
providers.push({ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true });

return providers;
}

/**
* UI-Router declarative configuration which can be provided to [[UIRouterModule.forRoot]]
*/
Expand Down

0 comments on commit 2fb0a79

Please sign in to comment.