import { IComponent , IDirective , IPipe } from '../types' ; import { ElementRef } from '../component' ; import { NvInstanceFactory } from '@indiv/di' ; export param { Function } ComponentClass param { * } inputs param { * } nativeElement param { IComponent } componentInstance returns { IComponent } export function buildComponentScope ( ComponentClass , inputs , nativeElement , componentInstance ) { const componentInjector = componentInstance . $privateInjector . fork ( ) ; componentInjector . setProviderAndInstance ( ElementRef , ElementRef , new ElementRef ( nativeElement ) ) ; const _component = NvInstanceFactory ( ComponentClass , null , componentInjector ) ; _component . $saveInputs = inputs ; _component . $nativeElement = nativeElement ; for ( const key in inputs ) { if ( _component . $inputsList ) { _component . $inputsList . forEach ( ( { propertyName , inputName } ) => { if ( inputName === key ) ( _component as any ) [ propertyName ] = inputs [ key ] ; } ) ; } } componentInstance . $declarationMap . forEach ( ( declaration , key ) => { if ( ! _component . $declarationMap . has ( key ) ) _component . $declarationMap . set ( key , declaration ) ; } ) ; return _component ; } export param { Function } DirectiveClass param { * } inputs param { * } nativeElement param { IComponent } componentInstance returns { IDirective } export function buildDirectiveScope ( DirectiveClass , inputs , nativeElement , componentInstance ) { const directiveInjector = componentInstance . $privateInjector . fork ( ) ; directiveInjector . setProviderAndInstance ( ElementRef , ElementRef , new ElementRef ( nativeElement ) ) ; const _directive = NvInstanceFactory ( DirectiveClass , null , directiveInjector ) ; _directive . $saveInputs = inputs ; _directive . $nativeElement = nativeElement ; if ( _directive . $inputsList ) { _directive . $inputsList . forEach ( ( { propertyName , inputName } ) => { if ( inputName === ( DirectiveClass as any ) . selector ) ( _directive as any ) [ propertyName ] = inputs ; } ) ; } componentInstance . $declarationMap . forEach ( ( declaration , key ) => { if ( ! _directive . $declarationMap . has ( key ) ) _directive . $declarationMap . set ( key , declaration ) ; } ) ; return _directive ; } export param { Function } PipeClass param { * } nativeElement param { IComponent } componentInstance returns { IPipe } export function buildPipeScope ( PipeClass , nativeElement , componentInstance ) { const findPipe = componentInstance . $pipeMap . get ( PipeClass ) ; if ( findPipe ) return findPipe ; const pipeInjector = componentInstance . $privateInjector . fork ( ) ; pipeInjector . setProviderAndInstance ( ElementRef , ElementRef , new ElementRef ( nativeElement ) ) ; const _pipe = NvInstanceFactory ( PipeClass , null , pipeInjector ) ; _pipe . $nativeElement = nativeElement ; componentInstance . $declarationMap . forEach ( ( declaration , key ) => { if ( ! _pipe . $declarationMap . has ( key ) ) _pipe . $declarationMap . set ( key , declaration ) ; } ) ; componentInstance . $pipeMap . set ( PipeClass , _pipe ) ; return _pipe ; }