1
- import { appName } from "./app.config" ;
1
+ import { IState , IStateProvider } from "angular-ui-router" ;
2
+
3
+ const appName = 'app' ;
2
4
3
5
const module = function ( moduleOrName ) {
4
6
return typeof moduleOrName === "string"
@@ -13,27 +15,30 @@ export function Component(options: {
13
15
templateUrl ?: string ,
14
16
bindings ?: any ,
15
17
require ?: any ,
16
- directives ?: any [ ]
17
- pipes ?: any [ ]
18
+ directives ?: any [ ] ,
19
+ pipes ?: any [ ] ,
18
20
providers ?: any [ ]
19
21
} , moduleOrName : string | ng . IModule = `${ appName } .components` ) {
20
- return ( controller : any ) => {
22
+ return ( Class : any ) => {
21
23
const selector = options . selector ;
22
24
delete options . selector ;
23
25
delete options . directives ;
24
26
delete options . pipes ;
25
27
delete options . providers ;
26
- module ( moduleOrName ) . component ( selector , angular . extend ( options , { controller : controller } ) ) ;
28
+ Class . selector = selector ;
29
+ module ( moduleOrName ) . component ( selector , angular . extend ( options , { controller : Class } ) ) ;
27
30
}
28
31
}
32
+ function annotate ( func : any ) {
33
+ const $injector = angular . injector ( [ 'ng' ] ) ;
34
+ func . $inject = $injector . annotate ( func ) . map ( member => member . replace ( / ^ _ / , '' ) ) ;
35
+ }
29
36
30
- export function Service ( moduleOrName : string | ng . IModule = `${ appName } .services` ) {
37
+ export function Injectable ( moduleOrName : string | ng . IModule = `${ appName } .services` ) {
31
38
return ( service : any ) => {
32
39
const name = service . name ;
33
40
const isProvider = service . hasOwnProperty ( '$get' ) ;
34
- if ( ! name ) {
35
- console . error ( 'Service decorator can be used with named class only' ) ;
36
- }
41
+ annotate ( service ) ;
37
42
module ( moduleOrName ) [ isProvider ? 'provider' : 'service' ] ( name , service ) ;
38
43
}
39
44
}
@@ -48,32 +53,41 @@ export interface PipeTransform {
48
53
49
54
export function Pipe ( options : { name : string } , moduleOrName : string | ng . IModule = `${ appName } .pipes` ) {
50
55
return ( Pipe : PipeTransformStatic ) => {
51
- const filter = ( ) => {
52
- console . log ( Pipe . $inject ) ;
53
- //@todo : add support for injection across all registered modules
54
- const $injector = angular . injector ( [ 'ng' ] ) ;
55
- const instance :any = $injector . instantiate ( Pipe ) ;
56
- return instance . transform . bind ( instance ) ;
57
- } ;
58
- module ( moduleOrName ) . filter ( options . name , filter ) ;
56
+ annotate ( Pipe ) ;
57
+ //@todo : add support for injection across all registered modules
58
+ debugger ;
59
+ const $injector = angular . injector ( [ 'ng' ] ) ;
60
+ const instance :any = $injector . instantiate ( Pipe ) ;
61
+ module ( moduleOrName ) . filter ( options . name , ( ) => instance . transform . bind ( instance ) ) ;
59
62
}
60
63
}
61
64
65
+ export interface IComponentState extends IState {
66
+ state : string ,
67
+ component ?: any ,
68
+ views ?: { [ name : string ] : IComponentState }
69
+ }
62
70
63
- export function Injectable ( ) {
64
- return ( Class : any ) => {
65
- const $injector = angular . injector ( [ 'ng' ] ) ;
66
- Class . $inject = $injector . annotate ( Class ) . map ( ( member ) => member . replace ( / ^ _ / , '' ) ) ;
67
- }
71
+ function setTemplate ( state : IComponentState ) {
72
+ const selector = state . component . selector ;
73
+ state . template = `<${ selector } ></${ selector } >` ;
74
+ delete state . component ;
68
75
}
69
76
70
- export function bootstrap ( appName : string , appClass : any ) {
71
- return ( anything : any ) => {
72
- if ( ! appClass ) {
73
- console . error ( `Please provide main component class as a second argument to @bootstrap decorator` ) ;
77
+ export function provideStates ( states : IComponentState [ ] , $stateProvider : IStateProvider ) {
78
+ states . map ( ( config ) => {
79
+ const name = config . state ;
80
+ const namedState = config . views ;
81
+ if ( namedState ) {
82
+ const namedViews = Object . keys ( namedState ) ;
83
+ namedViews . forEach ( ( view ) => {
84
+ setTemplate ( namedState [ view ] ) ;
85
+ } ) ;
74
86
}
75
- angular . element ( document ) . ready ( ( ) => {
76
- angular . bootstrap ( document , [ appName ] ) ;
77
- } ) ;
78
- }
87
+ else {
88
+ setTemplate ( config ) ;
89
+ }
90
+ delete config . state ;
91
+ return { name, config } ;
92
+ } ) . forEach ( state => $stateProvider . state ( state . name , state . config ) ) ;
79
93
}
0 commit comments