80340553b6bb8bb5407fd946b03397603d40479f
[it/otf.git] / sidebar.component.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Component, OnInit } from '@angular/core';\r
18 import { Router, NavigationEnd } from '@angular/router';\r
19 import { TranslateService } from '@ngx-translate/core';\r
20 import { AppGlobals } from 'app/app.global';\r
21 import {CookieService} from "ngx-cookie-service";\r
22 import { HealthService } from 'app/shared/services/health.service';\r
23 import { UserService } from 'app/shared/services/user.service';\r
24 import { GroupService } from 'app/shared/services/group.service';\r
25 import { Group, Groups } from 'app/shared/models/group.model';\r
26 \r
27 @Component({\r
28     selector: 'app-sidebar',\r
29     templateUrl: './sidebar.component.html',\r
30     styleUrls: ['./sidebar.component.scss']\r
31 })\r
32 export class SidebarComponent implements OnInit {\r
33     isActive: boolean = false;\r
34     showMenu: string = '';\r
35     pushRightClass: string = 'push-right';\r
36     version = AppGlobals.version\r
37     tcuapi: boolean;\r
38     tcuengine: boolean;\r
39     isAdmin: boolean = false;\r
40 \r
41     canManageGroup = false;\r
42 \r
43     currentGroupId;\r
44 \r
45     constructor(private translate: TranslateService, public router: Router, public user: UserService, private health: HealthService, private cookie: CookieService, public group: GroupService) {\r
46         this.translate.addLangs(['en', 'fr', 'ur', 'es', 'it', 'fa', 'de']);\r
47         this.translate.setDefaultLang('en');\r
48         const browserLang = this.translate.getBrowserLang();\r
49         this.translate.use(browserLang.match(/en|fr|ur|es|it|fa|de/) ? browserLang : 'en');\r
50         this.checkIsAdmin();\r
51         this.router.events.subscribe(val => {\r
52             if (\r
53                 val instanceof NavigationEnd &&\r
54                 window.innerWidth <= 992 &&\r
55                 this.isToggled()\r
56             ) {\r
57                 this.toggleSidebar();\r
58             }\r
59         });\r
60     }\r
61 \r
62     ngOnInit(){\r
63         if(this.group.getGroup()){\r
64             this.checkManage(this.group.getGroup());\r
65         }\r
66         this.group.groupChange().subscribe(group => {\r
67             this.checkManage(group);\r
68         })\r
69         this.setHealthStatus();\r
70     }\r
71 \r
72     checkManage(group){\r
73         this.canManageGroup = this.user.ability.can('management', new Groups(group));\r
74     }\r
75 \r
76     setHealthStatus(){\r
77         this.health.get('tcu-api').subscribe(res => {\r
78             if(res['code'] == 200 || res['statusCode'] == 200){\r
79                 this.tcuapi = true;\r
80             }else{\r
81                 this.tcuapi = false;\r
82             }\r
83         }, err => {\r
84             this.tcuapi = false;\r
85         });\r
86 \r
87         this.health.get('tcu-engine').subscribe(res => {\r
88             \r
89             if(res['code'] == 200 || res['statusCode'] == 200){\r
90                 this.tcuengine = true;\r
91             }else{\r
92                 this.tcuengine = false;\r
93             }\r
94         }, err => {\r
95             \r
96             this.tcuengine = false;\r
97         });\r
98     }\r
99 \r
100     eventCalled() {\r
101         this.isActive = !this.isActive;\r
102     }\r
103 \r
104     addExpandClass(element: any) {\r
105         if (element === this.showMenu) {\r
106             this.showMenu = '0';\r
107         } else {\r
108             this.showMenu = element;\r
109         }\r
110     }\r
111 \r
112     isToggled(): boolean {\r
113         const dom: Element = document.querySelector('body');\r
114         return dom.classList.contains(this.pushRightClass);\r
115     }\r
116 \r
117     toggleSidebar() {\r
118         const dom: any = document.querySelector('body');\r
119         dom.classList.toggle(this.pushRightClass);\r
120     }\r
121 \r
122     rltAndLtr() {\r
123         const dom: any = document.querySelector('body');\r
124         dom.classList.toggle('rtl');\r
125     }\r
126 \r
127     changeLang(language: string) {\r
128         this.translate.use(language);\r
129     }\r
130 \r
131     onLoggedout() {\r
132         localStorage.removeItem('isLoggedin');\r
133     }\r
134 \r
135     checkIsAdmin() {\r
136         if (this.cookie.get('access_token') && this.cookie.get('currentUser')) {\r
137             let currentUser = JSON.parse(this.cookie.get('currentUser'));\r
138             if (currentUser['permissions'].indexOf('admin') >= 0) {\r
139                 this.isAdmin = true;\r
140                 return true;\r
141             }\r
142         }\r
143         this.isAdmin = false;\r
144         return false;\r
145     }\r
146 \r
147 }\r