import { ActivatedRouteSnapshot, Resolve, UrlSegment } from '@angular/router';
import { Observable, of } from 'rxjs';
export class BreadcrumbsResolver implements Resolve<IBreadcrumb[]> {
public resolve(
route: ActivatedRouteSnapshot
): Observable<IBreadcrumb[]> | Promise<IBreadcrumb[]> | IBreadcrumb[] {
const data = route.routeConfig.data;
const path = data.path === null ? null : this.getFullPath(route);
const text =
typeof data.breadcrumbs === 'string'
? data.breadcrumbs
: data.breadcrumbs.text || data.text || path;
const crumbs: IBreadcrumb[] = [{ text: text, path: path }];
return of(crumbs);
}
public getFullPath(route: ActivatedRouteSnapshot): string {
const relativePath = (segments: UrlSegment[]) =>
segments.reduce((a, v) => (a += '/' + v.path), '');
const fullPath = (routes: ActivatedRouteSnapshot[]) =>
routes.reduce((a, v) => (a += relativePath(v.url)), '');
return fullPath(route.pathFromRoot);
}
}
export interface IBreadcrumb {
text: string;
path: string;
}