matrix-dimension/web/app/page-header/page-header.component.ts

53 lines
2.2 KiB
TypeScript
Raw Normal View History

2017-12-15 07:33:32 +00:00
import { Component } from "@angular/core";
2021-12-01 02:27:37 +00:00
import { ActivatedRoute, NavigationEnd, PRIMARY_OUTLET, Router, } from "@angular/router";
import { TranslateService } from "@ngx-translate/core";
import { filter } from "rxjs/operators";
@Component({
2021-09-01 23:29:24 +00:00
selector: "my-page-header",
templateUrl: "./page-header.component.html",
styleUrls: ["./page-header.component.scss"],
})
export class PageHeaderComponent {
2017-12-15 07:33:32 +00:00
public pageName: string;
2021-12-03 07:33:53 +00:00
public isHome = true;
2017-12-15 07:33:32 +00:00
2021-09-01 23:01:01 +00:00
constructor(
private router: Router,
private activatedRoute: ActivatedRoute,
public translate: TranslateService
) {
this.router.events
.pipe(filter((ev) => ev instanceof NavigationEnd))
.subscribe((ev) => {
let currentRoute = this.activatedRoute.root;
let url = "";
const event = ev as NavigationEnd;
2021-12-03 07:33:53 +00:00
let routeCount = 0;
2017-12-15 07:33:32 +00:00
2021-09-01 23:01:01 +00:00
while (currentRoute.children.length > 0) {
const children = currentRoute.children;
children.forEach((route) => {
2021-12-03 07:33:53 +00:00
const breadcrumb = route.snapshot.data["breadcrumb"];
if (typeof(breadcrumb) === 'string' && !route.snapshot.data['#translated']) {
this.translate.get(breadcrumb).subscribe((res: string) => {
route.snapshot.data["breadcrumb"] = res;
route.snapshot.data['#translated'] = true;
});
2021-09-01 23:01:01 +00:00
}
currentRoute = route;
2021-12-03 07:33:53 +00:00
routeCount++;
2021-09-01 23:01:01 +00:00
url += "/" + route.snapshot.url.map((s) => s.path).join("/");
if (route.outlet !== PRIMARY_OUTLET) return;
if (!route.routeConfig || !route.routeConfig.data) return;
2021-12-03 07:33:53 +00:00
if (url === event.urlAfterRedirects.split("?")[0]) {
2021-09-01 23:01:01 +00:00
this.pageName = route.snapshot.data.name;
2021-12-03 07:33:53 +00:00
}
2021-09-01 23:01:01 +00:00
});
}
2021-12-03 07:33:53 +00:00
this.isHome = routeCount <= 2; // Path of `./riot-app` (2 segments)
2021-09-01 23:01:01 +00:00
});
2017-12-15 07:33:32 +00:00
}
}