Last active
January 21, 2017 00:19
-
-
Save miguelramos/c0b3b96b846eeb0bcbe42e240d0077ea to your computer and use it in GitHub Desktop.
Angular 2 decorator
This file contains hidden or 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
export interface UIDecorator { | |
footer?: boolean; | |
navigator?: UINavbarState; | |
} | |
export class UIElement {} | |
export function UI(meta?: UIDecorator): any { | |
let decorator = <(meta?: UIDecorator) => any>makeDecorator( | |
'UI', | |
<UIDecorator>{ | |
footer: undefined, | |
navigator: undefined | |
}, | |
UIElement | |
); | |
return function (cls) { | |
let onInit = cls.prototype.ngOnInit || function() { }; | |
let onSetup = cls.prototype.ngOnSetup; | |
return (<any>decorator(meta)).Class({ | |
// extends: Object.prototype.constructor, | |
constructor: cls, | |
ngOnSetup: function () { | |
let state: State = this.state || null; | |
const isUIElement = annotation => annotation instanceof UIElement; | |
const getAnnotation = annotation => annotation; | |
const annotations: any[] = Reflect.getOwnMetadata('annotations', cls); | |
const annotation: UIDecorator = head(annotations.filter(isUIElement).map(getAnnotation)); | |
if (!this.hasOwnProperty('state')) { | |
let injector = Framework.getApplicationInjector(); | |
state = <State>injector.get(State); | |
} | |
if (annotation.footer) { | |
let footer = state.get(SYMBOLS.UI_STATE_FOOTER); | |
state.set(SYMBOLS.UI_STATE_FOOTER, merge(footer, { enable: annotation.footer })); | |
} | |
if (annotation.navigator) { | |
let nav = state.get(SYMBOLS.UI_STATE_NAVBAR); | |
state.set(SYMBOLS.UI_STATE_NAVBAR, merge(nav, annotation.navigator)); | |
} | |
}, | |
ngOnInit: function () { | |
onInit.call(this); | |
onSetup ? onSetup.call(this) : this.ngOnSetup(); | |
} | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment