Skip to content

Commit e7bedc2

Browse files
feat(ng2): Add @UIRouterModule decorator
Closes #2922
1 parent 8ab3dff commit e7bedc2

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

packages/ng2/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ui-router-ng2",
33
"description": "State-based routing for Angular 2",
44
"peerDependencies": {
5-
"@angular/core": ">=2.0.0-rc.3"
5+
"@angular/core": "^2.0.0-rc.5"
66
},
77
"main": "ng2.js",
88
"typings": "ng2.d.ts"

src/ng2.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export * from "./core";
88
import "./justjs";
99

1010
export * from "./ng2/interface";
11+
export * from "./ng2/routerModule";
1112
export * from "./ng2/providers";
1213
export * from "./ng2/location";
1314
export * from "./ng2/directives/directives";

src/ng2/routerModule.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {Ng2StateDeclaration} from "./interface";
2+
import {NgModule, NgModuleMetadataType} from "@angular/core";
3+
import {UIROUTER_DIRECTIVES} from "./directives/directives";
4+
import {UIROUTER_PROVIDERS} from "./providers";
5+
import {UIView} from "./directives/uiView";
6+
7+
@NgModule({
8+
declarations: [UIROUTER_DIRECTIVES],
9+
exports: [UIROUTER_DIRECTIVES],
10+
entryComponents: [UIView],
11+
providers: [UIROUTER_PROVIDERS]
12+
})
13+
export class _UIRouterModule {}
14+
15+
/**
16+
* A module declaration lteral, including UI-Router states.
17+
*
18+
* This interface extends the NG2 [NgModuleMetadataType](https://angular.io/docs/ts/latest/api/core/index/NgModuleMetadataType-interface.html)
19+
* by adding a `states` array.
20+
*/
21+
export interface UIRouterModuleMetadata extends NgModuleMetadataType {
22+
states: Ng2StateDeclaration[]
23+
}
24+
25+
/**
26+
* Declares a NgModule with UI-Router states
27+
*
28+
* A Typescript decorator for declaring a [NgModule](https://angular.io/docs/ts/latest/guide/ngmodule.html)
29+
* which contains UI-Router states.
30+
*
31+
* This decorator analyzes the `states` in the module, and adds module `declarations` and `entryComponents`
32+
* for all routed Components.
33+
*
34+
* @example
35+
* ```js
36+
*
37+
* var homeState = { name: 'home', url: '/home', component: Home };
38+
* var aboutState = { name: 'about', url: '/about', component: About };
39+
* @UIRouterModule({
40+
* imports: [BrowserModule],
41+
* states: [homeState, aboutState]
42+
* }) export class AppModule {};
43+
* ```
44+
*
45+
* @param moduleMetaData the [[UIRouterModuleMetadata]]
46+
* (See also [NgModuleMetadataType](https://angular.io/docs/ts/latest/api/core/index/NgModuleMetadataType-interface.html)
47+
*/
48+
export function UIRouterModule(moduleMetaData: UIRouterModuleMetadata) {
49+
let states = moduleMetaData.states || [];
50+
let components = states.map(state => state.views || { $default: state })
51+
.map(viewObj => Object.keys(viewObj).map(key => viewObj[key].component))
52+
.reduce((acc, arr) => acc.concat(arr), [])
53+
.filter(x => typeof x === 'function');
54+
55+
moduleMetaData.imports = (moduleMetaData.imports || []).concat(_UIRouterModule);
56+
moduleMetaData.declarations = (moduleMetaData.declarations || []).concat(components);
57+
moduleMetaData.entryComponents = (moduleMetaData.entryComponents || []).concat(components);
58+
59+
return function(moduleClass) {
60+
return NgModule(moduleMetaData)(moduleClass);
61+
}
62+
}

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"declaration": true,
1010
"sourceMap": true
1111
},
12+
"lib": [ "es2015", "dom" ],
1213
"exclude": [
1314
"node_modules",
1415
"build",

0 commit comments

Comments
 (0)