Language support fix for portal
[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         }
96
97     getLanguageInfo() {
98         this.menusService.getAllLanguages().subscribe(data =>{
99             this.result = data;
100             for(let lang of this.result.languageList ){
101                 this.languages.push(lang);
102             }
103
104             this.menusService.getCurrentLang(this.loginSnippetUserid).subscribe(data=>{
105             this.result = data;
106                 this.languages.map((obj:any)=>{
107                     obj.selected = obj.languageId == parseInt(this.result.languageId);
108                 })   ;                
109             });
110         });
111     }
112
113     getUserInformation() {
114         this.userProfileService.getFunctionalMenuStaticInfo().toPromise().then((res: any) => {
115             if (res === null || res.firstName === null || res.firstName === '' || res.lastName === null || res.lastName === '') {
116                 // $log.info('HeaderCtrl: failed to get all required data, trying user profile');
117                 this.userProfileService.getUserProfile().toPromise().then((profile: any) => {
118                     this.firstName = profile.firstName;
119                     this.lastName = profile.lastName;
120                 }, (err) => {
121                     // $log.error('Header Controller:: getUserProfile() failed: ' + err);
122                 });
123             } else {
124                 this.firstName = res.firstName;
125                 this.lastName = res.lastName;
126                 this.loginSnippetEmail = res.email;
127                 this.loginSnippetUserid = res.userId;
128                 this.lastLogin = Date.parse(res.last_login);
129                 this.getLanguageInfo();
130             }
131             if(res != null && res.isSystemUser === 'true'){
132                 this.isSystemUser = true;
133             }
134             sessionStorage.userId = res.userId;
135             this.menusService.getFunctionalMenuForUser().toPromise().then((jsonHeaderMenu: any) => {
136                 // $scope.menuItems = unflatten(jsonHeaderMenu);
137                 // $scope.megaMenuDataObject = $scope.menuItems;
138             }, (err) => {
139                 // $log.error('HeaderCtrl::GetFunctionalMenuForUser: HeaderCtrl json returned: ' + err);
140             });
141
142         }, (err) => {
143             // $log.error('HeaderCtrl::getFunctionalMenuStaticInfo failed: ' + err);
144         })
145     }
146
147     //     unflatten = function( array, parent, tree ){
148
149     //     tree = typeof tree !== 'undefined' ? tree : [];
150     //     parent = typeof parent !== 'undefined' ? parent : { menuId: null };
151     //     var children = _.filter( array, function(child){ return child.parentMenuId == parent.menuId; });
152
153     //     if( !_.isEmpty( children )  ){
154     //       if( parent.menuId === null ){
155     //         tree = children;
156     //       }else{
157     //         parent['children'] = children
158     //       }
159     //       _.each( children, function( child ){ unflatten( array, child ) } );
160     //     }
161
162     //     return tree;
163     // }
164
165     getUserApplicationRoles() {
166         this.userapproles = [];
167         if (this.displayUserAppRoles) {
168             this.displayUserAppRoles = false;
169         } else {
170             this.displayUserAppRoles = true;
171             this.isLoading = true;
172             this.userProfileService.getUserAppRoles(this.loginSnippetUserid)
173             .subscribe((res: any) => {
174                 this.isLoading = false;
175                 for (var i = 0; i < res.length; i++) {
176                     var userapprole = {
177                         App: res[i].appName,
178                         Roles: res[i].roleNames,
179                     };
180                     this.userapproles.push(userapprole);
181                 }
182             }, (err) => {
183                 this.isLoading = false;
184             });
185         }
186     }
187
188     allAppsLogout() {
189         this.firstName="";
190         this.lastName="";
191         this.displayUserAppRoles=false;         
192         var cookieTabs = this.cookieService.get("visInVisCookieTabs").toString;
193          if(cookieTabs!=null){
194              for(var t in cookieTabs){
195              
196                  var url = cookieTabs[t].content;
197                  if(url != "") {
198                      this.menusService.logout(url);
199                    }
200              }
201          }
202          // wait for individual applications to log out before the portal logout
203          setTimeout(function() {
204              window.location.href = "logout.htm";
205          }, 2000);
206     }
207
208     isToggled(): boolean {
209         const dom: Element = document.querySelector('body');
210         return dom.classList.contains(this.pushRightClass);
211     }
212
213     toggleSidebar() {
214         const dom: any = document.querySelector('body');
215         dom.classList.toggle(this.pushRightClass);
216     }
217
218     onLoggedout() {
219         localStorage.removeItem('isLoggedin');
220     }
221         
222         setLanguage(langId : string){
223         
224         this.menusService.setLanguage(langId, this.loginSnippetUserid).subscribe(data =>{
225             this.languageEvent.emit(langId);
226         });
227     }
228 }