1- import { appName } from "./app.config" ;
1+ import { IState , IStateProvider } from "angular-ui-router" ;
2+
3+ const appName = 'app' ;
24
35const module = function ( moduleOrName ) {
46 return typeof moduleOrName === "string"
@@ -13,27 +15,30 @@ export function Component(options: {
1315 templateUrl ?: string ,
1416 bindings ?: any ,
1517 require ?: any ,
16- directives ?: any [ ]
17- pipes ?: any [ ]
18+ directives ?: any [ ] ,
19+ pipes ?: any [ ] ,
1820 providers ?: any [ ]
1921} , moduleOrName : string | ng . IModule = `${ appName } .components` ) {
20- return ( controller : any ) => {
22+ return ( Class : any ) => {
2123 const selector = options . selector ;
2224 delete options . selector ;
2325 delete options . directives ;
2426 delete options . pipes ;
2527 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 } ) ) ;
2730 }
2831}
32+ function annotate ( func : any ) {
33+ const $injector = angular . injector ( [ 'ng' ] ) ;
34+ func . $inject = $injector . annotate ( func ) . map ( member => member . replace ( / ^ _ / , '' ) ) ;
35+ }
2936
30- export function Service ( moduleOrName : string | ng . IModule = `${ appName } .services` ) {
37+ export function Injectable ( moduleOrName : string | ng . IModule = `${ appName } .services` ) {
3138 return ( service : any ) => {
3239 const name = service . name ;
3340 const isProvider = service . hasOwnProperty ( '$get' ) ;
34- if ( ! name ) {
35- console . error ( 'Service decorator can be used with named class only' ) ;
36- }
41+ annotate ( service ) ;
3742 module ( moduleOrName ) [ isProvider ? 'provider' : 'service' ] ( name , service ) ;
3843 }
3944}
@@ -48,32 +53,41 @@ export interface PipeTransform {
4853
4954export function Pipe ( options : { name : string } , moduleOrName : string | ng . IModule = `${ appName } .pipes` ) {
5055 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 ) ) ;
5962 }
6063}
6164
65+ export interface IComponentState extends IState {
66+ state : string ,
67+ component ?: any ,
68+ views ?: { [ name : string ] : IComponentState }
69+ }
6270
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 ;
6875}
6976
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+ } ) ;
7486 }
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 ) ) ;
7993}
0 commit comments