Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[osm/NG-UI.git] / src / app / layouts / breadcrumb / BreadcrumbComponent.ts
1 /*
2  Copyright 2020 TATA ELXSI
3
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
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
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.
15
16  Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17 */
18 /**
19  * @file Bread Crumb component.
20  */
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';
28
29 /**
30  * Creating component
31  * @Component takes BreadcrumbComponent.html as template url
32  */
33 @Component({
34   selector: 'app-breadcrumb',
35   templateUrl: './BreadcrumbComponent.html',
36   styleUrls: ['./BreadcrumbComponent.scss']
37 })
38
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';
43
44   /** To inject services @public */
45   public injector: Injector;
46
47   /** To inject breadCrumb Default icon and url @public */
48   public readonly home: {} = { icon: 'pi pi-th-large', url: '/' };
49
50   /** To inject breadCrumb Menus @public */
51   public menuItems: BREADCRUMBITEM[];
52
53   /** Service holds the router information @private */
54   private router: Router;
55
56   /** Holds teh instance of AuthService class of type AuthService @private */
57   private activatedRoute: ActivatedRoute;
58
59   /** handle translate service @private */
60   private translateService: TranslateService;
61
62   /** Title Service in create ticket page */
63   private titleService: Title;
64
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);
71   }
72   /** Lifecyle Hooks the trigger before component is instantiate @public */
73   public ngOnInit(): void {
74     this.router.events
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));
78   }
79   /** To set the title if the page @private */
80   public setTitle(newTitle: string): void {
81       this.titleService.setTitle(newTitle);
82   }
83   /** Generate breadcrumbs from data given the module routes @private */
84   private createBreadcrumbs(route: ActivatedRoute, url: string = '', breadcrumbs: BREADCRUMBITEM[] = []):
85     BREADCRUMBITEM[] {
86     const children: ActivatedRoute[] = route.children;
87     if (children.length === 0) {
88       return breadcrumbs;
89     }
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}`;
94       }
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}', localStorage.getItem('project'));
103           }
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);
107           }
108           breadcrumbs.push(item);
109         });
110       }
111       this.setTitleforApplication(breadcrumbs);
112       return this.createBreadcrumbs(child, url, breadcrumbs);
113     }
114   }
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);
120       });
121       this.setTitle(addTitle);
122   }
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);
128       }
129       if (breadcrumbItem.url.indexOf('instances') !== -1) {
130         return this.matchInstanceTitle(opertionType);
131       }
132       return breadcrumbItem.title;
133     }
134   }
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');
141     } else {
142       return this.translateService.instant('PAGE.DASHBOARD.NETSLICETEMPLATE');
143     }
144   }
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');
153     } else {
154       return this.translateService.instant('PAGE.DASHBOARD.NETSLICEINSTANCE');
155     }
156   }
157 }