74f6fb3a43c7978c215bef63e9bd6de100807a35
[portal.git] / portal-FE-common / src / app / layout / components / header / header.component.ts
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright � 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 import { Component, OnInit, Output, EventEmitter } from '@angular/core';
39 import { Router, NavigationEnd } from '@angular/router';
40 import { UserProfileService, MenusService } from 'src/app/shared/services';
41 import { CookieService } from 'ngx-cookie-service';
42 import { environment } from 'src/environments/environment';
43
44 @Component({
45     selector: 'app-header',
46     templateUrl: './header.component.html',
47     styleUrls: ['./header.component.scss']
48 })
49 export class HeaderComponent implements OnInit {
50     public pushRightClass: string;
51     firstName: string;
52     lastName: string;
53     loginSnippetUserid: any;
54     lastLogin: number;
55     loginSnippetEmail: any;
56     userapproles: any[];
57     displayUserAppRoles: any;
58     isLoading: boolean;
59     api = environment.api;
60     brandName: string;
61     brandLogoImagePath: string;
62     isSystemUser: boolean = false;
63         languages: string[] = [];
64     result: any;
65         
66     @Output() languageEvent = new EventEmitter();
67
68     constructor(public router: Router, private userProfileService: UserProfileService, private menusService: MenusService, private cookieService: CookieService) {
69
70         this.router.events.subscribe(val => {
71             if (
72                 val instanceof NavigationEnd &&
73                 window.innerWidth <= 992 &&
74                 this.isToggled()
75             ) {
76                 this.toggleSidebar();
77             }
78         });
79     }
80
81     ngOnInit() {
82         this.pushRightClass = 'push-right';
83         this.getUserInformation();
84         
85         this.brandName = "ONAP Portal";
86         if(this.api.brandName != ''){
87             this.brandName = this.api.brandName;
88          }
89
90         this.brandLogoImagePath = "assets/images/global.logo";
91         if(this.api.brandLogoImagePath != ''){
92            this.brandLogoImagePath = this.api.brandLogoImagePath;
93         }
94                 
95                 this.menusService.getAllLanguages().subscribe(data =>{
96         this.result = data;
97             for(let lang of this.result.languageList ){
98                 this.languages.push(lang);
99             }
100                         
101         this.menusService.getCurrentLang(this.loginSnippetUserid).subscribe(data=>{
102         this.result = data;
103                 this.languages.map((obj:any)=>{
104                     obj.selected = obj.languageId == parseInt(this.result.languageId);
105                 })   ;
106             });
107         });
108     }
109
110     getUserInformation() {
111         this.userProfileService.getFunctionalMenuStaticInfo().toPromise().then((res: any) => {
112             if (res === null || res.firstName === null || res.firstName === '' || res.lastName === null || res.lastName === '') {
113                 // $log.info('HeaderCtrl: failed to get all required data, trying user profile');
114                 this.userProfileService.getUserProfile().toPromise().then((profile: any) => {
115                     this.firstName = profile.firstName;
116                     this.lastName = profile.lastName;
117                 }, (err) => {
118                     // $log.error('Header Controller:: getUserProfile() failed: ' + err);
119                 });
120             } else {
121                 this.firstName = res.firstName;
122                 this.lastName = res.lastName;
123                 this.loginSnippetEmail = res.email;
124                 this.loginSnippetUserid = res.userId;
125                 this.lastLogin = Date.parse(res.last_login);
126             }
127             if(res != null && res.isSystemUser === 'true'){
128                 this.isSystemUser = true;
129             }
130             sessionStorage.userId = res.userId;
131             this.menusService.getFunctionalMenuForUser().toPromise().then((jsonHeaderMenu: any) => {
132                 // $scope.menuItems = unflatten(jsonHeaderMenu);
133                 // $scope.megaMenuDataObject = $scope.menuItems;
134             }, (err) => {
135                 // $log.error('HeaderCtrl::GetFunctionalMenuForUser: HeaderCtrl json returned: ' + err);
136             });
137
138         }, (err) => {
139             // $log.error('HeaderCtrl::getFunctionalMenuStaticInfo failed: ' + err);
140         })
141     }
142
143     //     unflatten = function( array, parent, tree ){
144
145     //     tree = typeof tree !== 'undefined' ? tree : [];
146     //     parent = typeof parent !== 'undefined' ? parent : { menuId: null };
147     //     var children = _.filter( array, function(child){ return child.parentMenuId == parent.menuId; });
148
149     //     if( !_.isEmpty( children )  ){
150     //       if( parent.menuId === null ){
151     //         tree = children;
152     //       }else{
153     //         parent['children'] = children
154     //       }
155     //       _.each( children, function( child ){ unflatten( array, child ) } );
156     //     }
157
158     //     return tree;
159     // }
160
161     getUserApplicationRoles() {
162         this.userapproles = [];
163         if (this.displayUserAppRoles) {
164             this.displayUserAppRoles = false;
165         } else {
166             this.displayUserAppRoles = true;
167             this.isLoading = true;
168             this.userProfileService.getUserAppRoles(this.loginSnippetUserid)
169             .subscribe((res: any) => {
170                 this.isLoading = false;
171                 for (var i = 0; i < res.length; i++) {
172                     var userapprole = {
173                         App: res[i].appName,
174                         Roles: res[i].roleNames,
175                     };
176                     this.userapproles.push(userapprole);
177                 }
178             }, (err) => {
179                 this.isLoading = false;
180             });
181         }
182     }
183
184     allAppsLogout() {
185         this.firstName="";
186         this.lastName="";
187         this.displayUserAppRoles=false;         
188         var cookieTabs = this.cookieService.get("visInVisCookieTabs").toString;
189          if(cookieTabs!=null){
190              for(var t in cookieTabs){
191              
192                  var url = cookieTabs[t].content;
193                  if(url != "") {
194                      this.menusService.logout(url);
195                    }
196              }
197          }
198          // wait for individual applications to log out before the portal logout
199          setTimeout(function() {
200              window.location.href = "logout.htm";
201          }, 2000);
202     }
203
204     isToggled(): boolean {
205         const dom: Element = document.querySelector('body');
206         return dom.classList.contains(this.pushRightClass);
207     }
208
209     toggleSidebar() {
210         const dom: any = document.querySelector('body');
211         dom.classList.toggle(this.pushRightClass);
212     }
213
214     onLoggedout() {
215         localStorage.removeItem('isLoggedin');
216     }
217         
218         setLanguage(langId : string){
219         
220         this.menusService.setLanguage(langId, this.loginSnippetUserid).subscribe(data =>{
221             console.log("Language Applied :", data);
222             this.languageEvent.emit(langId);
223         });
224     }
225 }