2 Copyright 2020 TATA ELXSI
4 Licensed under the Apache License, Version 2.0 (the 'License');
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file Bread Crumb component.
21 import { Component, Injector, OnInit } from '@angular/core';
22 import { ActivatedRoute, NavigationEnd, Router, RouterEvent, UrlSegment } from '@angular/router';
23 import { TranslateService } from '@ngx-translate/core';
24 import { BREADCRUMBITEM } from 'CommonModel';
25 import { filter, startWith } from 'rxjs/operators';
26 import { isNullOrUndefined } from 'util';
30 * @Component takes BreadcrumbComponent.html as template url
33 selector: 'app-breadcrumb',
34 templateUrl: './BreadcrumbComponent.html',
35 styleUrls: ['./BreadcrumbComponent.scss']
38 /** Exporting a class @exports BreadcrumbComponent */
39 export class BreadcrumbComponent implements OnInit {
40 /** To inject breadCrumb @public */
41 public static readonly ROUTE_DATA_BREADCRUMB: string = 'breadcrumb';
43 /** To inject services @public */
44 public injector: Injector;
46 /** To inject breadCrumb Default icon and url @public */
47 public readonly home: {} = { icon: 'pi pi-th-large', url: '/' };
49 /** To inject breadCrumb Menus @public */
50 public menuItems: BREADCRUMBITEM[];
52 /** Service holds the router information @private */
53 private router: Router;
55 /** Holds teh instance of AuthService class of type AuthService @private */
56 private activatedRoute: ActivatedRoute;
58 /** handle translate service @private */
59 private translateService: TranslateService;
61 constructor(injector: Injector) {
62 this.injector = injector;
63 this.router = this.injector.get(Router);
64 this.activatedRoute = this.injector.get(ActivatedRoute);
65 this.translateService = this.injector.get(TranslateService);
67 /** Lifecyle Hooks the trigger before component is instantiate @public */
68 public ngOnInit(): void {
70 .pipe(filter((event: RouterEvent) => event instanceof NavigationEnd), startWith(undefined))
71 .subscribe(() => this.menuItems = this.createBreadcrumbs(this.activatedRoute.root));
74 /** Generate breadcrumbs from data given the module routes @private */
75 private createBreadcrumbs(route: ActivatedRoute, url: string = '', breadcrumbs: BREADCRUMBITEM[] = []):
77 const children: ActivatedRoute[] = route.children;
78 if (children.length === 0) {
81 for (const child of children) {
82 const routeURL: string = child.snapshot.url.map((segment: UrlSegment) => segment.path).join('/');
83 if (routeURL !== '') {
84 url += `/${routeURL}`;
86 let menuLIst: BREADCRUMBITEM[] = child.snapshot.data[BreadcrumbComponent.ROUTE_DATA_BREADCRUMB];
87 if (!isNullOrUndefined(menuLIst)) {
88 menuLIst = JSON.parse(JSON.stringify(menuLIst));
89 menuLIst.forEach((item: BREADCRUMBITEM) => {
90 if (!isNullOrUndefined(item.title)) {
91 item.title = item.title.replace('{type}', this.checkTitle(item, child.snapshot.params.type));
92 item.title = item.title.replace('{id}', child.snapshot.params.id);
93 item.title = item.title.replace('{project}', localStorage.getItem('project'));
95 if (!isNullOrUndefined(item.url)) {
96 item.url = item.url.replace('{type}', child.snapshot.params.type);
97 item.url = item.url.replace('{id}', child.snapshot.params.id);
99 breadcrumbs.push(item);
102 return this.createBreadcrumbs(child, url, breadcrumbs);
105 /** Check and update title based on type of operations @private */
106 private checkTitle(breadcrumbItem: BREADCRUMBITEM, opertionType: string): string {
107 if (!isNullOrUndefined(breadcrumbItem.url)) {
108 if (breadcrumbItem.url.indexOf('packages') !== -1) {
109 return this.matchPackageTitle(opertionType);
111 if (breadcrumbItem.url.indexOf('instances') !== -1) {
112 return this.matchInstanceTitle(opertionType);
114 return breadcrumbItem.title;
117 /** check and update package title based on package type @private */
118 private matchPackageTitle(opertionType: string): string {
119 if (opertionType === 'ns') {
120 return this.translateService.instant('NSPACKAGES');
121 } else if (opertionType === 'vnf') {
122 return this.translateService.instant('VNFPACKAGES');
124 return this.translateService.instant('PAGE.DASHBOARD.NETSLICETEMPLATE');
127 /** check and update package title based on instance type @private */
128 private matchInstanceTitle(opertionType: string): string {
129 if (opertionType === 'ns') {
130 return this.translateService.instant('NSINSTANCES');
131 } else if (opertionType === 'vnf') {
132 return this.translateService.instant('VNFINSTANCES');
133 } else if (opertionType === 'pdu') {
134 return this.translateService.instant('PDUINSTANCES');
136 return this.translateService.instant('PAGE.DASHBOARD.NETSLICEINSTANCE');