import { Injectable } from '@angular/core' ; import { Router , ActivatedRoute , RoutesRecognized } from '@angular/router' ; import { Subscription } from 'rxjs' ; import { filter , pairwise } from 'rxjs/operators' ; @ Injectable ( ) export class ScrollToTopService { private _routerListener : Subscription ; static _ ( router , route ) { return new ScrollToTopService ( router , route ) ; } constructor ( private router , private route ) { } listen ( ) { this . _routerListener = this . router . events . pipe ( filter ( ( e ) => e instanceof RoutesRecognized ) , pairwise ( ) ) . subscribe ( ( events ) => { const disabledPaths = [ 'wallet' ] , previousPath = events [ 0 ] . urlAfterRedirects . split ( '/' ) [ 1 ] , currentPath = events [ 1 ] . urlAfterRedirects . split ( '/' ) [ 1 ] ; const currentPathDisabled = disabledPaths . indexOf ( currentPath ) > - 1 ; const pathChanged = previousPath !== currentPath ; if ( ! currentPathDisabled || ( currentPathDisabled && pathChanged ) ) { window . scrollTo ( 0 , 0 ) ; } } ) ; return this ; } unlisten ( ) { if ( this . _routerListener ) this . _routerListener . unsubscribe ( ) ; return this ; } }