import { DirectiveList , IComponent , TComAndDir } from '../types' ; import { utils } from '../utils' ; import { buildDirectiveScope } from './compiler-utils' ; import { lifecycleCaller } from '../lifecycle' ; export param { IComponent } componentInstance param { TComAndDir } componentAndDirectives export function directivesConstructor ( componentInstance , componentAndDirectives ) { componentInstance . $directiveList = [ ] ; componentAndDirectives . directives . forEach ( directive => { const declaration = componentInstance . $declarationMap . get ( directive . name ) ; componentInstance . $directiveList . push ( { nativeElement : directive . nativeElement , inputs : directive . inputs , instanceScope : null , constructorFunction : declaration , isFromContent : directive . isFromContent , } ) ; } ) ; } export param { IComponent } componentInstance param { TComAndDir } componentAndDirectives export function mountDirective ( componentInstance , componentAndDirectives ) { const cacheDirectiveList = [ ... componentInstance . $directiveList ] ; const foundCacheDirectiveList = [ ] ; directivesConstructor ( componentInstance , componentAndDirectives ) ; const directiveListLength = componentInstance . $directiveList . length ; for ( let i = 0 ; i < directiveListLength ; i ++ ) { const directive = componentInstance . $directiveList [ i ] ; const cacheDirectiveIndex = cacheDirectiveList . findIndex ( cache => cache . nativeElement === directive . nativeElement ) ; const cacheDirective = cacheDirectiveList [ cacheDirectiveIndex ] ; if ( cacheDirectiveIndex !== - 1 ) { cacheDirectiveList . splice ( cacheDirectiveIndex , 1 ) ; foundCacheDirectiveList . push ( cacheDirective ) ; } if ( cacheDirective ) { directive . instanceScope = cacheDirective . instanceScope ; if ( ! utils . isEqual ( directive . instanceScope . $saveInputs , directive . inputs ) ) { if ( directive . instanceScope . nvReceiveInputs ) directive . instanceScope . nvReceiveInputs ( directive . inputs ) ; directive . instanceScope . $saveInputs = directive . inputs ; if ( directive . instanceScope . $inputsList ) { directive . instanceScope . $inputsList . forEach ( ( { propertyName , inputName } ) => { if ( inputName === ( directive . instanceScope as any ) . constructor . selector ) ( directive . instanceScope as any ) [ propertyName ] = directive . inputs ; } ) ; } } } else { directive . instanceScope = buildDirectiveScope ( directive . constructorFunction , directive . inputs , directive . nativeElement , componentInstance ) ; } directive . instanceScope . $indivInstance = componentInstance . $indivInstance ; if ( ! cacheDirective ) { lifecycleCaller ( directive . instanceScope , 'nvOnInit' ) ; lifecycleCaller ( directive . instanceScope , 'nvBeforeMount' ) ; } } const cacheDirectiveListLength = cacheDirectiveList . length ; for ( let i = 0 ; i < cacheDirectiveListLength ; i ++ ) { const cache = cacheDirectiveList [ i ] ; lifecycleCaller ( cache . instanceScope , 'nvOnDestory' ) ; } for ( let i = 0 ; i < directiveListLength ; i ++ ) { const directive = componentInstance . $directiveList [ i ] ; lifecycleCaller ( directive . instanceScope , 'nvHasRender' ) ; if ( ! foundCacheDirectiveList . find ( cache => cache . nativeElement === directive . nativeElement ) ) { if ( ! directive . instanceScope . $indivInstance . getIndivEnv . isServerRendering ) lifecycleCaller ( directive . instanceScope , 'nvAfterMount' ) ; } } }