09dd4c1a1f227227ceac54ebb5d9a8f8ce28dc39
[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 } 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
63     constructor(public router: Router, private userProfileService: UserProfileService, private menusService: MenusService, private cookieService: CookieService) {
64
65         this.router.events.subscribe(val => {
66             if (
67                 val instanceof NavigationEnd &&
68                 window.innerWidth <= 992 &&
69                 this.isToggled()
70             ) {
71                 this.toggleSidebar();
72             }
73         });
74     }
75
76     ngOnInit() {
77         this.pushRightClass = 'push-right';
78         this.getUserInformation();
79         
80         this.brandName = "ONAP Portal";
81         if(this.api.brandName != ''){
82             this.brandName = this.api.brandName;
83          }
84
85         this.brandLogoImagePath = "assets/images/global.logo";
86         if(this.api.brandLogoImagePath != ''){
87            this.brandLogoImagePath = this.api.brandLogoImagePath;
88         }
89     }
90
91     getUserInformation() {
92         this.userProfileService.getFunctionalMenuStaticInfo().toPromise().then((res: any) => {
93             if (res == null || res.firstName == null || res.firstName == '' || res.lastName == null || res.lastName == '') {
94                 // $log.info('HeaderCtrl: failed to get all required data, trying user profile');
95                 this.userProfileService.getUserProfile().toPromise().then((profile: any) => {
96                     this.firstName = profile.firstName;
97                     this.lastName = profile.lastName;
98                 }, (err) => {
99                     // $log.error('Header Controller:: getUserProfile() failed: ' + err);
100                 });
101             } else {
102                 this.firstName = res.firstName;
103                 this.lastName = res.lastName;
104                 this.loginSnippetEmail = res.email;
105                 this.loginSnippetUserid = res.userId;
106                 this.lastLogin = Date.parse(res.last_login);
107             }
108             sessionStorage.userId = res.userId;
109             this.menusService.getFunctionalMenuForUser().toPromise().then((jsonHeaderMenu: any) => {
110                 // $scope.menuItems = unflatten(jsonHeaderMenu);
111                 // $scope.megaMenuDataObject = $scope.menuItems;
112             }, (err) => {
113                 // $log.error('HeaderCtrl::GetFunctionalMenuForUser: HeaderCtrl json returned: ' + err);
114             });
115
116         }, (err) => {
117             // $log.error('HeaderCtrl::getFunctionalMenuStaticInfo failed: ' + err);
118         })
119     }
120
121     //     unflatten = function( array, parent, tree ){
122
123     //     tree = typeof tree !== 'undefined' ? tree : [];
124     //     parent = typeof parent !== 'undefined' ? parent : { menuId: null };
125     //     var children = _.filter( array, function(child){ return child.parentMenuId == parent.menuId; });
126
127     //     if( !_.isEmpty( children )  ){
128     //       if( parent.menuId === null ){
129     //         tree = children;
130     //       }else{
131     //         parent['children'] = children
132     //       }
133     //       _.each( children, function( child ){ unflatten( array, child ) } );
134     //     }
135
136     //     return tree;
137     // }
138
139     getUserApplicationRoles() {
140         this.userapproles = [];
141         if (this.displayUserAppRoles) {
142             this.displayUserAppRoles = false;
143         } else {
144             this.displayUserAppRoles = true;
145             this.isLoading = true;
146             this.userProfileService.getUserAppRoles(this.loginSnippetUserid)
147             .subscribe((res: any) => {
148                 this.isLoading = false;
149                 for (var i = 0; i < res.length; i++) {
150                     var userapprole = {
151                         App: res[i].appName,
152                         Roles: res[i].roleNames,
153                     };
154                     this.userapproles.push(userapprole);
155                 }
156             }, (err) => {
157                 this.isLoading = false;
158             });
159         }
160     }
161
162     allAppsLogout() {
163         this.firstName="";
164         this.lastName="";
165         this.displayUserAppRoles=false;         
166         var cookieTabs = this.cookieService.get("visInVisCookieTabs").toString;
167          if(cookieTabs!=null){
168              for(var t in cookieTabs){
169              
170                  var url = cookieTabs[t].content;
171                  if(url != "") {
172                      this.menusService.logout(url);
173                    }
174              }
175          }
176          // wait for individual applications to log out before the portal logout
177          setTimeout(function() {
178              window.location.href = "logout.htm";
179          }, 2000);
180     }
181
182     isToggled(): boolean {
183         const dom: Element = document.querySelector('body');
184         return dom.classList.contains(this.pushRightClass);
185     }
186
187     toggleSidebar() {
188         const dom: any = document.querySelector('body');
189         dom.classList.toggle(this.pushRightClass);
190     }
191
192     onLoggedout() {
193         localStorage.removeItem('isLoggedin');
194     }
195 }