import { InputProperty , OutputProperty , } from './node' ; import { functionName } from '../utils' ; const ANNOTATIONS_PROP_KEY = '__annotations__' ; export const propertyDecorators = ( instance ) : Array < any > => Reflect . getOwnMetadata ( 'propMetadata' , instance . constructor ) || [ ] ; export const parameterTypes = ( instance ) : Array < any > => { return ( Reflect . getOwnMetadata ( 'design:paramtypes' , instance . constructor ) || [ ] ) . map ( param => typeof param !== 'function' || param . name === 'Object' ? null : param ) ; } ; export const injectedParameterDecorators = ( instance ) : Array < any > => Reflect . getOwnMetadata ( 'parameters' , instance . constructor ) || instance . constructor . __parameters__ || instance . constructor . __paramaters__ ; export const iteratePropertyDecorators = ( instance , fn ) => { if ( instance == null ) { return ; } const decorators = propertyDecorators ( instance ) ; for ( const key of Object . keys ( decorators ) ) { for ( const meta of decorators [ key ] ) { fn ( key , meta ) ; } } } ; export const componentMetadata = ( token ) => { if ( ! token ) { return null ; } const metadata = ( token [ ANNOTATIONS_PROP_KEY ] || [ ] ) . find ( d => Object . getPrototypeOf ( d ) . ngMetadataName === 'Component' ) ; return metadata ? metadata : ( Reflect . getOwnMetadata ( 'annotations' , token ) || [ ] ) . find ( d => d . toString ( ) === '@Component' ) ; } ; export const componentInputs = ( metadata , instance ) : Array < InputProperty > => { const inputs = ( ( metadata && metadata . inputs ) || [ ] ) . map ( p => ( { propertyKey : p } ) ) ; iteratePropertyDecorators ( instance , ( key , meta ) => { if ( inputs . find ( i => i . propertyKey === key ) == null ) { if ( meta . toString ( ) === '@Input' ) { inputs . push ( { propertyKey : key , bindingPropertyName : meta . bindingPropertyName } ) ; } } } ) ; return inputs ; } ; export const componentOutputs = ( metadata , instance ) : Array < OutputProperty > => { const outputs = ( ( metadata && metadata . outputs ) || [ ] ) . map ( p => ( { propertyKey : p } ) ) ; iteratePropertyDecorators ( instance , ( key , meta ) => { if ( meta . toString ( ) === '@Output' ) { outputs . push ( { propertyKey : key , bindingPropertyName : meta . bindingPropertyName } ) ; } } ) ; return Array . from ( outputs ) ; } ; export interface Query { propertyKey : string ; selector : string ; } export const componentQueryChildren = ( type , metadata , instance ) : Array < Query > => { const queries = new Array < Query > ( ) ; iteratePropertyDecorators ( instance , ( key , meta ) => { if ( meta . toString ( ) === type ) { queries . push ( { propertyKey : key , selector : functionName ( meta . selector ) } ) ; } } ) ; return queries ; } ;