import { Injectable , OnDestroy , Renderer2 , RendererFactory2 , RendererStyleFlags2 , RendererType2 } from '@angular/core' ; import { Subscription } from 'rxjs' ; import { AllocatedNode , CommandType , fnArg , MessageBus , ObjectStore , RendererAdapter2 , SerializerTypes , SharedStylesHost } from '../shared' ; import { ServerGlobalEvents } from './server-global-events' ; @ Injectable ( ) export class ServerRendererFactory2 implements RendererFactory2 , OnDestroy { private subscription : Subscription ; constructor ( private sharedStylesHost , private bus , private store , private rendererAdapter , private globalEvents ) { this . subscription = this . bus . getCommands ( ) . subscribe ( ( command ) => { if ( command . target === 'renderer' && command . method === 'event' ) { this . rendererAdapter . event ( fnArg ( command . fnArgs [ 0 ] , SerializerTypes . STORE_OBJECT ) , fnArg ( command . fnArgs [ 1 ] , SerializerTypes . STORE_OBJECT ) , fnArg ( command . fnArgs [ 2 ] ) , fnArg ( command . fnArgs [ 3 ] ) , fnArg ( command . fnArgs [ 4 ] , SerializerTypes . DOM_EVENT ) ) ; } } ) ; } createRenderer ( hostElement , type ) { const renderer = new ServerRenderer2 ( this , this . rendererAdapter ) ; this . allocateNode ( renderer ) ; this . rendererAdapter . createRenderer ( fnArg ( hostElement , SerializerTypes . STORE_OBJECT ) , fnArg ( type , SerializerTypes . RENDERER_TYPE_2 ) , fnArg ( renderer , SerializerTypes . STORE_OBJECT ) ) ; return renderer ; } allocateId ( ) { return this . store . allocateId ( ) ; } allocateNode ( node ? ) { return this . store . allocateNode ( node ) ; } deallocateNode ( node ) { this . store . deallocateNode ( node ) ; } globalListen ( eventName , listener ) { return this . globalEvents . listen ( eventName , listener ) ; } ngOnDestroy ( ) { if ( this . subscription ) { this . subscription . unsubscribe ( ) ; this . subscription = null ; } } } @ Injectable ( ) export class ServerRenderer2 implements Renderer2 { constructor ( private factory , private rendererAdapter ) { } readonly data : { [ p ] : any ; } ; destroyNode ( node ) { this . rendererAdapter . destroyNode ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( node , SerializerTypes . STORE_OBJECT ) ) ; this . deallocateNode ( node ) ; } addClass ( el , name ) { this . rendererAdapter . addClass ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( el , SerializerTypes . STORE_OBJECT ) , fnArg ( name ) ) ; } appendChild ( parent , newChild ) { this . rendererAdapter . appendChild ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( parent , SerializerTypes . STORE_OBJECT ) , fnArg ( newChild , SerializerTypes . STORE_OBJECT ) ) ; } createComment ( value ) { const commentElement = this . allocateNode ( ) ; this . rendererAdapter . createComment ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( value ) , fnArg ( commentElement , SerializerTypes . STORE_OBJECT ) ) ; return commentElement ; } createElement ( name , namespace ? ) { const element = this . allocateNode ( ) ; this . rendererAdapter . createElement ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( name ) , fnArg ( namespace ) , fnArg ( element , SerializerTypes . STORE_OBJECT ) ) ; return element ; } createText ( value ) { const textElement = this . allocateNode ( ) ; this . rendererAdapter . createText ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( value ) , fnArg ( textElement , SerializerTypes . STORE_OBJECT ) ) ; return textElement ; } destroy ( ) { this . rendererAdapter . destroy ( fnArg ( this , SerializerTypes . STORE_OBJECT ) ) ; } insertBefore ( parent , newChild , refChild ) { this . rendererAdapter . insertBefore ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( parent , SerializerTypes . STORE_OBJECT ) , fnArg ( newChild , SerializerTypes . STORE_OBJECT ) , fnArg ( refChild , SerializerTypes . STORE_OBJECT ) ) ; } listen ( target , eventName , callback ) { const listenId = this . allocateId ( ) ; const targetEl = typeof target === 'string' ? null : target ; const targetName = typeof target === 'string' ? target : null ; const globalEvent = typeof target === 'string' ? `${ targetName }:${ eventName }` : null ; const unlisten = targetName ? this . globalListen ( globalEvent , callback ) : targetEl . events . listen ( eventName , callback ) ; this . rendererAdapter . listen ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( targetEl , SerializerTypes . STORE_OBJECT ) , fnArg ( targetName ) , fnArg ( eventName ) , fnArg ( listenId ) ) ; return ( ) => { this . rendererAdapter . unlisten ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( listenId ) ) ; unlisten ( ) ; } ; } nextSibling ( node ) { const element = this . allocateNode ( ) ; this . rendererAdapter . nextSibling ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( node , SerializerTypes . STORE_OBJECT ) , fnArg ( element , SerializerTypes . STORE_OBJECT ) ) ; return element ; } parentNode ( node ) { const element = this . allocateNode ( ) ; this . rendererAdapter . parentNode ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( node , SerializerTypes . STORE_OBJECT ) , fnArg ( element , SerializerTypes . STORE_OBJECT ) ) ; return element ; } removeAttribute ( el , name , namespace ? ) { this . rendererAdapter . removeAttribute ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( el , SerializerTypes . STORE_OBJECT ) , fnArg ( name ) , fnArg ( namespace ) ) ; } removeChild ( parent , oldChild , isHostElement ? ) { this . rendererAdapter . removeChild ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( parent , SerializerTypes . STORE_OBJECT ) , fnArg ( oldChild , SerializerTypes . STORE_OBJECT ) , fnArg ( isHostElement ) ) ; } removeClass ( el , name ) { this . rendererAdapter . removeClass ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( el , SerializerTypes . STORE_OBJECT ) , fnArg ( name ) ) ; } removeStyle ( el , style , flags ? ) { this . rendererAdapter . removeStyle ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( el , SerializerTypes . STORE_OBJECT ) , fnArg ( style ) , fnArg ( flags ) ) ; } selectRootElement ( selectorOrNode , preserveContent ? ) { const rootElement = this . allocateNode ( ) ; this . rendererAdapter . selectRootElement ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( selectorOrNode ) , fnArg ( preserveContent ) , fnArg ( rootElement , SerializerTypes . STORE_OBJECT ) ) ; return rootElement ; } setAttribute ( el , name , value , namespace ? ) { this . rendererAdapter . setAttribute ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( el , SerializerTypes . STORE_OBJECT ) , fnArg ( name ) , fnArg ( value ) , fnArg ( namespace ) ) ; } setProperty ( el , name , value ) { this . rendererAdapter . setProperty ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( el , SerializerTypes . STORE_OBJECT ) , fnArg ( name ) , fnArg ( value ) ) ; } setStyle ( el , style , value , flags ? ) { this . rendererAdapter . setStyle ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( el , SerializerTypes . STORE_OBJECT ) , fnArg ( style ) , fnArg ( value ) , fnArg ( flags ) ) ; } setValue ( node , value ) { this . rendererAdapter . setValue ( fnArg ( this , SerializerTypes . STORE_OBJECT ) , fnArg ( node , SerializerTypes . STORE_OBJECT ) , fnArg ( value ) ) ; } private allocateId ( ) { return this . factory . allocateId ( ) ; } private allocateNode ( ) { return this . factory . allocateNode ( ) ; } private deallocateNode ( node ) { return this . factory . deallocateNode ( node ) ; } private globalListen ( eventName , listener ) { return this . factory . globalListen ( eventName , listener ) ; } }