import { AUGURY_TOKEN_ID_METADATA_KEY } from './parse-modules' ; import { pathExists , getAtPath } from '../../utils/property-path' ; import { functionName } from '../../utils' ; export interface Dependency { id : string ; name : string ; decorators : Array < string > ; } export interface Property { id ? : string ; key : string ; value ; } export const isDebugElementComponent = ( element ) => ! ! element . componentInstance && ! componentInstanceExistsInParentChain ( element ) ; export const getComponentName = ( element ) : string => { if ( element . componentInstance && element . componentInstance . constructor && ! componentInstanceExistsInParentChain ( element ) ) { return functionName ( element . componentInstance . constructor ) ; } else if ( element . name ) { return element . name ; } return element . nativeElement . tagName . toLowerCase ( ) ; } ; const componentInstanceExistsInParentChain = ( debugElement ) => { const componentInstanceRef = debugElement . componentInstance ; while ( componentInstanceRef && debugElement . parent ) { if ( componentInstanceRef === debugElement . parent . componentInstance ) { return true ; } debugElement = debugElement . parent ; } return false ; } ; const getPropsIfTheyExist = ( object , props ) : Array < any > => { const properties = [ ] ; props . forEach ( ( prop ) => { const label = prop [ 0 ] ; const path = prop . length > 1 ? prop . slice ( 1 , prop . length ) : prop [ 0 ] ; if ( pathExists ( object , ... path ) ) { properties . push ( { key : label , value : getAtPath ( object , ... path ) . value } ) ; } } ) ; return properties ; } ; export abstract class Description { public static getProviderDescription ( provider , instance ) { if ( typeof provider === 'string' ) { return { key : provider , value : null } ; } return { id : Reflect . getMetadata ( AUGURY_TOKEN_ID_METADATA_KEY , provider ) , key : provider . name , value : null , } ; } public static getComponentDescription ( debugElement ) { if ( debugElement == null ) { return [ ] ; } let componentName ; const element = pathExists ( debugElement , 'nativeElement' ) ? debugElement . nativeElement : null ; if ( debugElement . componentInstance && ! componentInstanceExistsInParentChain ( debugElement ) ) { componentName = pathExists ( debugElement , 'componentInstance' , 'constructor' , 'name' ) ? debugElement . componentInstance . constructor . name : null ; } else { componentName = pathExists ( element , 'tagName' ) ? element . tagName . toLowerCase ( ) : null ; } const properties = [ ] ; switch ( componentName ) { case 'a' : return getPropsIfTheyExist ( element , [ [ 'text' ] , [ 'hash' ] , ] ) ; case 'form' : return getPropsIfTheyExist ( element , [ [ 'method' ] ] ) ; case 'input' : return getPropsIfTheyExist ( element , [ [ 'id' ] , [ 'name' ] , [ 'type' ] , [ 'required' ] ] ) ; case 'router-outlet' : const routerOutletProvider = debugElement . providerTokens . reduce ( ( prev , curr ) => prev ? prev : curr . name === 'RouterOutlet' ? curr : null , null ) ; return getPropsIfTheyExist ( debugElement . injector . get ( routerOutletProvider ) , [ [ 'name' ] ] ) ; case 'NgSelectOption' : return ( element ) ? Description . _getSelectOptionDesc ( element ) : [ ] ; case 'NgIf' : return Description . _getNgIfDesc ( debugElement . componentInstance ) ; case 'NgControlName' : return Description . _getControlNameDesc ( debugElement . componentInstance ) ; case 'NgSwitch' : return Description . _getNgSwitchDesc ( debugElement . componentInstance ) ; case 'NgSwitchWhen' : case 'NgSwitchDefault' : return Description . _getNgSwitchWhenDesc ( debugElement . componentInstance ) ; } return properties ; } private static _getSelectOptionDesc ( element ) { return getPropsIfTheyExist ( element , [ [ 'label' , 'innerText' ] , ] ) . concat ( [ { key : 'value' , value : element . getAttribute ( 'value' ) } ] ) ; } private static _getControlNameDesc ( instance ) { return getPropsIfTheyExist ( instance , [ [ 'name' ] , [ 'value' ] , [ 'valid' ] , ] ) ; } private static _getNgSwitchDesc ( instance ) { const properties = getPropsIfTheyExist ( instance , [ [ 'useDefault' , '_useDefault' ] , [ 'switchDefault' , '_switchValue' ] , [ 'valuesCount' , '_valueViews' ] , ] ) ; properties . filter ( element => element . key === 'valuesCount' ) . forEach ( element => element . value = element . value ? element . value . size : 0 ) ; return properties ; } private static _getNgSwitchWhenDesc ( instance ) { return getPropsIfTheyExist ( instance , [ [ 'value' , '_value' ] , ] ) ; } private static _getNgIfDesc ( instance ) { return getPropsIfTheyExist ( instance , [ [ 'condition' , '_prevCondition' ] , ] ) ; } }