From f936ffc4c8784c1a221a45ca1e5c11d4f40a7562 Mon Sep 17 00:00:00 2001 From: Philipp Pracht Date: Tue, 19 Jan 2021 11:27:37 +0100 Subject: [PATCH] ng support lib enhancements (#1809) Co-authored-by: JohannesDoberer --- .../component/luigi.preload.component.html | 25 +------ .../lib/component/luigi.preload.component.ts | 4 -- .../src/lib/luigi.angular.support.module.ts | 3 +- .../src/lib/route/luigi-route-strategy.ts | 19 ++++-- .../lib/service/luigi-auto-routing.service.ts | 68 ++++++++++++++----- .../src/lib/service/luigi-context-service.ts | 7 +- .../lib/service/luigi-context.service.impl.ts | 18 +++++ .../client-support-angular/src/public-api.ts | 1 + 8 files changed, 91 insertions(+), 54 deletions(-) diff --git a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/component/luigi.preload.component.html b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/component/luigi.preload.component.html index 68ea9c6a7a..6c5db10c58 100644 --- a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/component/luigi.preload.component.html +++ b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/component/luigi.preload.component.html @@ -1,24 +1 @@ -

NG Luigi Library Demo Component

- -

- This is a demo component; in Luigi it is a - virtual tree. -
- If you want to configure an Angular Component to be called in virtual tree, - you must use this route configuration: -
-
- {{ routeExampleVirtual }} -

-
- -

- If you want to configure an Angular Component to be just created once (like a - singleton( you can use this configuration: -
-
- {{ routeExampleReuse }} -

+

diff --git a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/component/luigi.preload.component.ts b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/component/luigi.preload.component.ts index a1ecf62d92..9c75253be1 100644 --- a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/component/luigi.preload.component.ts +++ b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/component/luigi.preload.component.ts @@ -7,9 +7,5 @@ import { Component, OnInit } from '@angular/core'; }) export class LuigiPreloadComponent implements OnInit { constructor() {} - routeExampleVirtual: string = - " {path: 'ng-luigi-demo', component: NgLuigiDemoComponent, data: {fromVirtualTreeRoot: true}}"; - routeExampleReuse: string = - " {path: 'ng-luigi-demo', component: NgLuigiDemoComponent, data: {reuse: true}}"; ngOnInit(): void {} } diff --git a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/luigi.angular.support.module.ts b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/luigi.angular.support.module.ts index 6003776636..db327ea3e8 100644 --- a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/luigi.angular.support.module.ts +++ b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/luigi.angular.support.module.ts @@ -4,8 +4,7 @@ import { LuigiPreloadComponent } from './component/luigi.preload.component'; import { LuigiContextService } from './service/luigi-context-service'; import { LuigiContextServiceImpl } from './service/luigi-context.service.impl'; import { LuigiAutoRoutingService } from './service/luigi-auto-routing.service'; -import {LuigiReuseRouteStrategy} from "./route/luigi-reuse-route-strategy.ts"; -import {LuigiRouteStrategy} from "./route/luigi-route-strategy"; +import { LuigiRouteStrategy } from './route/luigi-route-strategy'; export const staticRoutes: Routes = [ /** here an example if you want to specify that this component is a virtualTree element in Luigi Core navigation*/ diff --git a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/route/luigi-route-strategy.ts b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/route/luigi-route-strategy.ts index 50a0fd1117..f10cd3a163 100644 --- a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/route/luigi-route-strategy.ts +++ b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/route/luigi-route-strategy.ts @@ -1,10 +1,13 @@ -import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from "@angular/router"; -import { LuigiActivatedRouteSnapshotHelper } from "./luigi-activated-route-snapshot-helper"; +import { + ActivatedRouteSnapshot, + DetachedRouteHandle, + RouteReuseStrategy +} from '@angular/router'; +import { LuigiActivatedRouteSnapshotHelper } from './luigi-activated-route-snapshot-helper'; export class LuigiRouteStrategy implements RouteReuseStrategy { - shouldDetach(route: ActivatedRouteSnapshot): boolean { - return false; + return false; } store(route: ActivatedRouteSnapshot, handler: DetachedRouteHandle): void {} @@ -18,8 +21,10 @@ export class LuigiRouteStrategy implements RouteReuseStrategy { return (null as unknown) as DetachedRouteHandle; } - shouldReuseRoute(future: ActivatedRouteSnapshot, current: ActivatedRouteSnapshot): boolean { - return future.routeConfig === current.routeConfig; + shouldReuseRoute( + future: ActivatedRouteSnapshot, + current: ActivatedRouteSnapshot + ): boolean { + return false; } - } diff --git a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-auto-routing.service.ts b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-auto-routing.service.ts index 4f7887e75b..ac312464d8 100644 --- a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-auto-routing.service.ts +++ b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-auto-routing.service.ts @@ -1,9 +1,16 @@ import { Injectable, OnDestroy } from '@angular/core'; import { OperatorFunction, PartialObserver, Subscription } from 'rxjs'; -import { NavigationEnd, Router, RouterEvent } from '@angular/router'; +import { + convertToParamMap, + NavigationEnd, + ParamMap, + Router, + RouterEvent +} from '@angular/router'; import { linkManager } from '@luigi-project/client'; import { filter } from 'rxjs/operators'; import { LuigiActivatedRouteSnapshotHelper } from '../route/luigi-activated-route-snapshot-helper'; +import { LuigiContextService } from './luigi-context-service'; @Injectable({ providedIn: 'root' @@ -11,11 +18,14 @@ import { LuigiActivatedRouteSnapshotHelper } from '../route/luigi-activated-rout export class LuigiAutoRoutingService implements OnDestroy { private subscription: Subscription = new Subscription(); - constructor(private router: Router) { + constructor( + private router: Router, + private luigiContextService: LuigiContextService + ) { this.subscription.add( this.router.events .pipe(this.doFilter()) - .subscribe(this.doSubscription as () => void) + .subscribe(this.doSubscription.bind(this) as () => void) ); } @@ -37,22 +47,48 @@ export class LuigiAutoRoutingService implements OnDestroy { * Another option is to specify the LuigiPath: if you add in route data luigiRoute:'/xxxx/xxx'; * in the case we will update the path in LuigiCore navigation, here an example * {path: 'demo', component: DemoComponent, data:{luigiRoute: '/home/demo''}} - * @param event + * @param event the NavigationEnd event */ doSubscription(event: NavigationEnd): void { const current = LuigiActivatedRouteSnapshotHelper.getCurrent(); - if (current.data.luigiRoute) { - linkManager() - .withoutSync() - .navigate(current.data.luigiRoute); - return; - } - if (current.data.fromVirtualTreeRoot) { - console.debug('Calling fromVirtualTreeRoot for ulr ==> ' + event.url); - linkManager() - .fromVirtualTreeRoot() - .withoutSync() - .navigate(event.url); + + if (current.data) { + if (current.data.luigiRoute) { + let route = current.data.luigiRoute; + + if (current.params) { + const pmap: ParamMap = convertToParamMap(current.params); + pmap.keys.forEach(key => { + const val = pmap.getAll(key).forEach(param => { + route = route.replace(':' + key, param); + }); + }); + } + let lm = linkManager(); + if (current.data.fromContext) { + if (!this.luigiContextService.getContext()) { + console.debug( + 'Ignoring auto navigation request, luigi context not set' + ); + return; + } + if (current.data.fromContext === true) { + lm = lm.fromClosestContext(); + } else { + lm = lm.fromContext(current.data.fromContext); + } + } + + lm.withoutSync().navigate(route); + return; + } + if (current.data.fromVirtualTreeRoot) { + console.debug('Calling fromVirtualTreeRoot for ulr ==> ' + event.url); + linkManager() + .fromVirtualTreeRoot() + .withoutSync() + .navigate(event.url); + } } } diff --git a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-context-service.ts b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-context-service.ts index c56acb7c7a..38f16fbdf9 100644 --- a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-context-service.ts +++ b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-context-service.ts @@ -9,9 +9,14 @@ export abstract class LuigiContextService { abstract contextObservable(): Observable; /** - * Get latest context object + * Get latest set context object (can be null/undefined, if not set yet) */ abstract getContext(): Context; + + /** + * Get a promise that resolves when context is set. + */ + abstract getContextAsync(): Promise; } export enum ILuigiContextTypes { diff --git a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-context.service.impl.ts b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-context.service.impl.ts index 9a0d3e1a1d..14034e0bd9 100644 --- a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-context.service.impl.ts +++ b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/lib/service/luigi-context.service.impl.ts @@ -1,5 +1,6 @@ import { Injectable } from '@angular/core'; import { ReplaySubject, Observable } from 'rxjs'; +import { first } from 'rxjs/operators'; import { Context, addInitListener, @@ -40,6 +41,23 @@ export class LuigiContextServiceImpl implements LuigiContextService { return this.currentContext && this.currentContext.context; } + /** + * Get a promise that resolves when context is set. + */ + public getContextAsync(): Promise { + return new Promise((resolve, reject) => { + if (this.getContext()) { + resolve(this.getContext()); + } else { + this.contextObservable() + .pipe(first()) + .subscribe(ctx => { + resolve(ctx.context); + }); + } + }); + } + /** * Set current context */ diff --git a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/public-api.ts b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/public-api.ts index 7a12a649a7..70f835172e 100644 --- a/client-frameworks-support/client-support-angular/projects/client-support-angular/src/public-api.ts +++ b/client-frameworks-support/client-support-angular/projects/client-support-angular/src/public-api.ts @@ -5,4 +5,5 @@ export * from './lib/component/luigi.preload.component'; export * from './lib/luigi.angular.support.module'; export * from './lib/service/luigi-context-service'; +export * from './lib/service/luigi-context.service.impl'; export * from './lib/service/luigi-auto-routing.service';