From 8d7aa025a31936d4d21269a8b67365016c53bba4 Mon Sep 17 00:00:00 2001 From: illfixit <66363651+illfixit@users.noreply.github.com> Date: Fri, 20 Sep 2024 16:39:46 +0200 Subject: [PATCH] chore: undelete title utils for header --- src/app/core/services/title-utils.service.ts | 52 +++++++++++++++++++ .../connector-ui/connector-ui.component.html | 3 +- .../connector-ui/connector-ui.component.ts | 3 ++ src/app/shared/shared.module.ts | 2 + 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/app/core/services/title-utils.service.ts diff --git a/src/app/core/services/title-utils.service.ts b/src/app/core/services/title-utils.service.ts new file mode 100644 index 000000000..ef372f793 --- /dev/null +++ b/src/app/core/services/title-utils.service.ts @@ -0,0 +1,52 @@ +import {Injectable} from '@angular/core'; +import {Title} from '@angular/platform-browser'; +import { + ActivatedRoute, + ActivatedRouteSnapshot, + NavigationEnd, + Router, +} from '@angular/router'; +import {concat, of} from 'rxjs'; +import {filter, map, shareReplay} from 'rxjs/operators'; + +@Injectable() +export class TitleUtilsService { + routeData$ = this.routeDone$().pipe( + map(() => this.getRouteDataRecursively()), + shareReplay(1), + ); + + title$ = this.routeData$.pipe(map((data) => data.title)); + + constructor( + private router: Router, + private titleService: Title, + private activatedRoute: ActivatedRoute, + ) {} + + startUpdatingTitleFromRouteData(defaultTitle: string) { + this.title$.subscribe((title) => { + let fullTitle = title ?? defaultTitle; + this.titleService.setTitle(fullTitle); + }); + } + + private routeDone$() { + return concat( + of({}), + this.router.events.pipe( + filter((event) => event instanceof NavigationEnd), + ), + ); + } + + private getRouteDataRecursively(): any { + let snapshot: ActivatedRouteSnapshot | null = this.activatedRoute.snapshot; + let data = {}; + while (snapshot) { + data = {...data, ...snapshot.data}; + snapshot = snapshot.firstChild; + } + return data; + } +} diff --git a/src/app/routes/connector-ui/connector-ui.component.html b/src/app/routes/connector-ui/connector-ui.component.html index 756cf3f94..0e22dd892 100644 --- a/src/app/routes/connector-ui/connector-ui.component.html +++ b/src/app/routes/connector-ui/connector-ui.component.html @@ -3,6 +3,7 @@ class="sidenav" #drawer fixedInViewport + [disableClose]="true" [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'" [mode]="(isHandset$ | async) ? 'over' : 'side'" [opened]="(isHandset$ | async) === false"> @@ -64,7 +65,7 @@ - +