From c736131b713bdf6b85c4aad90a7e4c2dcdcffa2b Mon Sep 17 00:00:00 2001 From: markuczy Date: Wed, 31 Jul 2024 13:59:03 +0200 Subject: [PATCH 1/2] feat: breadcrumb service manual breadcrumbs as topic --- .../src/lib/services/breadcrumb.service.ts | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts b/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts index c2d17267..a5431191 100644 --- a/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts +++ b/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts @@ -5,16 +5,30 @@ import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy' import { BehaviorSubject, filter } from 'rxjs' import { MenuItem } from 'primeng/api' import { BreadCrumbMenuItem } from '../model/breadcrumb-menu-item.model' +import { SyncableTopic } from '@onecx/accelerator' + +// This topic is defined here and not in integration-interface, because +// it is not used as framework independent integration but for improving +// angular specific things +class ManualBreadcrumbsTopic extends SyncableTopic { + constructor() { + super('manualBreadcrumbs', 1) + } +} @Injectable({ providedIn: 'any' }) @UntilDestroy() export class BreadcrumbService { - private itemsSource = new BehaviorSubject([]) + private itemsSource$ = new ManualBreadcrumbsTopic() generatedItemsSource = new BehaviorSubject([]) - itemsHandler = this.itemsSource.asObservable() + itemsHandler = this.itemsSource$.asObservable() - constructor(private router: Router, private activeRoute: ActivatedRoute, private translateService: TranslateService) { + constructor( + private router: Router, + private activeRoute: ActivatedRoute, + private translateService: TranslateService + ) { this.generateBreadcrumbs(this.activeRoute.snapshot) this.router.events .pipe( @@ -93,7 +107,7 @@ export class BreadcrumbService { ] if (translationKeys.length) { this.translateService.get(translationKeys).subscribe((translations: any) => { - this.itemsSource.next( + this.itemsSource$.publish( items.map((i) => ({ ...i, label: translations[i.labelKey || ''] || i.label, @@ -102,7 +116,7 @@ export class BreadcrumbService { ) }) } else { - this.itemsSource.next(items) + this.itemsSource$.publish(items) } } } From ef0039e72718c771bca8924594a6ce38664a0e12 Mon Sep 17 00:00:00 2001 From: markuczy Date: Wed, 31 Jul 2024 14:48:04 +0200 Subject: [PATCH 2/2] refactor: make breadcrumb topic extendable --- .../src/lib/services/breadcrumb.service.ts | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts b/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts index a5431191..57cb30c7 100644 --- a/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts +++ b/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts @@ -2,15 +2,19 @@ import { Injectable } from '@angular/core' import { ActivatedRoute, ActivatedRouteSnapshot, Data, NavigationEnd, ParamMap, Router } from '@angular/router' import { TranslateService } from '@ngx-translate/core' import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy' -import { BehaviorSubject, filter } from 'rxjs' +import { BehaviorSubject, filter, map } from 'rxjs' import { MenuItem } from 'primeng/api' import { BreadCrumbMenuItem } from '../model/breadcrumb-menu-item.model' import { SyncableTopic } from '@onecx/accelerator' +interface ManualBreadcrumbs { + menuItems: MenuItem[] +} + // This topic is defined here and not in integration-interface, because // it is not used as framework independent integration but for improving // angular specific things -class ManualBreadcrumbsTopic extends SyncableTopic { +class ManualBreadcrumbsTopic extends SyncableTopic { constructor() { super('manualBreadcrumbs', 1) } @@ -22,7 +26,7 @@ export class BreadcrumbService { private itemsSource$ = new ManualBreadcrumbsTopic() generatedItemsSource = new BehaviorSubject([]) - itemsHandler = this.itemsSource$.asObservable() + itemsHandler = this.itemsSource$.pipe(map((manualBreadcrumbs) => manualBreadcrumbs.menuItems)) constructor( private router: Router, @@ -107,16 +111,18 @@ export class BreadcrumbService { ] if (translationKeys.length) { this.translateService.get(translationKeys).subscribe((translations: any) => { - this.itemsSource$.publish( - items.map((i) => ({ + this.itemsSource$.publish({ + menuItems: items.map((i) => ({ ...i, label: translations[i.labelKey || ''] || i.label, title: translations[i.titleKey || ''] || i.title, - })) - ) + })), + }) }) } else { - this.itemsSource$.publish(items) + this.itemsSource$.publish({ + menuItems: items, + }) } } }