Title will be show in the browser head
[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 { 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';
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       .pipe(filter((event: RouterEvent) => event instanceof NavigationEnd), startWith(undefined))
76       .subscribe(() => this.menuItems = this.createBreadcrumbs(this.activatedRoute.root));
77   }
78   /** To set the title if the page @private */
79   public setTitle(newTitle: string): void {
80       this.titleService.setTitle(newTitle);
81   }
82   /** Generate breadcrumbs from data given the module routes @private */
83   private createBreadcrumbs(route: ActivatedRoute, url: string = '', breadcrumbs: BREADCRUMBITEM[] = []):
84     BREADCRUMBITEM[] {
85     const children: ActivatedRoute[] = route.children;
86     if (children.length === 0) {
87       return breadcrumbs;
88     }
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}`;
93       }
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'));
102           }
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);
106           }
107           breadcrumbs.push(item);
108         });
109       }
110       this.setTitleforApplication(breadcrumbs);
111       return this.createBreadcrumbs(child, url, breadcrumbs);
112     }
113   }
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);
119       });
120       this.setTitle(addTitle);
121   }
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);
127       }
128       if (breadcrumbItem.url.indexOf('instances') !== -1) {
129         return this.matchInstanceTitle(opertionType);
130       }
131       return breadcrumbItem.title;
132     }
133   }
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');
140     } else {
141       return this.translateService.instant('PAGE.DASHBOARD.NETSLICETEMPLATE');
142     }
143   }
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');
152     } else {
153       return this.translateService.instant('PAGE.DASHBOARD.NETSLICEINSTANCE');
154     }
155   }
156
157 }