All files / app/shared/components/breadcrumbs breadcrumbs.service.ts

100% Statements 30/30
100% Branches 1/1
100% Functions 18/18
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 663x 3x 3x 3x                   3x   13x   13x   23x       13x   30x 3x   13x   30x 3x       3x 3x 3x 3x         7x     7x       6x       2x       2x       4x      
import { inject, Injectable, signal, WritableSignal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ActivatedRouteSnapshot, ActivationEnd, ActivationStart, Router, UrlSegment } from '@angular/router';
import { filter } from 'rxjs';
 
export interface Breadcrumb {
  title: string,
  path: string,
}
 
@Injectable({
  providedIn: 'root'
})
export class BreadcrumbsService {
 
  private _router = inject(Router);
 
  private _breadcrumbs : WritableSignal<Breadcrumb[]> = signal([]);
  public get breadcrumbs() {
    return this._breadcrumbs.asReadonly();
  }
 
  constructor() {
    this._router.events.pipe(
      takeUntilDestroyed(),
      filter(event => event instanceof ActivationStart),
    ).subscribe(() => this.reset());
 
    this._router.events.pipe(
      takeUntilDestroyed(),
      filter(event => event instanceof ActivationEnd),
    ).subscribe(event => this._addRouterBreadcrumb(event));
  }
 
  private _addRouterBreadcrumb(event: ActivationEnd) {
    if (event.snapshot.title) {
      const title = event.snapshot.title;
      const breadcrumb = {title: title, path: this._getPath(event.snapshot.pathFromRoot)};
      this._breadcrumbs.update(value => [breadcrumb, ...value]);
    }
  }
 
  private _getPath(route: ActivatedRouteSnapshot[]) {
    return route.reduce((acc, current) => acc + this._getUrl(current.url), '');
  }
  private _getUrl(path: UrlSegment[]) {
    return path.reduce((acc, current) => acc + '/' + current.toString(), '');
  }
 
  public addBreadcrumb(breadcrumb: Breadcrumb) {
    this._breadcrumbs.update(value => [...value, breadcrumb]);
  }
 
  public removeBreadcrumb() {
    this._breadcrumbs.update(value => value.slice(0, value.length -1));
  }
 
  public setBreadcrumbs(breadcrumbs: Breadcrumb[]) {
    this._breadcrumbs.set(breadcrumbs);
  }
 
  public reset() {
    this._breadcrumbs.set([]);
  }
}