Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / fusion / scripts / directives / header.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 app.directive('qHeader', function () {
21     return {
22         restrict: 'A', //This menas that it will be used as an attribute and NOT as an element. I don't like creating custom HTML elements
23         replace: false,
24         templateUrl: "app/fusion/scripts/view-models/header.html",
25         controller: ['$scope', '$filter','$http','$timeout', '$log','UserInfoService', '$window', '$cookies', function ($scope, $filter, $http, $timeout, $log,UserInfoService, $window, $cookies) {
26                 
27                 /*Define fields*/
28                 $scope.userName;
29                 $scope.userFirstName;
30                 $scope.redirectUrl;
31                 $scope.contactUsUrl;
32                 $scope.getAccessUrl;
33                 $scope.childData=[];
34                 $scope.parentData=[];
35                 $scope.menuItems = [];
36                 $scope.loadMenufail=false;
37                 $scope.megaMenuDataObject=[];           
38                 $scope.activeClickSubMenu = {
39                         x: ''
40                 };
41                 $scope.activeClickMenu = {
42                         x: ''
43                 };
44                 $scope.favoritesMenuItems = [];
45             $scope.favoriteItemsCount = 0;
46             $scope.showFavorites = false;
47             $scope.emptyFavorites = false;
48             $scope.favoritesWindow = false;
49                 
50             /*Menu Structure*/
51                         var menuStructureConvert = function(menuItems) {        
52                         console.log(menuItems);                 
53                                 $scope.megaMenuDataObjectTemp = 
54                                 [
55                                          {
56                                                  text: "ECOMP",
57                                                  children: menuItems
58                                          },
59                                          {
60                                                  text: "Help",
61                                                  children: [
62                                                             {
63                                                                  text:"Contact Us",
64                                                                  url:$scope.contactUsUrl
65                                                             },
66                                                                         {
67                                                              text:"Get Access",
68                                                              url:$scope.getAccessUrl
69                                                                         }]
70                                          }
71                                  ];                             
72                                 return $scope.megaMenuDataObjectTemp;
73                 };
74                 
75                 /***************functions**************/
76                 /*Put user info into fields*/
77                 $scope.inputUserInfo = function(userInfo){
78                         if (typeof(userInfo) != "undefined" && userInfo!=null && userInfo!=''){
79                                 if(typeof(userInfo.USER_FIRST_NAME) != "undefined" && userInfo.USER_FIRST_NAME!=null){          
80                                         $scope.userFirstName = userInfo.USER_FIRST_NAME;
81                                 }
82                         }               
83                 }
84                 /*getting user info from session*/
85                 $scope.getUserNameFromSession = function(){
86                         UserInfoService.getFunctionalMenuStaticDetailSession()
87                         .then(function (res) {
88                                 $scope.contactUsUrl=res.contactUsLink;
89                                 $scope.userName = res.userName;
90                                 $scope.userFirstName = res.firstName;
91                                 $scope.redirectUrl = res.portalUrl;
92                                 $scope.getAccessUrl = res.getAccessUrl;
93                         });
94                 }
95                 $scope.getTopMenuStaticInfo=function() {
96                         var promise = UserInfoService.getFunctionalMenuStaticDetailShareContext();
97                         promise.then(
98                                 function(res) {                                         
99                                         if(res==null || res==''){
100                                                 $log.info('failed getting static User information');     
101                                                 $scope.getUserNameFromSession();
102                                         }else{
103                                                 $log.info('Received static User information');
104                                                 
105                                                 var resData = res;
106                                                 console.log(resData);
107                                                 $scope.inputUserInfo(resData);
108                                                 $scope.userName = $scope.firstName+ ' '+ $scope.lastName;
109                                         }
110                                 },
111                                 function(err) {
112                                                 $log.info('failed getting static User information');                                    
113                                 }
114                         );
115                 }
116
117                 $scope.returnToPortal=function(){
118                         window.location.href = $scope.redirectUrl;
119                 }
120                 
121                 var unflatten = function( array, parent, tree ){
122                     tree = typeof tree !== 'undefined' ? tree : [];
123                     parent = typeof parent !== 'undefined' ? parent : { menuId: null };
124                     var children = _.filter( array, function(child){ return child.parentMenuId == parent.menuId; });
125                     if( !_.isEmpty( children )  ){
126                       if( parent.menuId === null ){
127                         tree = children;
128                       }else{
129                         parent['children'] = children
130                       }
131                       _.each( children, function( child ){ unflatten( array, child ) } );
132                     }
133                     return tree;
134                 }
135                 
136                 $scope.getMenu=function() {
137                         $scope.getTopMenuStaticInfo();
138                         $http({
139                                 method: "GET",
140                                 url: 'get_functional_menu',
141 // TIMEOUT USED FOR LOCAL TESTING ONLY                          
142 //                              timeout: 100
143                          }).success(function (response) {
144                                  
145                                                 if (response == '101: Timeout') {
146                                                         $log.debug('Timeout attempting to get_functional_menu');
147                                                         $scope.megaMenuDataObject = menuStructureConvert("");
148 //                                                      $scope.createErrorMenu();
149                                                         //$scope.loadMenufail=true;
150                                                 } else {
151                                                         if(typeof response != 'undefined' && response.length!=0 && typeof response[0] != 'undefined' && typeof response[0].error!="undefined"){
152                                                                 $log.debug('Timeout attempting to get_functional_menu');                                                                
153                                                                 $scope.menuItems = unflatten( response);
154                                                                 $scope.megaMenuDataObject = menuStructureConvert($scope.menuItems);                                              
155 //                                                              $scope.createErrorMenu();
156                                                                 //$scope.loadMenufail=true;
157                                                         }else{
158                                                                 $scope.loadMenufail=false;
159                                                                 $scope.contactUsURL = response.contactUsLink;
160                                                                 $log.debug('functional_menu',response);                                                         
161                                                                 $scope.megaMenuDataObject = menuStructureConvert("");
162                                                         }
163                                                 }
164                          }).error(function (response){
165                                                 $scope.megaMenuDataObject = menuStructureConvert("");
166 //                                              $scope.createErrorMenu();
167                                                 //$scope.loadMenufail=true;
168                                         $log.debug('REST API failed get_functional_menu...');
169                          });
170                         
171                 }
172                 
173                 $scope.adjustHeader=function() {
174                         $scope.showHeader = ($cookies.show_app_header == undefined ? true : $cookies.show_app_header);
175                         
176                         if($scope.showHeader == true) {
177                         $scope.drawer_margin_top = 70;
178                         $scope.drawer_custom_top = 54;
179                         $scope.toggle_drawer_top = 55;
180                         }
181                         else  {
182                                 
183                                 $scope.drawer_margin_top = 60;
184                         $scope.drawer_custom_top = 0;
185                         $scope.toggle_drawer_top = 10;
186                         }
187                         
188                 }
189
190                 $scope.getMenu();
191                 $scope.adjustHeader();
192
193 /* **************************************************************************/
194 /* Logic for the favorite menus is here */
195
196             $scope.loadFavorites = function () {
197                 $log.debug('loadFavorites has happened.');
198                 if ($scope.favoritesMenuItems == '') {
199                     $scope.generateFavoriteItems();
200                     $log.debug('loadFavorites is calling generateFavoriteItems()');
201                 } else {
202                     $log.debug('loadFavorites is NOT calling generateFavoriteItems()');
203                 }
204             }
205             
206             $scope.goToUrl = function (item) {
207                 $log.info("goToUrl called")
208                 $log.info(item);
209
210                 var url = item.url;
211                 var restrictedApp = item.restrictedApp;
212                 $log.debug('Restricted app status is: ' + restrictedApp);
213                 if (!url) {
214                     $log.info('No url found for this application, doing nothing..');
215                     return;
216                 }
217                 if (restrictedApp) {
218                     $window.open(url, '_blank');
219                 } else {
220                     $window.open(url, '_self');
221                 }
222
223             }
224             
225             $scope.submenuLevelAction = function(index, column) {
226                 if ($scope.favoritesMenuItems == '') {
227                     $scope.generateFavoriteItems();
228                     $log.debug('submenuLevelAction is calling generateFavoriteItems()');
229                 }
230                 $log.debug('item hovered/clicked: ' + index + '; column = ' + column);
231                 if (column == 2) {  // 2 is Design
232                     $scope.favoritesWindow = false;
233                     $scope.showFavorites = false;
234                     $scope.emptyFavorites = false;
235                 }
236                 if (index=='Favorites' && $scope.favoriteItemsCount != 0) {
237                     $log.debug('Showing Favorites window');
238                     $scope.favoritesWindow = true;
239                     $scope.showFavorites = true;
240                     $scope.emptyFavorites = false;
241                 }
242                 if (index=='Favorites' && $scope.favoriteItemsCount == 0) {
243                     $log.debug('Hiding Favorites window in favor of No Favorites Window');
244                     $scope.favoritesWindow = true;
245                     $scope.showFavorites = false;
246                     $scope.emptyFavorites = true;
247                 }
248                 if (column > 2) {
249                     $scope.favoritesWindow = false;
250                     $scope.showFavorites = false;
251                     $scope.emptyFavorites = false;
252                 }
253             };
254             
255             $scope.hideFavoritesWindow = function() {
256                 $log.debug('$scope.hideFavoritesWindow has been called');
257                 $scope.showFavorites = false;
258                 $scope.emptyFavorites = false;
259             }
260             
261             $scope.isUrlFavorite = function (menuId) {
262 //                 $log.debug('array objects in menu favorites = ' + $scope.favoriteItemsCount + '; menuId=' + menuId);
263                 var jsonMenu =  JSON.stringify($scope.favoritesMenuItems);
264                 var isMenuFavorite =  jsonMenu.indexOf('menuId\":' + menuId);
265                 if (isMenuFavorite==-1) {
266                     return false;
267                 } else {
268                     return true;
269                 }
270
271             }
272             
273             $scope.generateFavoriteItems  = function() {
274                 $http({
275                         method: "GET",
276                         url: 'get_favorites',
277 // TIMEOUT USED FOR LOCAL TESTING ONLY                          
278 //                              timeout: 100
279                             }).success(function (response) {
280                                                 if (response == '101: Timeout') {
281                                                 $log.error('Timeout attempting to get_favorites_menu');
282                                         } else {
283                                                 if(typeof response != 'undefined' && response.length!=0 && typeof response[0] != 'undefined' && typeof response[0].error!="undefined"){
284                                                     $log.error('REST API failed get_favorites' + response);
285                                                         }else{
286                                                                 $log.debug('get_favorites = ' + JSON.stringify(response));
287                                                                 $scope.favoritesMenuItems = response;
288                                         $scope.favoriteItemsCount = Object.keys($scope.favoritesMenuItems).length;
289                                         $log.info('number of favorite menus: ' + $scope.favoriteItemsCount);
290                                 }
291                                                 }
292                                 }).error(function (response){
293                                     $log.error('REST API failed get_favorites' + response);
294 //createFavoriteErrorMenu() USED FOR LOCAL TESTING ONLY                         
295 //                                      $scope.createFavoriteErrorMenu();
296                                 });
297                 }
298
299                 $scope.createFavoriteErrorMenu=function() {
300                 $scope.favoritesMenuItems = [];
301                 $scope.favoriteItemsCount = Object.keys($scope.favoritesMenuItems).length;
302                 $log.info('number of favorite menus: ' + $scope.favoriteItemsCount);
303                 }
304             
305                 /* end of Favorite Menu code */
306                 /* **************************************************************************/
307
308                 
309                 /* **************************************************************************/
310                 // THIS IS USED FOR LOCAL TESTING ONLY
311                 /* **************************************************************************/
312                 $scope.createErrorMenu=function() {
313                                 $log.debug('Creating fake menu now...');
314 //                      $scope.loadMenufail=true;
315                 $scope.menuItems = [
316                                        {
317                                             "menuId": 1,
318                                             "column": 2,
319                                             "text": "Design",
320                                             "parentMenuId": null,
321                                             "url": ""
322                                           },
323                                           {
324                                             "menuId": 2,
325                                             "column": 3,
326                                             "text": "Infrastructure Ordering",
327                                             "parentMenuId": null,
328                                             "url": ""
329                                           },
330                                           {
331                                             "menuId": 3,
332                                             "column": 4,
333                                             "text": "Service Creation",
334                                             "parentMenuId": null,
335                                             "url": ""
336                                           },
337                                           {
338                                             "menuId": 4,
339                                             "column": 5,
340                                             "text": "Service Mgmt",
341                                             "parentMenuId": null,
342                                             "url": ""
343                                           },
344                                           {
345                                             "menuId": 90,
346                                             "column": 1,
347                                             "text": "Google",
348                                             "parentMenuId": 1,
349                                             "url": ""
350                                           },
351                                           {
352                                             "menuId": 91,
353                                             "column": 1,
354                                             "text": "Mike Little's Coffee Cup",
355                                             "parentMenuId": 2,
356                                             "url": ""
357                                           },
358                                           {
359                                             "menuId": 92,
360                                             "column": 2,
361                                             "text": "Andy and his Astrophotgraphy",
362                                             "parentMenuId": 3,
363                                             "url": ""
364                                           },
365                                           {
366                                             "menuId": 93,
367                                             "column": 1,
368                                             "text": "JSONLint",
369                                             "parentMenuId": 4,
370                                             "url": ""
371                                           },
372                                           {
373                                             "menuId": 94,
374                                             "column": 2,
375                                             "text": "HROneStop",
376                                             "parentMenuId": 4,
377                                             "url": ""
378                                           },
379                                           {
380                                             "menuId": 95,
381                                             "column": 2,
382                                             "text": "4th Level App4a R16",
383                                             "parentMenuId": 4,
384                                             "url": ""
385                                           },
386                                           {
387                                             "menuId": 96,
388                                             "column": 3,
389                                             "text": "3rd Level App1c R200",
390                                             "parentMenuId": 4,
391                                             "url": ""
392                                           },
393                                           {
394                                             "menuId": 97,
395                                             "column": 1,
396                                             "text": "3rd Level App4b R16",
397                                             "parentMenuId": 5,
398                                             "url": ""
399                                           },
400                                           {
401                                             "menuId": 98,
402                                             "column": 2,
403                                             "text": "3rd Level App2b R16",
404                                             "parentMenuId": 5,
405                                             "url": ""
406                                           },
407                                           {
408                                             "menuId": 99,
409                                             "column": 1,
410                                             "text": "Favorites",
411                                             "parentMenuId": null,
412                                             "url": ""
413                                           }
414                                         ];
415                                 $scope.menuItems = unflatten( $scope.menuItems );
416                                 //remove this
417                                 $scope.megaMenuDataObject = menuStructureConvert($scope.menuItems);
418                 }
419         }]
420     }
421 });
422
423 app.filter("ellipsis", function(){
424     return function(text, length){
425         if (text) {
426             var ellipsis = text.length > length ? "..." : "";
427             return text.slice(0, length) + ellipsis;
428         };
429         return text;        
430     }
431 });
432
433 function reloadPageOnce() {
434         if( window.localStorage )
435         {
436           if( !localStorage.getItem('firstLoad') )
437           {
438             localStorage['firstLoad'] = true;
439             window.location.reload();
440           }  
441           else
442             localStorage.removeItem('firstLoad');
443         }
444 }
445 app.controller('loginSnippetCtrl', function ($scope,$http, $log,UserInfoService){
446         /*Define fields*/
447         $scope.userProfile={
448                         firstName:'',
449                         lastName:'',
450                         fullName:'',
451                         email:'',
452                         userid:''
453         }
454         /*Put user info into fields*/
455         $scope.inputUserInfo = function(userInfo){
456                 if (typeof(userInfo) != "undefined" && userInfo!=null && userInfo!=''){
457                         if (typeof(userInfo.USER_FIRST_NAME) != "undefined" && userInfo.USER_FIRST_NAME!=null && userInfo.USER_FIRST_NAME!='')
458                         $scope.userProfile.firstName = userInfo.USER_FIRST_NAME;
459                 if (typeof(userInfo.USER_LAST_NAME) != "undefined" && userInfo.USER_LAST_NAME!=null && userInfo.USER_LAST_NAME!='')
460                         $scope.userProfile.lastName = userInfo.USER_LAST_NAME;
461                 if (typeof(userInfo.USER_EMAIL) != "undefined" && userInfo.USER_EMAIL!=null && userInfo.USER_EMAIL!='')
462                         $scope.userProfile.email = userInfo.USER_EMAIL; 
463                 if (typeof(userInfo.USER_ORGUSERID) != "undefined" && userInfo.USER_ORGUSERID!=null && userInfo.USER_ORGUSERID!='')
464                                 $scope.userProfile.userid = userInfo.USER_ORGUSERID;            
465                 }               
466         }
467         /*getting user info from session*/
468         $scope.getUserNameFromSession = function(){
469                 UserInfoService.getFunctionalMenuStaticDetailSession()
470                 .then(function (response) {                     
471                         $scope.userProfile.fullName = response.userName;
472                         $scope.userProfile.userid = response.userid;
473                         $scope.userProfile.email = response.email;
474                 });
475     }
476         /*getting user info from shared context*/
477         $scope.getUserName=function() {
478                 var promise = UserInfoService.getFunctionalMenuStaticDetailShareContext();
479                 promise.then(
480                         function(res) { 
481                                 if(res==null || res==''){
482                                         $log.info('Getting User information from session');
483                                         $scope.getUserNameFromSession();
484                                         
485                                 }else{
486                                         $log.info('Received User information from shared context',res);
487                                         var resData = res;
488                                         console.log(resData);
489                                         $scope.inputUserInfo(resData);
490                                         $scope.userProfile.fullName = $scope.userProfile.firstName+ ' '+ $scope.userProfile.lastName;                                   
491                                 }
492                         },
493                         function(err) {
494                                 console.log('error');
495                         }
496                 );
497     };
498     /*call the get user info function*/
499     try{
500         $scope.getUserName();
501     }catch(err){
502         $log.info('Error while getting User information',err);
503     }
504 });