Skip to content

Instantly share code, notes, and snippets.

@miguelramos
Last active January 21, 2017 00:19
Show Gist options
  • Save miguelramos/c0b3b96b846eeb0bcbe42e240d0077ea to your computer and use it in GitHub Desktop.
Save miguelramos/c0b3b96b846eeb0bcbe42e240d0077ea to your computer and use it in GitHub Desktop.
Angular 2 decorator
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