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 { Title } from '@angular/platform-browser';
23 import { ActivatedRoute, NavigationEnd, Router, RouterEvent, UrlSegment } from '@angular/router';
24 import { TranslateService } from '@ngx-translate/core';
25 import { BREADCRUMBITEM } from 'CommonModel';
26 import { filter, startWith } from 'rxjs/operators';
27 import { isNullOrUndefined } from 'util';
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 .pipe(filter((event: RouterEvent) => event instanceof NavigationEnd), startWith(undefined))
76 .subscribe(() => this.menuItems = this.createBreadcrumbs(this.activatedRoute.root));
78 /** To set the title if the page @private */
79 public setTitle(newTitle: string): void {
80 this.titleService.setTitle(newTitle);
82 /** Generate breadcrumbs from data given the module routes @private */
83 private createBreadcrumbs(route: ActivatedRoute, url: string = '', breadcrumbs: BREADCRUMBITEM[] = []):
85 const children: ActivatedRoute[] = route.children;
86 if (children.length === 0) {
89 for (const child of children) {
90 const routeURL: string = child.snapshot.url.map((segment: UrlSegment) => segment.path).join('/');
91 if (routeURL !== '') {
92 url += `/${routeURL}`;
94 let menuLIst: BREADCRUMBITEM[] = child.snapshot.data[BreadcrumbComponent.ROUTE_DATA_BREADCRUMB];
95 if (!isNullOrUndefined(menuLIst)) {
96 menuLIst = JSON.parse(JSON.stringify(menuLIst));
97 menuLIst.forEach((item: BREADCRUMBITEM) => {
98 if (!isNullOrUndefined(item.title)) {
99 item.title = item.title.replace('{type}', this.checkTitle(item, child.snapshot.params.type));
100 item.title = item.title.replace('{id}', child.snapshot.params.id);
101 item.title = item.title.replace('{project}', localStorage.getItem('project'));
103 if (!isNullOrUndefined(item.url)) {
104 item.url = item.url.replace('{type}', child.snapshot.params.type);
105 item.url = item.url.replace('{id}', child.snapshot.params.id);
107 breadcrumbs.push(item);
110 this.setTitleforApplication(breadcrumbs);
111 return this.createBreadcrumbs(child, url, breadcrumbs);
114 /** Generate title from data given the module routes @private */
115 private setTitleforApplication(breadcrumbs: BREADCRUMBITEM[]): void {
116 let addTitle: string = 'Open Source MANO';
117 breadcrumbs.forEach((data: BREADCRUMBITEM): void => {
118 addTitle += ' | ' + this.translateService.instant(data.title);
120 this.setTitle(addTitle);
122 /** Check and update title based on type of operations @private */
123 private checkTitle(breadcrumbItem: BREADCRUMBITEM, opertionType: string): string {
124 if (!isNullOrUndefined(breadcrumbItem.url)) {
125 if (breadcrumbItem.url.indexOf('packages') !== -1) {
126 return this.matchPackageTitle(opertionType);
128 if (breadcrumbItem.url.indexOf('instances') !== -1) {
129 return this.matchInstanceTitle(opertionType);
131 return breadcrumbItem.title;
134 /** check and update package title based on package type @private */
135 private matchPackageTitle(opertionType: string): string {
136 if (opertionType === 'ns') {
137 return this.translateService.instant('NSPACKAGES');
138 } else if (opertionType === 'vnf') {
139 return this.translateService.instant('VNFPACKAGES');
141 return this.translateService.instant('PAGE.DASHBOARD.NETSLICETEMPLATE');
144 /** check and update package title based on instance type @private */
145 private matchInstanceTitle(opertionType: string): string {
146 if (opertionType === 'ns') {
147 return this.translateService.instant('NSINSTANCES');
148 } else if (opertionType === 'vnf') {
149 return this.translateService.instant('VNFINSTANCES');
150 } else if (opertionType === 'pdu') {
151 return this.translateService.instant('PDUINSTANCES');
153 return this.translateService.instant('PAGE.DASHBOARD.NETSLICEINSTANCE');