import { IComponent , ComponentList , DirectiveList , IDirective } from '../types' ; import { utils } from '../utils' ; param { IComponent } componentInstance returns { void } function resolveInputs ( componentInstance ) { const templateMatchResult = componentInstance . $template . match ( /\"\{[^\{\}]+?\}\"/g ) ; if ( ! templateMatchResult ) return ; templateMatchResult . forEach ( ( matchProps ) => { const pureMatchProps = matchProps . replace ( /^\"\{/ , '' ) . replace ( /\}\"$/ , '' ) ; if ( /^.*\(.*\)$/ . test ( pureMatchProps ) ) { const args = pureMatchProps . match ( /\((.*)\)/ ) [ 1 ] . replace ( /\s+/g , '' ) . split ( ',' ) ; args . forEach ( arg => { const pureArg = arg . split ( '.' ) [ 0 ] ; if ( componentInstance && pureArg in componentInstance && componentInstance . $dependencesList . indexOf ( pureArg ) === - 1 ) componentInstance . $dependencesList . push ( pureArg ) ; } ) ; } else { const pureProps = pureMatchProps . split ( '.' ) [ 0 ] ; if ( componentInstance && pureProps in componentInstance && componentInstance . $dependencesList . indexOf ( pureProps ) === - 1 ) componentInstance . $dependencesList . push ( pureProps ) ; } } ) ; } param { IComponent } componentInstance returns { void } function resolveDirective ( componentInstance ) { const templateMatchResult = componentInstance . $template . match ( /nv\-[a-z,A-Z]+\=\"((?!nv\-)(?!\=).)+\"/g ) ; if ( ! templateMatchResult ) return ; templateMatchResult . forEach ( ( matchProps ) => { const pureMatchProps = matchProps . split ( '=' ) [ 1 ] . replace ( /^\"/ , '' ) . replace ( /\"$/ , '' ) ; if ( /^.*\(.*\)$/ . test ( pureMatchProps ) ) { const args = pureMatchProps . replace ( /^(\@)/ , '' ) . match ( /\((.*)\)/ ) [ 1 ] . replace ( /\s+/g , '' ) . split ( ',' ) ; args . forEach ( arg => { const pureArg = arg . split ( '.' ) [ 0 ] ; if ( componentInstance && pureArg in componentInstance && componentInstance . $dependencesList . indexOf ( pureArg ) === - 1 ) componentInstance . $dependencesList . push ( pureArg ) ; } ) ; } else { let pureProps = null ; if ( /^nv-repeat=.*/ . test ( matchProps ) ) { const _value = pureMatchProps . split ( ' in ' ) [ 1 ] ; if ( ! _value ) throw new Error ( `directive nv-repeat 's expression ${ pureMatchProps } is wrong!` ) ; const value = _value . replace ( /\s*/g , '' ) ; pureProps = value . split ( '.' ) [ 0 ] ; } if ( ! /^nv-repeat=.*/ . test ( matchProps ) ) pureProps = pureMatchProps . split ( '.' ) [ 0 ] ; if ( componentInstance && pureProps in componentInstance && componentInstance . $dependencesList . indexOf ( pureProps ) === - 1 ) componentInstance . $dependencesList . push ( pureProps ) ; } } ) ; } param { IComponent } componentInstance returns function resolveTemplateText ( componentInstance ) { const templateMatchResult = componentInstance . $template . match ( /(\{\{[^\{\}]+?\}\})/g ) ; if ( ! templateMatchResult ) return ; templateMatchResult . forEach ( ( matchProps ) => { const pureMatchProps = matchProps . replace ( /^\{\{/ , '' ) . replace ( /\}\}$/ , '' ) ; if ( /^.*\(.*\)$/ . test ( pureMatchProps ) ) { const args = pureMatchProps . match ( /\((.*)\)/ ) [ 1 ] . replace ( /\s+/g , '' ) . split ( ',' ) ; args . forEach ( arg => { const pureArg = arg . split ( '.' ) [ 0 ] ; if ( componentInstance && pureArg in componentInstance && componentInstance . $dependencesList . indexOf ( pureArg ) === - 1 ) componentInstance . $dependencesList . push ( pureArg ) ; } ) ; } else { const pureProps = pureMatchProps . split ( '.' ) [ 0 ] ; if ( componentInstance && pureProps in componentInstance && componentInstance . $dependencesList . indexOf ( pureProps ) === - 1 ) componentInstance . $dependencesList . push ( pureProps ) ; } } ) ; } export param { IComponent } componentInstance export function collectDependencesFromViewModel ( componentInstance ) { resolveInputs ( componentInstance ) ; resolveDirective ( componentInstance ) ; resolveTemplateText ( componentInstance ) ; } param { IComponent | IDirective } instance param { ( string | Function ) } selector returns { ( ComponentList [ ] | DirectiveList [ ] ) } export function buildFoundMap ( instance , selector ) { let toFindMap ; if ( typeof selector === 'string' ) { if ( ( instance . $declarationMap . get ( selector ) as any ) . nvType === 'nvComponent' ) toFindMap = instance . $componentList ; if ( ( instance . $declarationMap . get ( selector ) as any ) . nvType === 'nvDirective' ) toFindMap = instance . $directiveList ; } if ( utils . isFunction ( selector ) ) { if ( ( selector as any ) . nvType === 'nvComponent' ) toFindMap = instance . $componentList ; if ( ( selector as any ) . nvType === 'nvDirective' ) toFindMap = instance . $directiveList ; } return toFindMap ; }