| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame^] | 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 { 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'; |
| 27 | |
| 28 | /** |
| 29 | * Creating component |
| 30 | * @Component takes BreadcrumbComponent.html as template url |
| 31 | */ |
| 32 | @Component({ |
| 33 | selector: 'app-breadcrumb', |
| 34 | templateUrl: './BreadcrumbComponent.html', |
| 35 | styleUrls: ['./BreadcrumbComponent.scss'] |
| 36 | }) |
| 37 | |
| 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'; |
| 42 | |
| 43 | /** To inject services @public */ |
| 44 | public injector: Injector; |
| 45 | |
| 46 | /** To inject breadCrumb Default icon and url @public */ |
| 47 | public readonly home: {} = { icon: 'pi pi-th-large', url: '/' }; |
| 48 | |
| 49 | /** To inject breadCrumb Menus @public */ |
| 50 | public menuItems: BREADCRUMBITEM[]; |
| 51 | |
| 52 | /** Service holds the router information @private */ |
| 53 | private router: Router; |
| 54 | |
| 55 | /** Holds teh instance of AuthService class of type AuthService @private */ |
| 56 | private activatedRoute: ActivatedRoute; |
| 57 | |
| 58 | /** handle translate service @private */ |
| 59 | private translateService: TranslateService; |
| 60 | |
| 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); |
| 66 | } |
| 67 | /** Lifecyle Hooks the trigger before component is instantiate @public */ |
| 68 | public ngOnInit(): void { |
| 69 | this.router.events |
| 70 | .pipe(filter((event: RouterEvent) => event instanceof NavigationEnd), startWith(undefined)) |
| 71 | .subscribe(() => this.menuItems = this.createBreadcrumbs(this.activatedRoute.root)); |
| 72 | } |
| 73 | |
| 74 | /** Generate breadcrumbs from data given the module routes @private */ |
| 75 | private createBreadcrumbs(route: ActivatedRoute, url: string = '', breadcrumbs: BREADCRUMBITEM[] = []): |
| 76 | BREADCRUMBITEM[] { |
| 77 | const children: ActivatedRoute[] = route.children; |
| 78 | if (children.length === 0) { |
| 79 | return breadcrumbs; |
| 80 | } |
| 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}`; |
| 85 | } |
| 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')); |
| 94 | } |
| 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); |
| 98 | } |
| 99 | breadcrumbs.push(item); |
| 100 | }); |
| 101 | } |
| 102 | return this.createBreadcrumbs(child, url, breadcrumbs); |
| 103 | } |
| 104 | } |
| 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); |
| 110 | } |
| 111 | if (breadcrumbItem.url.indexOf('instances') !== -1) { |
| 112 | return this.matchInstanceTitle(opertionType); |
| 113 | } |
| 114 | return breadcrumbItem.title; |
| 115 | } |
| 116 | } |
| 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'); |
| 123 | } else { |
| 124 | return this.translateService.instant('PAGE.DASHBOARD.NETSLICETEMPLATE'); |
| 125 | } |
| 126 | } |
| 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'); |
| 135 | } else { |
| 136 | return this.translateService.instant('PAGE.DASHBOARD.NETSLICEINSTANCE'); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } |