import { Injectable , Renderer2 , RendererFactory2 } from '@angular/core' ; import { command , DomEventArg , FnArg , fnArg , MessageBus , ObjectStore , PrimitiveArg , RendererAdapter2 , RendererMethods2 , RendererType2Arg , Serializer , SerializerTypes , StoreObjectArg } from '../shared' ; @ Injectable ( ) export class BrowserRendererAdapter2 extends RendererAdapter2 { constructor ( private serializer , private store , private factory , private bus ) { super ( ) ; } begin ( ) { this . factory . begin ( ) ; } end ( ) { this . factory . end ( ) ; } addClass ( rendererArg , elArg , nameArg ) { const [ renderer , el , name ] = this . deserialize ( ... arguments ) ; if ( el . classList ) { renderer . addClass ( el , name ) ; } } appendChild ( rendererArg , parentArg , newChildArg ) { const [ renderer , parent , newChild ] = this . deserialize ( ... arguments ) ; renderer . appendChild ( parent , newChild ) ; } event ( rendererArg , targetElArg , targetNameArg , eventNameArg , eventArg ) { this . callServerRenderer ( 'event' , ... arguments ) ; } createComment ( rendererArg , valueArg , commentElementArg ) { const [ renderer , value ] = this . deserialize ( ... arguments ) ; const commentElement = renderer . createComment ( value ) ; this . store . allocateNode ( commentElement , commentElementArg . value ) ; } createElement ( rendererArg , nameArg , namespaceArg , elementArg ) { const [ renderer , name , namespace ] = this . deserialize ( ... arguments ) ; const element = renderer . createElement ( name , namespace ) ; this . store . allocateNode ( element , elementArg . value ) ; } createRenderer ( hostElementArg , typeArg , rendererArg ) { const [ hostElement , type ] = this . deserialize ( ... arguments ) ; const renderer = this . factory . createRenderer ( hostElement , type ) ; this . store . allocateNode ( renderer , rendererArg . value ) ; } createText ( rendererArg , valueArg , textElementArg ) { const [ renderer , value ] = this . deserialize ( ... arguments ) ; const textElement = renderer . createText ( value ) ; this . store . allocateNode ( textElement , textElementArg . value ) ; } destroy ( rendererArg ) { const [ renderer ] = this . deserialize ( ... arguments ) ; renderer . destroy ( ) ; this . store . deallocateNode ( renderer ) ; } destroyNode ( rendererArg , nodeArg ) { const [ renderer , node ] = this . deserialize ( ... arguments ) ; if ( renderer . destroyNode ) { renderer . destroyNode ( node ) ; } this . store . deallocateNode ( node ) ; } insertBefore ( rendererArg , parentArg , newChildArg , refChildArg ) { const [ renderer , parent , newChild , refChild ] = this . deserialize ( ... arguments ) ; renderer . insertBefore ( parent , newChild , refChild ) ; } listen ( rendererArg , targetElArg , targetNameArg , eventNameArg , listenIdArg ) { const [ renderer , targetEl , targetName , eventName , listenId ] = this . deserialize ( ... arguments ) ; const unlisten = renderer . listen ( targetEl || targetName , eventName , ( event ) => { this . event ( fnArg ( renderer , SerializerTypes . STORE_OBJECT ) , fnArg ( targetEl , SerializerTypes . STORE_OBJECT ) , fnArg ( targetName ) , fnArg ( eventName ) , fnArg ( event , SerializerTypes . DOM_EVENT ) ) ; return false ; } ) ; this . store . allocateNode ( unlisten , listenId ) ; } nextSibling ( rendererArg , nodeArg , elementArg ) { const [ renderer , node ] = this . deserialize ( ... arguments ) ; const element = renderer . nextSibling ( node ) ; this . store . allocateNode ( element , elementArg . value ) ; } parentNode ( rendererArg , nodeArg , elementArg ) { const [ renderer , node ] = this . deserialize ( ... arguments ) ; const element = renderer . parentNode ( node ) ; this . store . allocateNode ( element , elementArg . value ) ; } removeAttribute ( rendererArg , elArg , nameArg , namespaceArg ) { const [ renderer , el , name , namespace ] = this . deserialize ( ... arguments ) ; renderer . removeAttribute ( el , name , namespace ) ; } removeChild ( rendererArg , parentArg , oldChildArg , isHostElementArg ) { const [ renderer , parent , oldChild , isHostElement ] = this . deserialize ( ... arguments ) ; renderer . removeChild ( parent , oldChild , isHostElement ) ; } removeClass ( rendererArg , elArg , nameArg ) { const [ renderer , el , name ] = this . deserialize ( ... arguments ) ; if ( el . classList ) { renderer . removeClass ( el , name ) ; } } removeStyle ( rendererArg , elArg , styleArg , flagsArg ) { const [ renderer , el , style , flags ] = this . deserialize ( ... arguments ) ; renderer . removeStyle ( el , style , flags ) ; } selectRootElement ( rendererArg , selectorOrNodeArg , preserveContentArg , rootElementArg ) { const [ renderer , selector , preserveContent ] = this . deserialize ( ... arguments ) ; const rootElement = renderer . selectRootElement ( selector , preserveContent ) ; this . store . allocateNode ( rootElement , rootElementArg . value ) ; } setAttribute ( rendererArg , elArg , nameArg , valueArg , namespaceArg ) { const [ renderer , el , name , value , namespace ] = this . deserialize ( ... arguments ) ; renderer . setAttribute ( el , name , value , namespace ) ; } setProperty ( rendererArg , elArg , nameArg , valueArg ) { const [ renderer , el , name , value ] = this . deserialize ( ... arguments ) ; renderer . setProperty ( el , name , value ) ; } setStyle ( rendererArg , elArg , styleArg , valueArg , flagsArg ) { const [ renderer , el , style , value , flags ] = this . deserialize ( ... arguments ) ; renderer . setStyle ( el , style , value , flags ) ; } setValue ( rendererArg , nodeArg , valueArg ) { const [ renderer , node , value ] = this . deserialize ( ... arguments ) ; renderer . setValue ( node , value ) ; } unlisten ( rendererArg , listenIdArg ) { const [ renderer , listenId ] = this . deserialize ( ... arguments ) ; const unlisten = this . store . deserialize ( listenId ) ; if ( typeof unlisten === 'function' ) { unlisten ( ) ; } this . store . deallocateNode ( unlisten ) ; } private callServerRenderer ( method , ... fnArgs ) { const payload = fnArgs . map ( arg => this . serializer . serialize ( arg . value , arg . type ) ) ; this . bus . sendCommand ( command ( 'renderer' , method , payload ) ) ; } private deserialize ( ... fnArgs ) { return fnArgs . map ( arg => this . serializer . deserialize ( arg . value , arg . type ) ) as [ Renderer2 , ... any [ ] ] ; } }