import { Injectable , Component } from '@angular/core' ; import { Location } from '@angular/common' ; import { Router , ActivationEnd , NavigationEnd , ActivatedRoute , ActivatedRouteSnapshot } from '@angular/router' ; const areEqualArrays = ( a , b ) => ( ( a . length === b . length ) ? a . every ( value => b . includes ( value ) ) : false ) ; const objectKeyValuesToList = ( obj ) : any [ ] => { return Object . keys ( obj ) . map ( key => obj [ key ] ) ; } ; @ Injectable ( { providedIn : 'root' } ) export class RoutesService { constructor ( public location , public routing , private activedRoute ) { let lastActivationEnd = null ; this . routing . events . subscribe ( event => { if ( event instanceof ActivationEnd ) { lastActivationEnd = event ; } if ( event instanceof NavigationEnd ) { const data = this . getPathData ( lastActivationEnd . snapshot ) ; if ( this . previouslastPaths [ 0 ] === data . lastPaths [ 0 ] ) { if ( ! areEqualArrays ( objectKeyValuesToList ( this . previousParams ) , objectKeyValuesToList ( data . params ) ) ) { if ( this . previousPathPostFix !== data . pathPostFix ) { if ( this . previousPathPostFix . length > 0 ) { setTimeout ( ( ) => { this . routing . navigate ( [ `${ decodeURIComponent ( event . url ) }/${ this . previousPathPostFix }` ] ) ; } , 1 ) ; } } } else { this . previousPathPostFix = data . pathPostFix ; } } else { this . previousPathPostFix = data . pathPostFix ; } this . previouslastPaths = data . lastPaths ; this . previousParams = data . params ; } } ) ; } private static singleEncode = true ; private previouslastPaths = [ ] ; private previousParams = { } ; private previousPathPostFix = '' ; public static getClusterViewPath ( ) { return '/' ; } public static getNodesViewPath ( ) { return '/nodes' ; } public static getSystemAppsViewPath ( ) { return '/system/apps' ; } public static getAppsViewPath ( ) { return '/apps' ; } public static getAppTypesViewPath ( ) { return '/appTypes' ; } public static getNodeViewPath ( nodeName ) { return '/node/' + this . doubleEncode ( nodeName ) ; } public static getNetworksViewPath ( ) { return '/networks' ; } public static getNetworkViewPath ( networkName ) { return '/network/' + this . doubleEncode ( networkName ) ; } public static getDeployedAppViewPath ( nodeName , appId ) { return '/node/' + this . doubleEncode ( nodeName ) + '/deployedapp/' + this . doubleEncode ( appId ) ; } public static getDeployedServiceViewPath ( nodeName , appId , serviceId , activationId ) { return '/node/' + this . doubleEncode ( nodeName ) + '/deployedapp/' + this . doubleEncode ( appId ) + '/deployedservice/' + this . doubleEncode ( serviceId ) + ( activationId ? '/activationid/' + this . doubleEncode ( activationId ) : '' ) ; } public static getDeployedReplicasViewPath ( nodeName , appId , serviceId , activationId ) { return '/node/' + this . doubleEncode ( nodeName ) + '/deployedapp/' + this . doubleEncode ( appId ) + '/deployedservice/' + this . doubleEncode ( serviceId ) + ( activationId ? '/activationid/' + this . doubleEncode ( activationId ) : '' ) + '/replicas/' ; } public static getDeployedCodePackagesViewPath ( nodeName , appId , serviceId , activationId ) { return '/node/' + this . doubleEncode ( nodeName ) + '/deployedapp/' + this . doubleEncode ( appId ) + '/deployedservice/' + this . doubleEncode ( serviceId ) + ( activationId ? '/activationid/' + this . doubleEncode ( activationId ) : '' ) + '/codepackages/' ; } public static getDeployedReplicaViewPath ( nodeName , appId , serviceId , activationId , partitionId , replicaId ) { return '/node/' + this . doubleEncode ( nodeName ) + '/deployedapp/' + this . doubleEncode ( appId ) + '/deployedservice/' + this . doubleEncode ( serviceId ) + ( activationId ? '/activationid/' + this . doubleEncode ( activationId ) : '' ) + '/partition/' + this . doubleEncode ( partitionId ) + '/replica/' + this . doubleEncode ( replicaId ) ; } public static getCodePackageViewPath ( nodeName , appId , serviceId , activationId , codePackageName ) { return '/node/' + this . doubleEncode ( nodeName ) + '/deployedapp/' + this . doubleEncode ( appId ) + '/deployedservice/' + this . doubleEncode ( serviceId ) + ( activationId ? '/activationid/' + this . doubleEncode ( activationId ) : '' ) + '/codepackage/' + this . doubleEncode ( codePackageName ) ; } public static getAppTypeViewPath ( appTypeName ) { return '/apptype/' + this . doubleEncode ( appTypeName ) ; } public static getAppViewPath ( appTypeName , appId ) { return '/apptype/' + this . doubleEncode ( appTypeName ) + '/app/' + this . doubleEncode ( appId ) ; } public static getServiceViewPath ( appTypeName , appId , serviceId ) { return '/apptype/' + this . doubleEncode ( appTypeName ) + '/app/' + this . doubleEncode ( appId ) + '/service/' + this . doubleEncode ( serviceId ) ; } public static getPartitionViewPath ( appTypeName , appId , serviceId , partitionId ) { return '/apptype/' + this . doubleEncode ( appTypeName ) + '/app/' + this . doubleEncode ( appId ) + '/service/' + this . doubleEncode ( serviceId ) + '/partition/' + this . doubleEncode ( partitionId ) ; } public static getReplicaViewPath ( appTypeName , appId , serviceId , partitionId , replicaId ) { return '/apptype/' + this . doubleEncode ( appTypeName ) + '/app/' + this . doubleEncode ( appId ) + '/service/' + this . doubleEncode ( serviceId ) + '/partition/' + this . doubleEncode ( partitionId ) + '/replica/' + this . doubleEncode ( replicaId ) ; } public static doubleEncode ( str ) { return RoutesService . singleEncode ? encodeURIComponent ( str ) : encodeURIComponent ( encodeURIComponent ( str ) ) ; } private static forceSingleEncode ( force ) { RoutesService . singleEncode = force ; } getPathData ( snapshot ) { const data = { params : snapshot . params , pathPostFix : '' , lastPaths : [ ] } ; while ( snapshot !== null ) { data . pathPostFix = snapshot . routeConfig . path ; if ( snapshot . component ) { data . lastPaths . push ( snapshot . component ) ; } snapshot = snapshot . firstChild ; } return data ; } public navigate ( pathGetter ) { let path ; try { RoutesService . forceSingleEncode ( true ) ; path = pathGetter ( ) ; } finally { } this . routing . navigate ( [ path ] ) ; } }