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
+ }
0 commit comments