All files / breadcrumb-router si-breadcrumb-default-resolver.service.ts

84.61% Statements 44/52
64% Branches 16/25
83.33% Functions 10/12
84% Lines 42/50

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                        1x 7x             8x 2x   6x         3x 3x 3x   3x                   6x 6x 6x 18x 1x 17x 11x 11x   11x 6x   5x   11x   18x   6x       27x   27x 16x   11x   27x 27x 19x   27x 27x       11x 11x       11x       14x       3x                   17x 17x       17x       17x               17x                    
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { inject, Injectable, LOCALE_ID } from '@angular/core';
import { ActivatedRouteSnapshot } from '@angular/router';
import { BreadcrumbItem } from '@siemens/element-ng/breadcrumb';
import { Observable } from 'rxjs';
 
import { BreadcrumbRouterLink, SiBreadcrumbResolverService } from './si-breadcrumb-router.model';
 
@Injectable({ providedIn: 'root' })
export class SiBreadcrumbDefaultResolverService implements SiBreadcrumbResolverService {
  private locale = inject(LOCALE_ID).toString();
 
  /**
   * Method which resolves the route and creates the breadcrumb items from it.
   * Is called by the `si-breadcrumb-router-component` but can also be called manually in inheritance patterns.
   */
  resolve(route: ActivatedRouteSnapshot): BreadcrumbItem[] | Observable<BreadcrumbItem[]> {
    if (route.data.siBreadcrumb) {
      return this.resolveCustomRoutePart(route)!;
    } else {
      return this.resolveDefault(route);
    }
  }
 
  private resolveCustomRoutePart(route: ActivatedRouteSnapshot): BreadcrumbItem[] | undefined {
    if (route.data.siBreadcrumb) {
      const rawLinks = route.data.siBreadcrumb as BreadcrumbRouterLink[];
      return rawLinks.map(
        rl =>
          ({
            title: this.calculateName(route, rl.title),
            link: rl.link ? this.calculateUrl(route, rl.link) : route.fragment!
          }) as BreadcrumbItem
      );
    }
    return;
  }
 
  private resolveDefault(route: ActivatedRouteSnapshot | null): BreadcrumbItem[] {
    const links: BreadcrumbItem[] = [];
    let currRoute = route;
    while (currRoute != null) {
      if (currRoute.data.siBreadcrumb) {
        links.unshift(...this.resolveCustomRoutePart(currRoute)!);
      } else if (currRoute.url.length > 0) {
        const routeUrl: string = this.getUrl(currRoute);
        const routeName: string = this.getName(currRoute);
        let link: BreadcrumbItem;
        if (links.length === 0) {
          link = { title: routeName } as BreadcrumbItem;
        } else {
          link = { title: routeName, link: routeUrl } as BreadcrumbItem;
        }
        links.unshift(link);
      }
      currRoute = currRoute.parent;
    }
    return links;
  }
 
  private getUrl(route: ActivatedRouteSnapshot): string {
    const parent = route.parent;
    let url: string;
    if (parent != null) {
      url = this.getUrl(parent);
    } else {
      url = '';
    }
    const currentRouteUrl = route.url.map(o => o.toString()).join('/');
    if (!url.endsWith('/')) {
      url = url + '/';
    }
    url = url + currentRouteUrl;
    return url;
  }
 
  private getName(route: ActivatedRouteSnapshot): string {
    let name: string = route.data.title ?? route.url[0].path;
    Iif (typeof name === 'object') {
      name = name[this.locale];
    }
 
    return this.calculateName(route, name);
  }
 
  private calculateName(route: ActivatedRouteSnapshot, baseName: string): string {
    return this.calculate(route, baseName, /{(\w+)}/g);
  }
 
  private calculateUrl(route: ActivatedRouteSnapshot, baseName: string): string {
    return this.calculate(route, baseName, /:(\w+)\/?/g, ':', '');
  }
 
  private calculate(
    route: ActivatedRouteSnapshot,
    base: string,
    pattern: RegExp,
    replaceStart = '{',
    replaceEnd = '}'
  ): string {
    let name = base;
    const values: string[] = [];
 
    let finding: RegExpExecArray | null;
    // tslint:disable-next-line:no-conditional-assignment
    while ((finding = pattern.exec(name)) != null) {
      values.push(finding[1]);
    }
 
    values.forEach(value => {
      let replace = this.findParam(route, value);
      Iif (route.data.replaceValues) {
        replace = route.data.replaceValues[replace] ?? replace;
      }
      name = name.replace(`${replaceStart}${value}${replaceEnd}`, replace);
    });
 
    return name;
  }
 
  private findParam(route: ActivatedRouteSnapshot, paramKey: string): string {
    return (
      route.paramMap.get(paramKey) ??
      (route.parent ? this.findParam(route.parent, paramKey) : paramKey)
    );
  }
}