nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / views / home / applications-home / applications-home.controller.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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 'use strict';
21 (function () {
22     const HTTP_PROTOCOL_RGX = /https?:\/\//;
23     class  ApplicationsHomeCtrl {
24         constructor(applicationsService, $log, $window, userProfileService, $scope,$cookies, utilsService) {
25                         //activate spinner
26             this.isLoading = true;
27             $scope.getUserAppsIsDone = false;
28             this.userProfileService = userProfileService;
29             //get all user's applications on init
30             $scope.buildNumber = "OpenECOMP Portal Version: 1.0.0"
31
32             userProfileService.getUserProfile()
33                 .then(profile=> {
34                     $log.info('ApplicationsHomeCtrl::getUserProfile', profile);
35                     $scope.userId = profile.orgUserId;
36                     //$scope.appsViewData = ['notempty']; // initialize with length != 0
37                     $scope.appsViewData = [];
38                     $scope.appsView = [];
39
40                     //redirect to login.htm, if the EPService cookie is missing and logged in user is not a guest.
41                     if (!$cookies.get('EPService') && !profile.guestSession) {
42                         this.isLoading = false;
43                         var myHostName;
44                         myHostName = location.host;
45                         $log.info("EPService cookie is missing, so attempting to redirecting to login page.");
46                         if (utilsService.isRunningInLocalDevEnv()) {
47                             $log.info("Portal is running in local development and redirecting to 'http://localhost:8080/ecompportal/login.htm'.");
48                             $window.location.href = "http://localhost:8080/ecompportal/login.htm";
49                         } else {
50                             $log.info("Redirecting to 'login.htm'.");
51                             $window.location.href = "login.htm";
52                         }
53                     }
54
55                     applicationsService.getUserApps()
56                     .then(res => {
57                         $log.info('ApplicationsHomeCtrl::getUserApps: ', res);
58                         this.apps = res;
59                         let rowNo = 0;
60                         for (let i = 0; i < this.apps.length; i++) {
61                             $scope.appsView[i] = {
62                                 sizeX: 2,
63                                 sizeY: 2,
64                                 headerText: '',
65                                 subHeaderText: '',
66                                 imageLink: '',
67                                 order: '',
68                                 url: ''
69                             };
70                             $scope.appsView[i].headerText = this.apps[i].name;
71                             $scope.appsView[i].subHeaderText = this.apps[i].notes;
72                             $scope.appsView[i].imageLink = this.apps[i].thumbnail || this.apps[i].imageUrl;
73                             $scope.appsView[i].order = this.apps[i].order;
74                             $scope.appsView[i].url = this.apps[i].url;
75                             $scope.appsView[i].restrictedApp = this.apps[i].restrictedApp;
76                         }
77                         $log.info('ApplicationsHomeCtrl::getUserApps:  apps count : ' + $scope.appsView.length);
78
79                         if ($cookies.getObject($scope.userId+'_apps') == undefined || $cookies.getObject($scope.userId+'_apps') == null || $cookies.getObject($scope.userId+'_apps').length == 0) {
80                             if (($scope.appsView != undefined) && ($scope.appsView != null) && ($scope.appsView.length > 0)){
81                                 $scope.appsViewData = $scope.appsView;
82                                 $cookies.putObject($scope.userId + '_apps', $scope.appsView);
83                             }
84                         }
85                         else{
86                             this.listChanged = false;
87                             this.appsListFromCookie = $cookies.getObject($scope.userId+'_apps');
88                             this.finalAppsList = [];
89                             //
90                             // If an app is still valid for this user from backend(appsView) and
91                             // it was in the cookie already, put it in the list in the same order
92                             // it was in within the cookie.
93                             //
94                             let finalCount = 0;
95                             for (let i = 0; i < this.appsListFromCookie.length; i++) {
96                                 this.foundAppInListFromBackend = false;
97                                 for (let j = 0; j < $scope.appsView.length; j++) {
98                                     if (this.appsListFromCookie[i].url == $scope.appsView[j].url) {
99                                         this.finalAppsList[finalCount] = {
100                                             sizeX: 2,
101                                             sizeY: 2,
102                                             headerText: '',
103                                             subHeaderText: '',
104                                             imageLink: '',
105                                             order: '',
106                                             url: ''
107                                         };
108                                         this.finalAppsList[finalCount].headerText = this.appsListFromCookie[i].headerText;
109                                         this.finalAppsList[finalCount].subHeaderText = this.appsListFromCookie[i].subHeaderText;
110                                         this.finalAppsList[finalCount].imageLink = this.appsListFromCookie[i].imageLink;
111                                         this.finalAppsList[finalCount].order = this.appsListFromCookie[i].order;
112                                         this.finalAppsList[finalCount].url = this.appsListFromCookie[i].url;
113                                         this.finalAppsList[finalCount].restrictedApp = this.appsListFromCookie[i].restrictedApp;
114                                         finalCount++;
115                                         this.foundAppInListFromBackend = true;
116                                         break;
117                                     }
118                                 }
119                                 if (this.foundAppInListFromBackend == false) {
120                                     this.listChanged = true;
121                                 }
122                             }
123
124                             //
125                             // Fill in the rest of the list with the Apps retrieved from the backend that we did not already add.  There could have been
126                             // new Apps configured for the user that are not in the cookie.
127                             //
128                             for (let i = 0; i < $scope.appsView.length; i++) {
129                                 this.found = false;
130                                 for (let j = 0; j < this.finalAppsList.length; j++) {
131                                     if ($scope.appsView[i].url == this.finalAppsList[j].url) {
132                                         // already present
133                                         this.found = true;
134                                         break;
135                                     }
136                                 }
137                                 if (this.found == false) {
138                                     this.finalAppsList[finalCount] = {
139                                         sizeX: 2,
140                                         sizeY: 2,
141                                         headerText: '',
142                                         subHeaderText: '',
143                                         imageLink: '',
144                                         order: '',
145                                         url: ''
146                                     };
147                                     this.finalAppsList[finalCount].headerText = $scope.appsView[i].headerText;
148                                     this.finalAppsList[finalCount].subHeaderText = $scope.appsView[i].subHeaderText;
149                                     this.finalAppsList[finalCount].imageLink = $scope.appsView[i].imageLink;
150                                     this.finalAppsList[finalCount].order = $scope.appsView[i].order;
151                                     this.finalAppsList[finalCount].url = $scope.appsView[i].url;
152                                     this.finalAppsList[finalCount].restrictedApp = $scope.appsView[i].restrictedApp;
153                                     finalCount++;
154                                     this.listChanged = true;
155                                 }
156                             }
157
158                             if ((this.finalAppsList != undefined) && (this.finalAppsList != null) && (this.finalAppsList.length > 0)) {
159                                 if (this.listChanged) {
160                                     $scope.appsViewData = this.finalAppsList;
161                                     $cookies.putObject($scope.userId + '_apps', this.finalAppsList);
162                                 } else {
163                                     $scope.appsViewData = $cookies.getObject($scope.userId+'_apps');
164                                 }
165                             }
166                             this.isLoading = false;
167                             $scope.getUserAppsIsDone = true;
168                         }
169                     }).catch(err => {
170                         $log.error('oh no! couldnt get applications list...', err);
171                         this.isLoading = false;
172                         $scope.getUserAppsIsDone = true;
173                     });
174                 });
175
176     
177             this.gridsterOpts = {
178                 columns: 6,
179                 colWidth: 190,
180                 rowHeight: 190,
181                 margins: [20, 20],
182                 outerMargin: true,
183                 pushing: true,
184                 floating: true,
185                 swapping: true,
186                 draggable : {
187                     stop: function () {
188                         $cookies.putObject($scope.userId + '_apps', $scope.appsViewData);
189                     }
190                 }
191             };
192
193             //navigate to application url in new tab
194             this.goToPortal = (item) => {
195                 $log.info("goToPortal called with item: " + item);
196                 let url = item.url;
197                 let restrictedApp = item.restrictedApp;
198                 if (!url) {
199                     $log.info('No url found for this application, doing nothing..');
200                     return;
201                 }
202                 if (!HTTP_PROTOCOL_RGX.test(url)) {
203                     url = 'http://' + url;
204                 }
205                 if (restrictedApp) {
206                     $window.open(url, '_blank');
207                 } else {
208                     var tabContent = { id: new Date(), title: item.headerText, url:item.url.split('#')[0] + '?' + (new Date).getTime() + "#" + item.url.split('#')[1]};
209                         $cookies.putObject('addTab', tabContent );
210                 }
211
212             };
213             // try {
214             //     userProfileService.getUserProfile()
215             //         .then(profile=> {
216                         // if (profile.roles.indexOf('superAdmin') > -1) {
217                         //     $scope.buildNumber = "ECOMP Portal Version: 1610.0.2058"
218                         // }
219             //         }).catch(err=> {
220             //             $log.error('Applications Home:: getUserProfile() failed: ' + err);
221             //     });
222             // } catch (err) {
223             //     $log.error('Applications Home:: getUserProfile() failed: ' + err);
224             // }
225             if(getParameterByName('noUserError')!=null){
226                  if(getParameterByName('noUserError')=="Show"){
227                           $("#errorInfo").show();
228                  }
229             }
230         }
231     }
232     ApplicationsHomeCtrl.$inject = ['applicationsService', '$log', '$window', 'userProfileService', '$scope','$cookies', 'utilsService'];
233     angular.module('ecompApp').controller('ApplicationsHomeCtrl', ApplicationsHomeCtrl);
234 })();
235
236 function getParameterByName(name, url) {
237     if (!url) url = window.location.href;
238     name = name.replace(/[\[\]]/g, "\\$&");
239     var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
240         results = regex.exec(url);
241     if (!results) return '';
242     if (!results[2]) return '';
243     return (results[2].replace(/\+/g, " "));
244 }