Modify html label to support multi-language
[portal.git] / ecomp-portal-FE-common / client / app / views / header / header.controller.js
index 6b5be74..e464048 100644 (file)
@@ -1,39 +1,83 @@
 /*-
- * ================================================================================
- * ECOMP Portal
- * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
+ * ============LICENSE_START==========================================
+ * ONAP Portal
+ * ===================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ===================================================================
+ *
+ * Unless otherwise specified, all software contained herein is licensed
+ * under the Apache License, Version 2.0 (the "License");
+ * you may not use this software except in compliance with the License.
  * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
+ *             http://www.apache.org/licenses/LICENSE-2.0
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- * ================================================================================
+ *
+ * Unless otherwise specified, all documentation contained herein is licensed
+ * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
+ * you may not use this documentation except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *             https://creativecommons.org/licenses/by/4.0/
+ *
+ * Unless required by applicable law or agreed to in writing, documentation
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END============================================
+ *
+ * 
  */
 'use strict';
 (function () {
        class HeaderCtrl {
-        constructor($log, $window, userProfileService, menusService, $scope, ECOMP_URL_REGEX, $cookies, $state,auditLogService,notificationService,ngDialog,$modal) {
-            this.firstName = '';
-            this.lastName = '';
-            this.$log = $log;
-            this.menusService = menusService;
-            this.$scope = $scope;
-            this.favoritesMenuItems = '';
-            $scope.favoriteItemsCount = 0;
-            $scope.favoritesMenuItems = '';
-            $scope.showFavorites = false;
-            $scope.emptyFavorites = false;
-            $scope.favoritesWindow = false;
-            $scope.notificationCount=0;
-            $scope.showNotification = true;
+    constructor($log, $window, $translate, translateService,userProfileService, menusService, $scope, ECOMP_URL_REGEX, $cookies, $state,auditLogService,notificationService,ngDialog,$modal) {
+      this.firstName = '';
+      this.lastName = '';
+      this.$log = $log;
+      this.menusService = menusService;
+      this.$scope = $scope;
+      this.favoritesMenuItems = '';
+      $scope.cur_lang = '';
+      $scope.langList = []
+      $scope.favoriteItemsCount = 0;
+      $scope.favoritesMenuItems = '';
+      $scope.showFavorites = false;
+      $scope.emptyFavorites = false;
+      $scope.favoritesWindow = false;
+      $scope.notificationCount=0;
+      $scope.showNotification = true;
+      // get all languages
+      var loginId = sessionStorage.getItem('userId')
+      translateService.getCurrentLang(loginId).then(res => {
+        $scope.cur_lang = res.languageAlias
+        $translate.use($scope.cur_lang);
+      })
+      translateService.getLangList().then(res => {
+        $scope.langList = res.languageList
+      })
+      // switch language
+      $scope.switching = function(lang) {
+        $translate.use(lang);
+        var langs = $scope.langList
+        var langId = ''
+        var selectedLang = langs.find(function(item) {
+          return item.languageAlias === lang;
+        });
+        langId = selectedLang.languageId
+        var loginId = sessionStorage.getItem('userId')
+        translateService.saveSelectedLang(loginId, {'languageId': langId}).then(res => {
+          window.location.reload();
+        })
+      }
+      $scope.cur_lang = $translate.use();
 
             $scope.hideMenus = false;
 
             
             userProfileService.getFunctionalMenuStaticInfo()
             .then(res=> {
-               // $log.debug('HeaderCtrl::getFunctionalMenuStaticInfo: getting Functional Menu Static Info init');
                if(res==null || res.firstName==null || res.firstName=='' || res.lastName==null || res.lastName=='' ){
-                       // $log.info('HeaderCtrl::getFunctionalMenuStaticInfo: failed getting userinfo from shared context.. ');
                        $log.info('HeaderCtrl: failed to get all required data, trying user profile');
                        userProfileService.getUserProfile()
                     .then(profile=> {
-                       // $log.debug('HeaderCtrl:: getting userinfo from session success');
                         this.firstName = profile.firstName;
                         this.lastName = profile.lastName;                     
-                        // $log.debug('HeaderCtrl::getFunctionalMenuStaticInfo: user has the following roles: ' + profile.roles);
                     }).catch(err=> {
                         $log.error('Header Controller:: getUserProfile() failed: ' + err);
                     });
                } else {
-                       // $log.debug('HeaderCtrl: fetched Functional Menu Static Info successfully',res);
                        this.firstName = res.firstName;
                        this.lastName = res.lastName;                                     
                }
 
             $scope.loadFavorites = function () {
                 $scope.hideMenus = false;
-                // $log.debug('HeaderCtrl::loadFavorites: loadFavorites has happened.');
                 if ($scope.favoritesMenuItems == '') {
                     generateFavoriteItems();
-                    // $log.debug('HeaderCtrl::loadFavorites: loadFavorites is calling generateFavoriteItems()');
-                } else {
-                    // $log.debug('HeaderCtrl::loadFavorites: loadFavorites is NOT calling generateFavoriteItems()');
                 }
             }
 
             $scope.goToUrl = (item) =>  {
-               //  $log.error('HeaderCtrl::goToUrl has started',item);
                 let url = item.url;
                 let restrictedApp = item.restrictedApp;
                 if (!url) {
                     $window.open(url, '_blank');
                 } else {
                        if(item.url=="getAccess" || item.url=="contactUs"){
-                           // if (url = window.location.href)
                                $state.go("root."+url);
                        } else {
                        var tabContent = { id: new Date(), title: item.text, url: item.url,appId:item.appid };
                        $cookies.putObject('addTab', tabContent );
                     }
-                    // $log.debug('HeaderCtrl::goToUrl: url = ', url);
                 }
                 $scope.hideMenus = true;
             }
             
             
             
-            $scope.submenuLevelAction = function(index, column) {
-                if ($scope.favoritesMenuItems == '') {
-                    generateFavoriteItems();
-                    // $log.debug('HeaderCtrl::submenuLevelAction: submenuLevelAction is calling generateFavoriteItems()');
-                } else {
-                    // $log.debug('submenuLevelAction is NOT calling generateFavoriteItems()');
-                }
-                // $log.debug('item hovered: ' + index + '; column = ' + column);
-                // if (column == 2) {  // 2 is Design
-                //     // This is an admitted hack. See aw3218 for reasons why
-                //     $log.debug('submenuLevelAction column == 2');
-                //     $scope.favoritesWindow = false;
-                //     $scope.showFavorites = false;
-                //     $scope.emptyFavorites = false;
-                // }
+            $scope.submenuLevelAction = function(index, column) {           
                 if (index=='Favorites' && $scope.favoriteItemsCount != 0) {
-                    // $log.debug('HeaderCtrl::submenuLevelAction: Showing Favorites window');
-                    // generateFavoriteItems();
                     $scope.favoritesWindow = true;
                     $scope.showFavorites = true;
                     $scope.emptyFavorites = false;
                 }
                 if (index=='Favorites' && $scope.favoriteItemsCount == 0) {
-                    // $log.debug('HeaderCtrl::submenuLevelAction: Hiding Favorites window in favor of No Favorites Window');
-                    // generateFavoriteItems();
                     $scope.favoritesWindow = true;
                     $scope.showFavorites = false;
                     $scope.emptyFavorites = true;
                     $scope.showFavorites = false;
                     $scope.emptyFavorites = false;
                 }
-
             };
             
             $scope.hideFavoritesWindow = function() {
                 $scope.showFavorites = false;
                 $scope.emptyFavorites = false;
-                // $scope.thirdFourthMenus = true;
             }
             
             $scope.isUrlFavorite = function (menuId) {
-                // $log.debug('array objects in menu favorites = ' + $scope.favoriteItemsCount + '; menuId=' + menuId);
                 var jsonMenu =  JSON.stringify($scope.favoritesMenuItems);
                 var isMenuFavorite =  jsonMenu.indexOf('menuId\":' + menuId);
-                // $log.debug('jsonMenu.indexOf(menuId:' + jsonMenu.indexOf('menuId\":'+menuId));
-                // $log.debug('isMenuFavorite= ' + isMenuFavorite);
                 if (isMenuFavorite==-1) {
                     return false;
                 } else {
                     return true;
                 }
-
             }
             
             /*Getting Ecomp portal Title*/
             let generateFavoriteItems  = () => {
                 menusService.getFavoriteItems()
                     .then(favorites=> {
-                        // $log.debug('HeaderCtrl.getFavoriteItems:: ' + JSON.stringify(favorites));
                         $scope.favoritesMenuItems = favorites;
                         $scope.favoriteItemsCount = Object.keys(favorites).length;
-                        // $log.info('HeaderCtrl.getFavoriteItems:: number of favorite menus: ' + $scope.favoriteItemsCount);
                     }).catch(err=> {
                         $log.error('HeaderCtrl.getFavoriteItems:: Error retrieving Favorites menus: ' + err);
                 });
 
            $scope.setAsFavoriteItem =  function(event, menuId){
                var jsonMenuID = angular.toJson({'menuId': + menuId });
-               // $log.debug('HeaderCtrl::setFavoriteItems: ' + jsonMenuID  + " - " +  event.target.id);
-
                menusService.setFavoriteItem(jsonMenuID)
                .then(() => {
-                   // var elementId = '#'+ event.currentTarget.id;
                    angular.element('#' + event.target.id).css('color', '#fbb313');
                    generateFavoriteItems();
                }).catch(err=> {
            };
 
             $scope.removeAsFavoriteItem =  function(event, menuId){
-                // $log.debug('-----------------------------removeAsFavoriteItem: ' + menuId + " - " +  event.target.id);
                 menusService.removeFavoriteItem(menuId)
                 .then(() => {
                     angular.element('#' + event.target.id).css('color', '#666666');
                     data: ''
                 }).closePromise.then(needUpdate => {
                     if(needUpdate.value === true){
-                        // $log.debug('AdminsCtrl:openAddNewAdminModal:: updating table data...');
                         updateTableData();
                     }
                 });
             try {
                userProfileService.getFunctionalMenuStaticInfo()
                 .then(res=> {
-                 // $log.info('HeaderCtrl::LoginSnippetCtrl: Login information: ' + JSON.stringify(res));
                  $scope.firstName = res.firstName;
                  $scope.lastName = res.lastName;
                  $scope.loginSnippetEmail = res.email;
                  $scope.loginSnippetUserid = res.userId;
-                 $scope.lastLogin = res.last_login;
+                 $scope.lastLogin = Date.parse(res.last_login);
                 }).catch(err=> {
                  $log.error('HeaderCtrl::LoginSnippetCtrl: failed in getFunctionalMenuStaticInfo: ' + err);
                 });
                        var userapprole ={
                                App:res[i].appName,
                                Roles:res[i].roleNames, 
-                       };
-                       
+                       };                      
                        $scope.userapproles.push(userapprole); 
-                       }
+                        }
                         
                });
                
     
     NotificationCtrl.$inject = ['$log', '$scope', '$cookies', '$timeout', 'sessionService','notificationService','$interval','ngDialog','$modal'];
     LoginSnippetCtrl.$inject = ['$log', '$scope', '$cookies', '$timeout','userProfileService', 'sessionService'];
-    HeaderCtrl.$inject = ['$log', '$window', 'userProfileService', 'menusService', '$scope', 'ECOMP_URL_REGEX','$cookies','$state','auditLogService','notificationService','ngDialog','$modal'];
+    HeaderCtrl.$inject = ['$log', '$window', '$translate', 'translateService', 'userProfileService', 'menusService', '$scope', 'ECOMP_URL_REGEX','$cookies','$state','auditLogService','notificationService','ngDialog','$modal'];
     angular.module('ecompApp').controller('HeaderCtrl', HeaderCtrl);
     angular.module('ecompApp').controller('loginSnippetCtrl', LoginSnippetCtrl);
     angular.module('ecompApp').controller('notificationCtrl', NotificationCtrl);