Created
September 16, 2016 16:40
-
-
Save adharris/b99e2224ff96c04c21fc9bec9ab4e0b1 to your computer and use it in GitHub Desktop.
uiRouter.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule } from '@angular/core'; | |
import { BrowserModule } from '@angular/platform-browser'; | |
import { AppComponent } from './app.component'; | |
import { UIRouterModule } from 'app/uiRouter'; | |
import { ProgramModule } from 'app/programs'; | |
import { ParticipantModule } from 'app/participants'; | |
@NgModule({ | |
declarations: [AppComponent], | |
bootstrap: [AppComponent], | |
imports: [ | |
BrowserModule, | |
UIRouterModule.forRoot('/programs'), | |
ProgramModule, | |
ParticipantModule, | |
], | |
}) | |
export class AppModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule } from '@angular/core'; | |
import { UIRouterModule } from 'app/uiRouter'; | |
import { ParticipantApi, ParticipantService } from './services' | |
import { PARTICIPANT_COMPONENTS } from './components'; | |
import { PARTICIPANT_STATES, PARTICIPANT_VIEW_COMPONENTS } from './views' | |
import { PARTICIPANT_PIPES } from './util'; | |
import { PARTICIPANT_FORMS } from './forms'; | |
@NgModule({ | |
providers: [ | |
ParticipantApi, | |
ParticipantService, | |
], | |
imports: [ | |
UIRouterModule.forChild({ | |
states: PARTICIPANT_STATES, | |
}), | |
], | |
declarations: [ | |
PARTICIPANT_COMPONENTS, | |
PARTICIPANT_VIEW_COMPONENTS, | |
PARTICIPANT_PIPES, | |
PARTICIPANT_FORMS, | |
] | |
}) | |
export class ParticipantModule { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Injector, Inject, OpaqueToken, ANALYZE_FOR_ENTRY_COMPONENTS } from '@angular/core'; | |
import { UIRouter, Ng2StateDeclaration, ParamTypeDefinition} from 'ui-router-ng2'; | |
export const UIROUTER_OTHERWISE = new OpaqueToken('UIROUTER_OTHERWISE'); | |
export const UIROUTER_OPTIONS = new OpaqueToken('UIROUTER_OPTIONS'); | |
export interface UIRouterConfigOptions { | |
states?: Ng2StateDeclaration[]; | |
paramTypes?: {[name: string]: ParamTypeDefinition}, | |
services?: any[], | |
} | |
export function provideRouterOptions(options: UIRouterConfigOptions) { | |
if (!options) return []; | |
return [ | |
{provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: options.states, multi: true}, | |
{provide: UIROUTER_OPTIONS, useValue: options, multi: true}, | |
] | |
} | |
@Injectable() | |
export class RouterConfig { | |
constructor(@Inject(Injector) private injector: Injector, | |
@Inject(UIROUTER_OTHERWISE) private otherwise: any, | |
@Inject(UIROUTER_OPTIONS) private options: UIRouterConfigOptions[]) { | |
} | |
configure(uiRouter: UIRouter) { | |
this.options.forEach(o => registerParamTypes(uiRouter, o)); | |
this.options.forEach(o => registerStates(uiRouter, o)); | |
this.options.forEach(o => injectServices(this.injector, o)) | |
if (this.otherwise) { | |
uiRouter.urlRouterProvider.otherwise('/programs'); | |
} | |
} | |
} | |
function registerParamTypes(uiRouter: UIRouter, | |
options: UIRouterConfigOptions) { | |
if (!options.paramTypes) { return } | |
for (let name in options.paramTypes) { | |
uiRouter.urlMatcherFactory.type(name, options.paramTypes[name]); | |
} | |
} | |
function registerStates(uiRouter: UIRouter, options: UIRouterConfigOptions) { | |
if (!options.states) { return } | |
options.states.forEach(state => uiRouter.stateRegistry.register(state)); | |
} | |
function injectServices(injector: Injector, options: UIRouterConfigOptions) { | |
if (!options.services) { return } | |
options.services.forEach(service => injector.get(service)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule, ModuleWithProviders, ANALYZE_FOR_ENTRY_COMPONENTS } from '@angular/core'; | |
import { LocationStrategy, PathLocationStrategy } from '@angular/common' | |
import { UIROUTER_PROVIDERS, UIROUTER_DIRECTIVES, UIRouterConfig} from 'ui-router-ng2' | |
import { RouterConfig, provideRouterOptions, UIRouterConfigOptions, UIROUTER_OTHERWISE } from './uiRouter.config'; | |
const LOCATION_STRATEGEY = { | |
provide: LocationStrategy, | |
useClass: PathLocationStrategy, | |
} | |
@NgModule({ | |
declarations: UIROUTER_DIRECTIVES, | |
exports: UIROUTER_DIRECTIVES, | |
}) | |
export class UIRouterModule { | |
static forRoot(otherwise: any, | |
options?: UIRouterConfigOptions): ModuleWithProviders { | |
return { | |
ngModule: UIRouterModule, | |
providers: [ | |
UIROUTER_PROVIDERS, | |
LOCATION_STRATEGEY, | |
{provide: UIRouterConfig, useClass: RouterConfig}, | |
{provide: UIROUTER_OTHERWISE, useValue: otherwise}, | |
provideRouterOptions(options), | |
] | |
} | |
} | |
static forChild(options?: UIRouterConfigOptions): ModuleWithProviders { | |
return { | |
ngModule: UIRouterModule, | |
providers: [ | |
provideRouterOptions(options), | |
], | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment