angular-breadcrumb-fyp
Version:
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 12.1.0.
95 lines (89 loc) • 4.49 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, NgModule } from '@angular/core';
import * as i1 from '@angular/router';
import { ActivationEnd } from '@angular/router';
import { Subject } from 'rxjs';
class AngularBreadcrumbFypService {
constructor(router) {
this.router = router;
this.crumbs$ = new Subject();
this.breadcrumbs = [];
this.paths = [];
this.listenToRoutingEvents();
}
listenToRoutingEvents() {
this.router.events.subscribe((e) => {
if (e instanceof ActivationEnd && e.snapshot.component) {
let current = e.snapshot;
this.breadcrumbs = [];
this.paths = [];
this.buildBreadCrumb(current, this.breadcrumbs, this.paths, false);
}
});
}
buildBreadCrumb(current, targetBreadcrumbs, targetPaths, recursively) {
if (!recursively) {
targetBreadcrumbs.unshift({ label: current.data.crumb, url: this.getUrl(current) });
targetPaths.unshift(this.getUrl(current));
}
let parent = current.parent;
if (parent) {
let newBreadcrumb = { label: parent.data.crumb, url: this.getUrl(parent) };
let alreadyExists = targetBreadcrumbs.findIndex(b => b.label == newBreadcrumb.label) != -1;
if (!alreadyExists && newBreadcrumb.label) {
targetBreadcrumbs.unshift(newBreadcrumb);
targetPaths.unshift(this.getUrl(parent));
}
this.buildBreadCrumb(parent, targetBreadcrumbs, targetPaths, true);
}
this.crumbs$.next(this.breadcrumbs);
}
getUrl(current) {
var _a, _b, _c, _d, _e;
let url = '';
if (((_b = (_a = current.routeConfig) === null || _a === void 0 ? void 0 : _a.path) === null || _b === void 0 ? void 0 : _b.length) === 0 && ((_d = (_c = current.parent) === null || _c === void 0 ? void 0 : _c.routeConfig) === null || _d === void 0 ? void 0 : _d.path)) {
url = current.parent.routeConfig.path;
}
else if ((_e = current.routeConfig) === null || _e === void 0 ? void 0 : _e.path) {
url = current.routeConfig.path;
}
return url;
}
navigateBreadCrumb(crumb) {
let crumbPath = crumb.url;
let paths = this.paths;
let index = paths.indexOf(crumbPath);
let destinationPath = paths.slice(0, (index + 1)).join('/');
this.router.navigate([destinationPath]);
}
}
AngularBreadcrumbFypService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: AngularBreadcrumbFypService, deps: [{ token: i1.Router }], target: i0.ɵɵFactoryTarget.Injectable });
AngularBreadcrumbFypService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: AngularBreadcrumbFypService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: AngularBreadcrumbFypService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () { return [{ type: i1.Router }]; } });
class AngularBreadcrumbFypModule {
}
AngularBreadcrumbFypModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: AngularBreadcrumbFypModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
AngularBreadcrumbFypModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: AngularBreadcrumbFypModule });
AngularBreadcrumbFypModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: AngularBreadcrumbFypModule, providers: [], imports: [[]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: AngularBreadcrumbFypModule, decorators: [{
type: NgModule,
args: [{
declarations: [],
imports: [],
exports: [],
providers: []
}]
}] });
/*
* Public API Surface of angular-breadcrumb-fyp
*/
/**
* Generated bundle index. Do not edit.
*/
export { AngularBreadcrumbFypModule, AngularBreadcrumbFypService };
//# sourceMappingURL=angular-breadcrumb-fyp.js.map