Created
April 7, 2016 23:38
-
-
Save olostan/7e30bcdcabc3638123e008f664cf6d0f to your computer and use it in GitHub Desktop.
TypeScript Annotations for Angular 1 (full version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as ng from 'angular'; | |
interface Function { | |
(...any):any; | |
$inject?: any; | |
injectableType?: string; | |
injectableName?: string; | |
} | |
type BindingType = '<' | '@' | '&' | '='; | |
interface Bindings { | |
[property:string]:BindingType | |
} | |
export interface IComponentDefinitionObject { | |
bindings?:Bindings; | |
controllerAs?:string; | |
require?; | |
template?; | |
templateUrl?; | |
transclude?; | |
$routeConfig?: ng.RouteDefinition[]; | |
} | |
interface InjectableComponentDefinitionObject { | |
controller: Function; | |
injectableType: string; | |
injectableName: string; | |
} | |
function component(name:string, definition:IComponentDefinitionObject):any { | |
return (target:Function) => { | |
const injectee = <InjectableComponentDefinitionObject>definition; | |
injectee.controller = target; | |
injectee.injectableName = name; | |
injectee.injectableType = 'component'; | |
return injectee | |
}; | |
} | |
function injector(type:string, name:string, values:string[]):any { | |
return (target:Function) => { | |
const injectee:Function = (...args:any[]):Object => { | |
return ((classConstructor:Function, args:any[], ctor:any):Object => { | |
ctor.prototype = classConstructor.prototype; | |
const child:Object = new ctor; | |
const result:Object = classConstructor.apply(child, args); | |
return typeof result === "object" ? result : child; | |
})(target, args, () => { | |
return null; | |
}); | |
}; | |
injectee.$inject = values; | |
if (name) injectee.injectableName = name; | |
if (type) injectee.injectableType = type; | |
return injectee; | |
}; | |
} | |
var inject = injector.bind(null, null, null); | |
var service = injector.bind(null, "service"); | |
var directive = injector.bind(null, "directive"); | |
var controller = injector.bind(null, "controller"); | |
function register(module:ng.IModule, o:any) { | |
if(!o){ | |
throw Error("Can't register an undefined component!"); | |
} | |
var registrant = module[o.injectableType]; | |
if (!registrant) throw `Unknown injectable type '${o.injectableType}' for '${o.injectableName}'`; | |
registrant.call(module, o.injectableName, o); | |
} | |
export {inject, service, directive, controller, component, register}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment