import { Injectable , Type } from '@angular/core' ; import { ExtensionService } from '@alfresco/adf-extensions' ; import { ExtensionRoute } from '../models/types' ; import { Router } from '@angular/router' ; @ Injectable ( { providedIn : 'root' } ) export class RouterExtensionService { defaults = { layout : 'app.layout.main' , auth : [ 'app.auth' ] } ; constructor ( private router , protected extensions ) { } mapExtensionRoutes ( ) { const routesWithoutParent = [ ] ; this . getApplicationRoutes ( ) . forEach ( ( extensionRoute ) => { if ( this . extensionRouteHasChild ( extensionRoute ) ) { const parentRoute = this . findRoute ( extensionRoute . parentRoute ) ; if ( parentRoute ) { this . convertExtensionRouteToRoute ( extensionRoute ) ; parentRoute . children . unshift ( extensionRoute ) ; } } else { routesWithoutParent . push ( extensionRoute ) ; } } ) ; this . router . config . unshift ( ... routesWithoutParent ) ; } public getApplicationRoutes ( ) { return this . extensions . routes . map ( ( route ) => { const guards = this . extensions . getAuthGuards ( route . auth && route . auth . length > 0 ? route . auth : this . defaults . auth ) ; return { path : route . path , component : this . getComponentById ( route . layout || this . defaults . layout ) , canActivateChild : guards , canActivate : guards , parentRoute : route . parentRoute , children : [ ... ( route [ 'children' ] ? route [ 'children' ] . map ( ( { path , component , outlet , data } ) => { return { path , outlet , data , component : this . getComponentById ( component ) } ; } ) : [ ] ) , { path : '' , component : this . getComponentById ( route . component ) , data : route . data } ] } ; } ) ; } private getComponentById ( id ) { return this . extensions . getComponentById ( id ) ; } private extensionRouteHasChild ( route ) { return route . parentRoute !== undefined ; } private convertExtensionRouteToRoute ( extensionRoute ) { delete extensionRoute . parentRoute ; delete extensionRoute . component ; } private findRoute ( parentRoute ) { const routeIndex = this . router . config . findIndex ( ( route ) => route . path === parentRoute ) ; return this . router . config [ routeIndex ] ; } }