All files / src/app/core/navigation/breadcrumbs breadcrumbs.component.ts

98.25% Statements 56/57
88.46% Branches 23/26
100% Functions 14/14
98.08% Lines 51/52

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                                                5x 5x   5x 5x   5x             5x 7x   7x   7x 7x 63x   5x   5x   12x 13x     5x 5x       5x         5x 7x     5x     12x   12x 7x   7x 3x   4x     7x 7x   5x     12x 7x     12x     5x 5x 5x 13x 13x 1x 1x 1x     13x   5x     5x 8x     5x 12x 4x     8x       8x   5x  
/*
The MIT License
 
Copyright (c) 2017 (null) McNull https://github.com/McNull
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */
 
import { Component, Injector, OnDestroy } from '@angular/core';
import { ActivatedRouteSnapshot, NavigationEnd, Router } from '@angular/router';
 
import { from, Observable, of, Subscription } from 'rxjs';
import { concat, distinct, filter, first, flatMap, toArray } from 'rxjs/operators';
 
import { BreadcrumbsResolver, IBreadcrumb } from '../../../shared/models/breadcrumbs';
 
@Component({
  selector: 'cd-breadcrumbs',
  template: require('./breadcrumbs.component.html'),
  styles: []
})
export class BreadcrumbsComponent implements OnDestroy {
  crumbs: IBreadcrumb[] = [];
  subscription: Subscription;
  private defaultResolver = new BreadcrumbsResolver();
 
  constructor(private router: Router, private injector: Injector) {
    this.subscription = this.router.events
      .pipe(filter((x) => x instanceof NavigationEnd))
      .subscribe(() => {
        const currentRoot = router.routerState.snapshot.root;
 
        this._resolveCrumbs(currentRoot)
          .pipe(
            flatMap((x) => x),
            distinct((x) => x.text),
            toArray(),
            flatMap((x) => {
              const y = this.postProcess(x);
              return this.wrapIntoObservable<IBreadcrumb[]>(y).pipe(first());
            })
          )
          .subscribe((x) => {
            this.crumbs = x;
          });
      });
  }
 
  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }
 
  private _resolveCrumbs(route: ActivatedRouteSnapshot): Observable<IBreadcrumb[]> {
    let crumbs$: Observable<IBreadcrumb[]>;
 
    const data = route.routeConfig && route.routeConfig.data;
 
    if (data && data.breadcrumbs) {
      let resolver: BreadcrumbsResolver;
 
      if (data.breadcrumbs.prototype instanceof BreadcrumbsResolver) {
        resolver = this.injector.get(data.breadcrumbs);
      } else {
        resolver = this.defaultResolver;
      }
 
      const result = resolver.resolve(route);
      crumbs$ = this.wrapIntoObservable<IBreadcrumb[]>(result).pipe(first());
    } else {
      crumbs$ = of([]);
    }
 
    if (route.firstChild) {
      crumbs$ = crumbs$.pipe(concat(this._resolveCrumbs(route.firstChild)));
    }
 
    return crumbs$;
  }
 
  postProcess(breadcrumbs: IBreadcrumb[]) {
    const result = [];
    breadcrumbs.forEach((element) => {
      const split = element.text.split('/');
      if (split.length > 1) {
        element.text = split[split.length - 1];
        for (let i = 0; i < split.length - 1; i++) {
          result.push({ text: split[i], path: null });
        }
      }
      result.push(element);
    });
    return result;
  }
 
  isPromise(value: any): boolean {
    return value && typeof value.then === 'function';
  }
 
  wrapIntoObservable<T>(value: T | Promise<T> | Observable<T>): Observable<T> {
    if (value instanceof Observable) {
      return value;
    }
 
    Iif (this.isPromise(value)) {
      return from(Promise.resolve(value));
    }
 
    return of(value as T);
  }
}