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 { isNullOrUndefined } from 'util';
22 import { Component, Injector, OnInit } from '@angular/core';
23 import { Title } from '@angular/platform-browser';
24 import { ActivatedRoute, NavigationEnd, Router, RouterEvent, UrlSegment } from '@angular/router';
25 import { TranslateService } from '@ngx-translate/core';
26 import { BREADCRUMBITEM } from 'CommonModel';
27 import { filter, startWith } from 'rxjs/operators';
31 * @Component takes BreadcrumbComponent.html as template url
34 selector: 'app-breadcrumb',
35 templateUrl: './BreadcrumbComponent.html',
36 styleUrls: ['./BreadcrumbComponent.scss']
39 /** Exporting a class @exports BreadcrumbComponent */
40 export class BreadcrumbComponent implements OnInit {
41 /** To inject breadCrumb @public */
42 public static readonly ROUTE_DATA_BREADCRUMB: string = 'breadcrumb';
44 /** To inject services @public */
45 public injector: Injector;
47 /** To inject breadCrumb Default icon and url @public */
48 public readonly home: {} = { icon: 'pi pi-th-large', url: '/' };
50 /** To inject breadCrumb Menus @public */
51 public menuItems: BREADCRUMBITEM[];
53 /** Service holds the router information @private */
54 private router: Router;
56 /** Holds teh instance of AuthService class of type AuthService @private */
57 private activatedRoute: ActivatedRoute;
59 /** handle translate service @private */
60 private translateService: TranslateService;
62 /** Title Service in create ticket page */
63 private titleService: Title;
65 constructor(injector: Injector) {
66 this.injector = injector;
67 this.router = this.injector.get(Router);
68 this.activatedRoute = this.injector.get(ActivatedRoute);
69 this.translateService = this.injector.get(TranslateService);
70 this.titleService = this.injector.get(Title);
72 /** Lifecyle Hooks the trigger before component is instantiate @public */
73 public ngOnInit(): void {
75 // eslint-disable-next-line deprecation/deprecation
76 .pipe(filter((event: RouterEvent) => event instanceof NavigationEnd), startWith(undefined))
77 .subscribe(() => this.menuItems = this.createBreadcrumbs(this.activatedRoute.root));
79 /** To set the title if the page @private */
80 public setTitle(newTitle: string): void {
81 this.titleService.setTitle(newTitle);
83 /** Generate breadcrumbs from data given the module routes @private */
84 private createBreadcrumbs(route: ActivatedRoute, url: string = '', breadcrumbs: BREADCRUMBITEM[] = []):
86 const children: ActivatedRoute[] = route.children;
87 if (children.length === 0) {
90 for (const child of children) {
91 const routeURL: string = child.snapshot.url.map((segment: UrlSegment) => segment.path).join('/');
92 if (routeURL !== '') {
93 url += `/${routeURL}`;
95 let menuLIst: BREADCRUMBITEM[] = child.snapshot.data[BreadcrumbComponent.ROUTE_DATA_BREADCRUMB];
96 if (!isNullOrUndefined(menuLIst)) {
97 menuLIst = JSON.parse(JSON.stringify(menuLIst));
98 menuLIst.forEach((item: BREADCRUMBITEM) => {
99 if (!isNullOrUndefined(item.title)) {
100 item.title = item.title.replace('{type}', this.checkTitle(item, child.snapshot.params.type));
101 item.title = item.title.replace('{id}', child.snapshot.params.id);
102 item.title = item.title.replace('{project}', sessionStorage.getItem('project'));
104 if (!isNullOrUndefined(item.url)) {
105 item.url = item.url.replace('{type}', child.snapshot.params.type);
106 item.url = item.url.replace('{id}', child.snapshot.params.id);
108 breadcrumbs.push(item);
111 this.setTitleforApplication(breadcrumbs);
112 return this.createBreadcrumbs(child, url, breadcrumbs);
115 /** Generate title from data given the module routes @private */
116 private setTitleforApplication(breadcrumbs: BREADCRUMBITEM[]): void {
117 let addTitle: string = 'Open Source MANO';
118 breadcrumbs.forEach((data: BREADCRUMBITEM): void => {
119 addTitle += ' | ' + this.translateService.instant(data.title);
121 this.setTitle(addTitle);
123 /** Check and update title based on type of operations @private */
124 private checkTitle(breadcrumbItem: BREADCRUMBITEM, opertionType: string): string {
125 if (!isNullOrUndefined(breadcrumbItem.url)) {
126 if (breadcrumbItem.url.indexOf('packages') !== -1) {
127 return this.matchPackageTitle(opertionType);
129 if (breadcrumbItem.url.indexOf('instances') !== -1) {
130 return this.matchInstanceTitle(opertionType);
132 return breadcrumbItem.title;
135 /** check and update package title based on package type @private */
136 private matchPackageTitle(opertionType: string): string {
137 if (opertionType === 'ns') {
138 return this.translateService.instant('NSPACKAGES');
139 } else if (opertionType === 'vnf') {
140 return this.translateService.instant('VNFPACKAGES');
142 return this.translateService.instant('PAGE.DASHBOARD.NETSLICETEMPLATE');
145 /** check and update package title based on instance type @private */
146 private matchInstanceTitle(opertionType: string): string {
147 if (opertionType === 'ns') {
148 return this.translateService.instant('NSINSTANCES');
149 } else if (opertionType === 'vnf') {
150 return this.translateService.instant('VNFINSTANCES');
151 } else if (opertionType === 'pdu') {
152 return this.translateService.instant('PDUINSTANCES');
154 return this.translateService.instant('PAGE.DASHBOARD.NETSLICEINSTANCE');