Skip to content

Commit

Permalink
feat(ng2): Add @UIRouterModule decorator
Browse files Browse the repository at this point in the history
Closes #2922
  • Loading branch information
christopherthielen committed Aug 31, 2016
1 parent 8ab3dff commit e7bedc2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/ng2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ui-router-ng2",
"description": "State-based routing for Angular 2",
"peerDependencies": {
"@angular/core": ">=2.0.0-rc.3"
"@angular/core": "^2.0.0-rc.5"
},
"main": "ng2.js",
"typings": "ng2.d.ts"
Expand Down
1 change: 1 addition & 0 deletions src/ng2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./core";
import "./justjs";

export * from "./ng2/interface";
export * from "./ng2/routerModule";
export * from "./ng2/providers";
export * from "./ng2/location";
export * from "./ng2/directives/directives";
Expand Down
62 changes: 62 additions & 0 deletions src/ng2/routerModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {Ng2StateDeclaration} from "./interface";
import {NgModule, NgModuleMetadataType} from "@angular/core";
import {UIROUTER_DIRECTIVES} from "./directives/directives";
import {UIROUTER_PROVIDERS} from "./providers";
import {UIView} from "./directives/uiView";

@NgModule({
declarations: [UIROUTER_DIRECTIVES],
exports: [UIROUTER_DIRECTIVES],
entryComponents: [UIView],
providers: [UIROUTER_PROVIDERS]
})
export class _UIRouterModule {}

/**
* A module declaration lteral, including UI-Router states.
*
* This interface extends the NG2 [NgModuleMetadataType](https://angular.io/docs/ts/latest/api/core/index/NgModuleMetadataType-interface.html)
* by adding a `states` array.
*/
export interface UIRouterModuleMetadata extends NgModuleMetadataType {
states: Ng2StateDeclaration[]
}

/**
* Declares a NgModule with UI-Router states
*
* A Typescript decorator for declaring a [NgModule](https://angular.io/docs/ts/latest/guide/ngmodule.html)
* which contains UI-Router states.
*
* This decorator analyzes the `states` in the module, and adds module `declarations` and `entryComponents`
* for all routed Components.
*
* @example
* ```js
*
* var homeState = { name: 'home', url: '/home', component: Home };
* var aboutState = { name: 'about', url: '/about', component: About };
* @UIRouterModule({
* imports: [BrowserModule],
* states: [homeState, aboutState]
* }) export class AppModule {};
* ```
*
* @param moduleMetaData the [[UIRouterModuleMetadata]]
* (See also [NgModuleMetadataType](https://angular.io/docs/ts/latest/api/core/index/NgModuleMetadataType-interface.html)
*/
export function UIRouterModule(moduleMetaData: UIRouterModuleMetadata) {
let states = moduleMetaData.states || [];
let components = states.map(state => state.views || { $default: state })
.map(viewObj => Object.keys(viewObj).map(key => viewObj[key].component))
.reduce((acc, arr) => acc.concat(arr), [])
.filter(x => typeof x === 'function');

moduleMetaData.imports = (moduleMetaData.imports || []).concat(_UIRouterModule);
moduleMetaData.declarations = (moduleMetaData.declarations || []).concat(components);
moduleMetaData.entryComponents = (moduleMetaData.entryComponents || []).concat(components);

return function(moduleClass) {
return NgModule(moduleMetaData)(moduleClass);
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"declaration": true,
"sourceMap": true
},
"lib": [ "es2015", "dom" ],
"exclude": [
"node_modules",
"build",
Expand Down

0 comments on commit e7bedc2

Please sign in to comment.