1
|
import { Component, OnInit } from '@angular/core';
|
2
|
import { Router } from '@angular/router';
|
3
|
import { MenuItem } from 'primeng/api';
|
4
|
|
5
|
@Component({
|
6
|
selector: 'app-breadcrumb',
|
7
|
templateUrl: './breadcrumb.component.html',
|
8
|
styleUrls: ['./breadcrumb.component.scss']
|
9
|
})
|
10
|
export class BreadcrumbComponent implements OnInit {
|
11
|
items: MenuItem[] = [];
|
12
|
home: MenuItem;
|
13
|
showBreadcrumb: boolean = false;
|
14
|
|
15
|
constructor(private router: Router) { }
|
16
|
|
17
|
ngOnInit(): void {
|
18
|
|
19
|
let url = this.router.url;
|
20
|
let chunks = url.split('/').filter(x => x !== '' && x !== 'pages');
|
21
|
|
22
|
if (chunks.length <= 1) {
|
23
|
this.showBreadcrumb = false;
|
24
|
}
|
25
|
else {
|
26
|
let items: MenuItem[] = [];
|
27
|
let home: MenuItem = {};
|
28
|
let parent: string = '/pages/';
|
29
|
let isHome: boolean = true;
|
30
|
|
31
|
chunks.forEach(function (chunk) {
|
32
|
let item: MenuItem = {};
|
33
|
let path: string;
|
34
|
let previousPath: string;
|
35
|
|
36
|
item.label = chunk?.replace(/-/g, " ")?.toUpperCase();
|
37
|
if (parent !== previousPath) {
|
38
|
path = parent + chunk;
|
39
|
previousPath = path + '/';
|
40
|
parent = previousPath;
|
41
|
}
|
42
|
item.routerLink = path;
|
43
|
if (isHome) {
|
44
|
item.icon = 'pi pi-home';
|
45
|
home = item;
|
46
|
isHome = false;
|
47
|
}
|
48
|
else {
|
49
|
items.push(item);
|
50
|
}
|
51
|
|
52
|
});
|
53
|
|
54
|
this.items = items;
|
55
|
this.home = home;
|
56
|
this.showBreadcrumb = true;
|
57
|
}
|
58
|
|
59
|
}
|
60
|
|
61
|
}
|