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