import { PlatformLocation } from '@angular/common' ; import { Injectable , Injector } from '@angular/core' ; import { command , FnArg , fnArg , LocationAdapter , LocationMethods , MessageBus , ObjectStore , PrimitiveArg , Serializer , SerializerTypes , StoreObjectArg } from '../shared' ; @ Injectable ( ) export class BrowserLocationAdapter extends LocationAdapter { constructor ( private bus , private serializer , private store , private platformLocation ) { super ( ) ; } back ( ) { this . platformLocation . back ( ) ; } forward ( ) { this . platformLocation . forward ( ) ; } getBaseHrefFromDOM ( baseHrefArg ) { this . callServerLocation ( 'getBaseHrefFromDOM' , ... arguments ) ; } getLocation ( locationArg ) { this . callServerLocation ( 'getLocation' , ... arguments ) ; } getState ( stateArg ) { this . callServerLocation ( 'getState' , ... arguments ) ; } hashChange ( handlerArg , eventArg ) { this . callServerLocation ( 'hashChange' , ... arguments ) ; } onHashChange ( handlerArg ) { const handler = ( event ) => this . hashChange ( fnArg ( handler , SerializerTypes . STORE_OBJECT ) , fnArg ( event ) ) ; this . store . allocateNode ( handler , handlerArg . value ) ; this . platformLocation . onHashChange ( handler ) ; } onPopState ( handlerArg ) { const handler = ( event ) => this . popState ( fnArg ( handler , SerializerTypes . STORE_OBJECT ) , fnArg ( event ) ) ; this . store . allocateNode ( handler , handlerArg . value ) ; this . platformLocation . onPopState ( handler ) ; } popState ( handlerArg , eventArg ) { this . callServerLocation ( 'popState' , ... arguments ) ; } pushState ( stateArg , titleArg , urlArg ) { const [ state , title , url ] = this . deserialize ( ... arguments ) ; this . platformLocation . pushState ( state , title , url ) ; } replaceState ( stateArg , titleArg , urlArg ) { const [ state , title , url ] = this . deserialize ( ... arguments ) ; this . platformLocation . replaceState ( state , title , url ) ; } private callServerLocation ( method , ... fnArgs ) { const payload = fnArgs . map ( arg => this . serializer . serialize ( arg . value , arg . type ) ) ; this . bus . sendCommand ( command ( 'location' , method , payload ) ) ; } private deserialize ( ... fnArgs ) { return fnArgs . map ( arg => this . serializer . deserialize ( arg . value , arg . type ) ) as [ ... any [ ] ] ; } }