nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / views / dashboard / dashboard.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
22 function _classCallCheck(instance, Constructor) {
23         if (!(instance instanceof Constructor)) {
24                 throw new TypeError('Cannot call a class as a function');
25         }
26 }
27
28 (function() {
29         var HTTP_PROTOCOL_RGX = /https?:\/\//;
30
31         var DashboardCtrl = function DashboardCtrl(applicationsService, $log,
32                         $window, userProfileService, $scope, $cookies, $timeout, $interval,
33                         $modal, $state, dashboardService,ngDialog) {
34
35                 var _this = this;
36
37                 _classCallCheck(this, DashboardCtrl);
38
39                 // activate spinner
40                 this.isLoading = true;
41                 $scope.getUserAppsIsDone = false;
42                 this.userProfileService = userProfileService;
43                 $scope.demoNum = 1;
44                 $scope.event_content_show = false;
45                 $scope.widgetData = [];
46
47                 $scope.editWidgetModalPopup = function(availableData, resourceType) {
48                         $scope.editData = JSON.stringify(availableData);
49                         $scope.availableDataTemp = $scope.availableData;
50                         ngDialog.open({
51                                         templateUrl : 'app/views/dashboard/dashboard-widget-manage.html',
52                                         controller : 'CommonWidgetController',
53                                         resolve : {
54                                                 message : function message() {
55                                                         var message = {
56                                                                 type : resourceType,
57                                                                 availableData : $scope.editData
58                                                         };
59                                                         return message;
60                                                 }
61                                         }
62                                 }).closePromise.then(needUpdate => {    
63                                         if(resourceType=='NEWS'){
64                                                 $scope.updateNews();
65                                         }else if(resourceType=='EVENTS'){
66                                                 $scope.updateEvents();
67                                         }else if(resourceType=='IMPORTANTRESOURCES'){
68                                                 $scope.updateImportRes();
69                                         }
70                   });           
71                 };
72
73                 userProfileService.getUserProfile()
74                         .then(
75                                 function(profile) {
76                                         // $log.info(profile);
77                                         $scope.attuid = profile.attuid;
78                                         // $log.info('user has the following attuid: ' +
79                                         // profile.attuid);
80                                         $scope.appsView = [];
81                                         
82                                         applicationsService.getPersUserApps()
83                                                 .then(
84                                                         function(res) {
85                                                         // $log.info(res);
86                                                         _this.apps = res;
87                                                         
88                                                         for (var i = 0; i < _this.apps.length; i++) {
89                                                                 $scope.appsView[i] = {
90                                                                         sizeX                   : 2,
91                                                                         sizeY                   : 2,
92                                                                         headerText              : _this.apps[i].name,
93                                                                         subHeaderText   : _this.apps[i].notes,
94                                                                         imageLink               : _this.apps[i].thumbnail || _this.apps[i].imageUrl,
95                                                                         order                   : _this.apps[i].order,
96                                                                         restrictedApp   : _this.apps[i].restrictedApp,
97                                                                         url                     : _this.apps[i].url
98                                                                 };
99                                                         }
100                                                         
101                                                         // Append the show add/remove applications tile
102                                                         $scope.appsView[_this.apps.length] = {
103                                                                         addRemoveApps : true,
104                                                                         sizeX : 2,
105                                                                         sizeY : 2,
106                                                                         headerText : 'Add/Remove Applications',
107                                                                         subHeaderText : '',
108                                                                         imageLink : 'assets/images/cloud.png',
109                                                                         order : '',
110                                                                         restrictedApp : false,
111                                                                         url : ''
112                                                         };
113                 
114                                                         // $log.info('getUserApps apps count : ' +
115                                                         // $scope.appsView.length);
116                                                         // Show 2 rows in the gridster if needed
117                                                         if ($scope.appsView.length > 6) {                                                                                                       
118                                                                 $(".dashboard-boarder").css({
119                                                                 "height" : "400px"
120                                                                         });
121                                                                 }else{
122                                                                         $(".dashboard-boarder").css({
123                                                                         "height" : "210px"
124                                                                 });
125                                                                 }
126                                                                 _this.isLoading = false;
127                                                                 $scope.getUserAppsIsDone = true;                                                                                                                                                                                                        
128                                                         })['catch']
129                                         (function(err) {
130                                                 $log.error('DashboardCtlr: failed to get applications list', err);
131                                                 _this.isLoading = false;
132                                                 $scope.getUserAppsIsDone = true;
133                                         });
134                 });
135
136
137                 /* Widget Gridster Section */
138                 $scope.newsGridsterItem = {
139                         sizeX : 4,
140                         sizeY : 4,
141                         headerText : 'News',
142                         subHeaderText : '',
143                         imageLink : '',
144                         order : '',
145                         url : ''
146                 };
147
148                 $scope.eventsGridsterItem = {
149                         sizeX : 4,
150                         sizeY : 4,
151                         headerText : 'Calendar Events',
152                         subHeaderText : '',
153                         imageLink : '',
154                         order : '',
155                         url : ''
156                 };
157
158                 $scope.impoResGridsterItem = {
159                         sizeX : 4,
160                         sizeY : 4,
161                         headerText : 'Resources',
162                         subHeaderText : '',
163                         imageLink : '',
164                         order : '',
165                         url : ''
166                 };
167                 this.gridsterOpts = {
168                         columns : 12,
169                         colWidth : 95,
170                         rowHeight : 95,
171                         margins : [ 20, 20 ],
172                         outerMargin : true,
173                         pushing : true,
174                         floating : true,
175                         swapping : true,
176                         draggable : {
177                                 handle: 'img'
178                         }
179                 };
180                 
181                 this.emptyGridsterOpts = {
182                                 columns : 24,
183                                 colWidth : 190,
184                                 rowHeight : 190,
185                                 margins : [ 20, 20 ],
186                                 outerMargin : true,
187                                 pushing : true,
188                                 floating : true,
189                                 swapping : true,
190                                 draggable : {
191                                         handle: 'img'
192                                 }
193                         };
194
195                 this.goToCatalog = function(item) {
196                         $state.go('root.appCatalog');
197                 } 
198                 
199                 // navigate to application url in new tab
200                 this.goToPortal = function(item) {
201                         if (!item.url) {
202                                 $log.error('No URL found for this application, doing nothing!');
203                                 return;
204                         }
205                         if (item.restrictedApp) {
206                                 // Link-based apps open in their own browser tab
207                                 $window.open(item.url, '_blank');
208                         } else {
209                                 // cache control so browsers load app page every time
210                                 var ccParam = '?cc=' + new Date().getTime();
211                                 var appUrl = null;
212                                 var urlParts = item.url.split('#');
213                                 if (urlParts.length < 2)
214                                         appUrl = item.url + ccParam;
215                                 else
216                                         appUrl = urlParts[0] + ccParam + "#" + urlParts[1];
217                                 // $log.debug('DashboardCtrlr::goToPortal: opening tab with URL
218                                 // ' + appUrl);
219                                 var tabContent = {
220                                         id : new Date(),
221                                         title : item.headerText,
222                                         url : appUrl
223                                 };
224                                 $cookies.putObject('addTab', tabContent);
225                         }
226                 };
227                 
228                 /* News data */
229                 $scope.newsData = [];
230                 $scope.updateNews = function() {
231                         $scope.newsData.length=0;
232                         dashboardService.getCommonWidgetData('NEWS').then(function(res) {
233                                 // $log.info(res.message);
234                                 var newsDataJsonArray = res.response.items;
235                                 for (var i = 0; i < newsDataJsonArray.length; i++) {
236                                         $scope.newsData.push(newsDataJsonArray[i]);
237                                 }
238                         })['catch'](function(err) {
239                                 $log.error('dashboard controller: failed to get news list', err);
240                                 _this.isLoading = false;
241                         });
242                 }
243                 $scope.updateNews();
244                 
245                 /* Events data */
246                 $scope.eventData = [];
247                 $scope.updateEvents = function() {
248                         $scope.eventData.length=0;
249                         dashboardService.getCommonWidgetData('EVENTS').then(function(res) {
250                                 var eventDataJsonArray = res.response.items;    
251                                 for (var i = 0; i < eventDataJsonArray.length; i++) {
252                                         if(eventDataJsonArray[i].eventDate !=null) {
253                                                 // yyyy-mm-dd
254                                                 eventDataJsonArray[i].year = eventDataJsonArray[i].eventDate.substring(2,4);
255                                                 eventDataJsonArray[i].mon  = eventDataJsonArray[i].eventDate.substring(5,7);
256                                                 eventDataJsonArray[i].day  = eventDataJsonArray[i].eventDate.substring(8,10);
257                                         }
258                                         $scope.eventData.push(eventDataJsonArray[i]);
259                                 }
260                         })['catch'](function(err) {
261                                 $log.error('dashboard controller: failed to get Events list', err);
262                                 _this.isLoading = false;
263                         });
264                 }
265                 $scope.updateEvents();
266                 
267                 /* Important Resources data */
268                 $scope.importResData = [];
269                 $scope.updateImportRes = function() {
270                         $scope.importResData.length=0;
271                         dashboardService.getCommonWidgetData('IMPORTANTRESOURCES').then(
272                                         function(res) {
273                                                 // $log.error('couldnt get important list...',
274                                                 // res.response.dataBlob);
275                                                 // $log.info(res);
276                                                 var importResDataJSONArray = res.response.items;
277                                                 for (var i = 0; i < importResDataJSONArray.length; i++) {
278                                                         $scope.importResData.push(importResDataJSONArray[i]);
279                                                 }
280                                         })['catch'](function(err) {
281                                 $log.error('dashboard controller: failed to get resources list...', err);
282                                 _this.isLoading = false;
283                         });
284                 }
285                 $scope.updateImportRes();
286
287                 if (getParameterByName('noUserError') != null) {
288                         if (getParameterByName('noUserError') == "Show") {
289                                 $("#errorInfo").show();
290                         }
291                 }
292                 
293         };
294
295         DashboardCtrl.$inject = [ 'applicationsService', '$log', '$window',
296                         'userProfileService', '$scope', '$cookies', '$timeout',
297                         '$interval', '$modal',  '$state', 'dashboardService','ngDialog'];
298         angular.module('ecompApp').controller('DashboardCtrl', DashboardCtrl);
299 })();
300
301 function getParameterByName(name, url) {
302         if (!url)
303                 url = window.location.href;
304         name = name.replace(/[\[\]]/g, "\\$&");
305         var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex
306                         .exec(url);
307         if (!results)
308                 return '';
309         if (!results[2])
310                 return '';
311         return results[2].replace(/\+/g, " ");
312 }