[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / dashboard / dashboard.controller.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 'use strict';\r
21 \r
22 function _classCallCheck(instance, Constructor) {\r
23         if (!(instance instanceof Constructor)) {\r
24                 throw new TypeError('Cannot call a class as a function');\r
25         }\r
26 }\r
27 \r
28 (function() {\r
29         var HTTP_PROTOCOL_RGX = /https?:\/\//;\r
30 \r
31         var DashboardCtrl = function DashboardCtrl(conf, applicationsService, $log,\r
32                         $window, userProfileService, $scope, $cookies, $timeout, $interval, \r
33                         $modal, $state, beReaderService, dashboardService, confirmBoxService, \r
34                         auditLogService,ngDialog, $compile, widgetsCatalogService) {\r
35 \r
36                 this.conf = conf;       \r
37                 var _this = this;\r
38 \r
39                 _classCallCheck(this, DashboardCtrl);\r
40 \r
41                 // activate spinner\r
42                 this.isLoading = true;\r
43                 this.isCommError = false;\r
44                 $scope.getUserAppsIsDone = false;\r
45                 this.userProfileService = userProfileService;\r
46                 $scope.demoNum = 1;\r
47                 $scope.event_content_show = false;\r
48                 $scope.widgetData = [];\r
49                 $scope.activateThis = function(ele){\r
50                          $compile(ele.contents())($scope);\r
51                          $scope.$apply();\r
52                 };\r
53 \r
54                 $scope.editWidgetModalPopup = function(availableData, resourceType) {\r
55                         $scope.editData = JSON.stringify(availableData);\r
56                         $scope.availableDataTemp = $scope.availableData;\r
57                         ngDialog.open({\r
58                                         templateUrl : 'app/views/dashboard/dashboard-widget-manage.html',\r
59                                         controller : 'CommonWidgetController',\r
60                                         resolve : {\r
61                                                 message : function message() {\r
62                                                         var message = {\r
63                                                                 type : resourceType,\r
64                                                                 availableData : $scope.editData\r
65                                                         };\r
66                                                         return message;\r
67                                                 }\r
68                                         }\r
69                                 }).closePromise.then(needUpdate => {    \r
70                                         if(resourceType=='NEWS'){\r
71                                                 $scope.updateNews();\r
72                                         }else if(resourceType=='EVENTS'){\r
73                                                 $scope.updateEvents();\r
74                                         }else if(resourceType=='IMPORTANTRESOURCES'){\r
75                                                 $scope.updateImportRes();\r
76                                         }\r
77                   });           \r
78                 };\r
79                 \r
80                 $scope.editWidgetParameters = function(widgetId) {\r
81                         let data = {\r
82                 widgetId: widgetId\r
83             }\r
84                         ngDialog.open({\r
85                                         templateUrl : 'app/views/dashboard/dashboard-widget-parameter-manage.html',\r
86                                         controller : 'WidgetParameterController',\r
87                                         data: data\r
88                                 }).closePromise.then(needUpdate => {    \r
89                                                 \r
90                   });           \r
91                 };\r
92                 \r
93                 $scope.sort_options = [        \r
94                                        {index: 0, value: 'N', title: 'Name'},\r
95                                        {index: 1, value: 'L', title: 'Last used'},\r
96                                        {index: 2, value: 'F', title: 'Most used'},\r
97                                        {index: 3, value: 'M', title: 'Manual'}                                 \r
98                                    ]; \r
99                 \r
100                 $scope.selectedSortTypeChanged = function(userAppSortTypePref) {\r
101                         $scope.appsViewData = [];\r
102                         $scope.appsView = [];\r
103                         \r
104                         $scope.sort_type = userAppSortTypePref;\r
105                         \r
106                         applicationsService\r
107                         .getAppsOrderBySortPref(userAppSortTypePref)\r
108                                 .then(function(res) {\r
109                                         _this.apps = res;                       \r
110                                         $scope.applyPresentationDetailsToApps(_this.apps);                              \r
111                         })      \r
112                         applicationsService\r
113                         .saveAppsSortTypePreference($scope.selectedSortType)\r
114                                 .then(function(res) {\r
115                                         // Nothing to do\r
116                                 })\r
117                 \r
118                 }\r
119                 $scope.$watch('selectedSortType.value', (newVal, oldVal) => {\r
120                         for(var i=0;i<$scope.sort_options.length;i++){\r
121                         if($scope.sort_options[i].value==newVal){\r
122                         $scope.selectedSortType=angular.copy($scope.sort_options[i]);;\r
123                         }\r
124                         }\r
125                         });\r
126                 \r
127                 $scope.restoreSortSelected = function(){\r
128                         confirmBoxService.confirm("Restore the default size and position of all widgets?").then(\r
129                                 function(confirmed){\r
130                                         var checkConfirm = confirmed;\r
131                                         if(checkConfirm === true){\r
132                                         applicationsService\r
133                                         .delWidgetsSortPref($scope.widgetsViewData).then(function(){\r
134                                                 $state.reload();\r
135                                         });\r
136                                         }\r
137                                 });\r
138                 /*      if(confirm('Restore the default size and position of all widgets?') == true)\r
139                                 {\r
140                         applicationsService\r
141                         .delWidgetsSortPref($scope.widgetsViewData).then(function(){\r
142                                 $state.reload();\r
143                         })\r
144                                 }*/\r
145                                 \r
146                 }\r
147                 \r
148                 $scope.applyPresentationDetailsToApps = function(appsReturned) {\r
149                         var rowNo = 0;\r
150                         for (var i = 0; i < _this.apps.length; i++) {\r
151                                 $scope.appsView[i] = {\r
152                                         sizeX : 1,\r
153                                         sizeY : 1,\r
154                                         headerText : '',\r
155                                         subHeaderText : '',\r
156                                         imageLink : '',\r
157                                         order : '',\r
158                                         url : '',\r
159                                         appid: '',\r
160                                 };\r
161                                 $scope.appsView[i].headerText = appsReturned[i].name;\r
162                                 $scope.appsView[i].subHeaderText = appsReturned[i].notes;\r
163                                 let imgLnk = '';\r
164                                 if (appsReturned[i].imageUrl)\r
165                                         imgLnk = conf.api.appThumbnail.replace(':appId', appsReturned[i].id);\r
166                                 //$log.debug('DashboardCtlr::applyPresn: imgLink = ' + imgLnk);\r
167                                 $scope.appsView[i].imageLink = imgLnk;\r
168                                 $scope.appsView[i].order = appsReturned[i].order;\r
169                                 $scope.appsView[i].url = appsReturned[i].url;\r
170                                 $scope.appsView[i].restrictedApp = appsReturned[i].restrictedApp;\r
171                                 $scope.appsView[i].appid = appsReturned[i].id;\r
172                         }\r
173                         $scope.appsView[_this.apps.length] = {\r
174                                         addRemoveApps : true,\r
175                                         sizeX : 1,\r
176                                         sizeY : 1,\r
177                                         headerText : 'Add/Remove Applications',\r
178                                         subHeaderText : '',\r
179                                         imageLink : 'assets/images/cloud.png',\r
180                                         order : '',\r
181                                         restrictedApp : false,\r
182                                         url : '',\r
183                         };\r
184                         if($scope.appsView.length>6){                                                                                                   \r
185                                 $(".dashboard-boarder").css({\r
186                                         "height" : "400px"\r
187                                 });\r
188                         }else{\r
189                                 $(".dashboard-boarder").css({\r
190                                         "height" : "210px"\r
191                                 });\r
192                         }\r
193                         \r
194                         if ($scope.appsView != undefined\r
195                                         && $scope.appsView != null\r
196                                         && $scope.appsView.length > 0) {\r
197                                 $scope.appsViewData = $scope.appsView;\r
198                         }\r
199                 }\r
200                 \r
201 $scope.widgetsView = [];\r
202                 \r
203                 $scope.applyPresentationDetailsToWidgets = function(widgetsReturned){\r
204                  var rowNo = 0;\r
205                  for (var i = 0; i < widgetsReturned.length; i++) {\r
206                                 $scope.widgetsView[i] = {\r
207                                         sizeX : '',\r
208                                         sizeY :'',\r
209                                         headerText:'',\r
210                                         widgetText:'',\r
211                                         widgetIdentifier : '',\r
212                                         url : '',\r
213                                         widgetid: '',\r
214                                         attrb:'',\r
215                                         row:'',\r
216                                         col:'',\r
217                                 };\r
218                                 $scope.widgetsView[i].widgetid = widgetsReturned[i].id;\r
219                                 $scope.widgetsView[i].headerText = widgetsReturned[i].headerName;\r
220                                 $scope.widgetsView[i].widgetText = widgetsReturned[i].name;\r
221                                 \r
222                                 if(widgetsReturned[i].headerName.toLowerCase() === 'news'){\r
223                                         $scope.widgetsView[i].widgetIdentifier = 'NEWS';\r
224                                 }\r
225                                 else\r
226                                 if(widgetsReturned[i].headerName.toLowerCase() === 'resources'){\r
227                                         $scope.widgetsView[i].widgetIdentifier = 'IMPORTANTRESOURCES';\r
228                                 }\r
229                                 else\r
230                                 if(widgetsReturned[i].headerName.toLowerCase() === 'events'){\r
231                                         $scope.widgetsView[i].widgetIdentifier = 'EVENTS';\r
232                                 }\r
233 \r
234                                 $scope.widgetsView[i].url = widgetsReturned[i].url;\r
235                                 $scope.widgetsView[i].attrb = widgetsReturned[i].attrs;\r
236                                 if(widgetsReturned[i].width === null){\r
237                                         $scope.widgetsView[i].sizeX = 2;\r
238                                 }else{\r
239                                         $scope.widgetsView[i].sizeX = widgetsReturned[i].width;\r
240                                 }\r
241                                 if(widgetsReturned[i].height === null){\r
242                                         $scope.widgetsView[i].sizeY = 2;\r
243                                 }else{\r
244                                         $scope.widgetsView[i].sizeY = widgetsReturned[i].height;\r
245                                 }\r
246                                 $scope.widgetsView[i].row = widgetsReturned[i].x;\r
247                                 $scope.widgetsView[i].col = widgetsReturned[i].y;\r
248                         }\r
249                 if ($scope.widgetsView != undefined\r
250                                 && $scope.widgetsView != null\r
251                                 && $scope.widgetsView.length > 0) {\r
252                         $scope.widgetsViewData = $scope.widgetsView;\r
253                 }\r
254                 }\r
255 \r
256         applicationsService\r
257         .getUserAppsSortTypePreference().then(function(res) {\r
258                         var resJson = {};\r
259                     resJson.value = res;\r
260                         if (resJson.value==="N" || resJson.value==="") {\r
261                                 resJson.index = 0;\r
262                                 resJson.title = 'Name';\r
263                         }else if (resJson.value==="L") {\r
264                                 resJson.index = 1;\r
265                                 resJson.title = 'Last used';\r
266                         }else if(resJson.value==="F"){\r
267                                 resJson.index = 2;\r
268                                 resJson.title = 'Most used';\r
269                         }else {\r
270                                 resJson.index = 3;\r
271                                 resJson.title = 'Manual';\r
272                         }       \r
273                         $scope.selectedSortType = resJson;\r
274                         $scope.selectedSortTypeChanged(res);\r
275 \r
276                 \r
277                 });\r
278                 \r
279         $scope.widgetsList = [];\r
280         \r
281         let getUserWidgets = (loginName) => {\r
282          var conf = this.conf;\r
283                  widgetsCatalogService.getUserWidgets(loginName).then(res => {\r
284                          if(!(res instanceof Array)){\r
285                                  this.isCommError = true;\r
286                                  return;\r
287                          }\r
288                          for(var i = 0; i < res.length; i++){    \r
289                                  var widget_id = res[i][0];\r
290                                  var widget_name = res[i][1];\r
291                                  let url = this.conf.api.widgetCommon + "/" + widget_id + "/framework.js";\r
292                                  var header_name = widget_name;\r
293                                  if(res[i][7] == 1){\r
294                                         header_name = (widget_name.length > 9) ?widget_name.substring(0, 8) + '...' : widget_name;\r
295                                  }\r
296                                  if(res[i][4] === "S" || res[i][4] === null ){\r
297                                  $scope.widgetsList.push({\r
298                                            id: widget_id,\r
299                                            headerName: header_name,\r
300                                    name: widget_name,\r
301                                    url: url,\r
302                                    attrs: [{attr: 'data-' + res[i][0], value: ''}],\r
303                                    x: res[i][3], \r
304                                    y: res[i][5],\r
305                                    height: res[i][6],\r
306                                    width: res[i][7]\r
307                                  });\r
308                                  }\r
309                                  var script = document\r
310                                          .createElement('script');\r
311                                          script.src = url;\r
312                                          script.async = false;\r
313                                          var entry = document\r
314                                                 .getElementsByTagName('script')[0];\r
315                                          entry.parentNode\r
316                                                 .insertBefore(script, entry);\r
317                          } \r
318                 $scope.applyPresentationDetailsToWidgets($scope.widgetsList);\r
319         }).catch(err => {\r
320             $log.error('WidgetsHomeCtrl::getUserWidgets error: ' + err);\r
321         }).finally(()=> {\r
322          \r
323         });\r
324    };\r
325 \r
326       userProfileService.getUserProfile().then(\r
327                         function(profile) {\r
328                                 $scope.orgUserId = profile.orgUserId;\r
329                                 getUserWidgets($scope.orgUserId);\r
330           });\r
331                 \r
332                 /* Widget Gridster Section */\r
333                 $scope.newsGridsterItem = {\r
334                         sizeX : 1,\r
335                         sizeY : 1,\r
336                         headerText : 'News',\r
337                         subHeaderText : '',\r
338                         imageLink : '',\r
339                         order : '',\r
340                         url : ''\r
341                 };\r
342 \r
343                 $scope.eventsGridsterItem = {\r
344                         sizeX : 1,\r
345                         sizeY : 1,\r
346                         headerText : 'Events',\r
347                         subHeaderText : '',\r
348                         imageLink : '',\r
349                         order : '',\r
350                         url : ''\r
351                 };\r
352 \r
353                 $scope.impoResGridsterItem = {\r
354                         sizeX : 1,\r
355                         sizeY : 1,\r
356                         headerText : 'Resources',\r
357                         subHeaderText : '',\r
358                         imageLink : '',\r
359                         order : '',\r
360                         url : ''\r
361                 };\r
362                 \r
363                 this.gridsterAppOpts = {\r
364                                 columns : 6,\r
365                                 colWidth : 190,\r
366                                 rowHeight : 190,\r
367                                 margins : [ 20, 20 ],\r
368                                 outerMargin : true,\r
369                                 pushing : true,\r
370                                 floating : true,\r
371                                 swapping : true,\r
372                                 resizable: {\r
373                                         enabled: false,\r
374                                 },\r
375                                 draggable : {\r
376                                         handle:'.icon-content-gridguide',\r
377                                         stop: function stop() { \r
378                                                 $scope.defaultSortBy = function() {\r
379                                                         var resJson = {};\r
380                                                         resJson.value = 'M';\r
381                                                         resJson.index = 3;\r
382                                                         resJson.title = 'Manual';\r
383                                                         $scope.selectedSortType = resJson;\r
384                                                         applicationsService.saveAppsSortTypeManual($scope.appsViewData) \r
385                                                         applicationsService.saveAppsSortTypePreference($scope.selectedSortType)\r
386                                                 }\r
387                                                 $scope.defaultSortBy();                 \r
388                                         }\r
389                                 }\r
390                         };\r
391                         \r
392                         this.gridsterWidgetOpts = {\r
393                                         columns : 6,\r
394                                         colWidth : 190,\r
395                                         rowHeight : 190,\r
396                                         margins : [ 20, 20 ],\r
397                                         outerMargin : true,\r
398                                         pushing : true,\r
399                                         floating : true,\r
400                                         swapping : true,\r
401                                         resizable: {\r
402                                                 enabled: true,\r
403                                                 stop: function stop(event, uiWidget, $element){\r
404                                                         if($element.sizeX == 1)\r
405                                                                 $element.headerText = ($element.widgetText.length > 9) ? $element.widgetText.substring(0, 8) + '...' : $element.widgetText;\r
406                                                         if($element.sizeX >= 2)\r
407                                                                 $element.headerText = $element.widgetText;\r
408                                                         \r
409                                                         applicationsService\r
410                                                         .saveWidgetsSortManual($scope.widgetsViewData)\r
411                                         \r
412                                                 }\r
413                                         },\r
414                                         draggable : {\r
415                                                 handle:'.icon-content-gridguide',\r
416                                                 stop: function stop(){\r
417                                                         applicationsService\r
418                                                         .saveWidgetsSortManual($scope.widgetsViewData)\r
419                                         \r
420                                                 }\r
421                                         }\r
422                         };\r
423                 \r
424                 this.goToCatalog = function(item) {\r
425                         $state.go('root.appCatalog');\r
426                 } \r
427                 \r
428                 this.goToWidgetCatLog = function(item) {\r
429                         $state.go('root.widgetCatalog');\r
430                 } \r
431                 \r
432                 // navigate to application url in new tab\r
433                 this.goToPortal = function(item) {\r
434                         if (!item.url) {\r
435                                 $log.error('No URL found for this application, doing nothing!');\r
436                                 return;\r
437                         }\r
438                         if (item.restrictedApp) {\r
439                                 // Link-based apps open in their own browser tab\r
440                                 $window.open(item.url, '_blank');\r
441                         } else {\r
442                                 // cache control so browsers load app page every time\r
443                                 var ccParam = 'cc=' + new Date().getTime();\r
444                                 var urlParts = item.url.split('#');\r
445                                 var appUrl = null;\r
446                                 if (urlParts.length < 2) {\r
447                                         // no #\r
448                                         let urlLastChar = item.url.charAt(item.url.length - 1);\r
449                                         if (item.url.includes("?"))\r
450                                                 appUrl = (urlLastChar === '&' ? item.url + ccParam : item.url + '&' + ccParam);\r
451                                         else \r
452                                                 appUrl = item.url + '?' + ccParam;\r
453                                 } else {\r
454                                         // has #\r
455                                         let urlLastChar = urlParts[0].charAt(urlParts[0].length - 1);\r
456                                         if (item.url.includes("?"))\r
457                                                 appUrl = (urlLastChar === '&' ? urlParts[0] + ccParam + '#' + urlParts[1]  :  urlParts[0] + '&' + ccParam + '#' + urlParts[1]);\r
458                                         else\r
459                                                 appUrl = urlParts[0] + '?' + ccParam + "#" + urlParts[1];\r
460                                 }\r
461                                 // $log.debug('DashboardCtrlr::goToPortal: opening tab with URL\r
462                                 // ' + appUrl);\r
463                                 var tabContent = {\r
464                                         id: new Date(),\r
465                                         title: item.headerText,\r
466                                         url: appUrl,\r
467                                         appId: item.appId\r
468                                 };\r
469                                 $cookies.putObject('addTab', tabContent);\r
470                         }\r
471 \r
472                 };\r
473                 \r
474                 this.auditLog = function(app) {\r
475                         console.log(app);\r
476                         auditLogService.storeAudit(app.appid,'app',app.url);\r
477                 };\r
478                 \r
479                 if (getParameterByName('noUserError') != null) {\r
480                         if (getParameterByName('noUserError') == "Show") {\r
481                                 $("#errorInfo").show();\r
482                         }\r
483                 }               \r
484         };\r
485 \r
486         DashboardCtrl.$inject = [ 'conf', 'applicationsService', '$log', '$window',\r
487                         'userProfileService', '$scope', '$cookies', '$timeout', '$interval', \r
488                         '$modal',  '$state', 'beReaderService', 'dashboardService', 'confirmBoxService', \r
489                         'auditLogService', 'ngDialog', '$compile', 'widgetsCatalogService' ];\r
490         angular.module('ecompApp').controller('DashboardCtrl', DashboardCtrl);\r
491 })();\r
492 \r
493 function getParameterByName(name, url) {\r
494         if (!url)\r
495                 url = window.location.href;\r
496         name = name.replace(/[\[\]]/g, "\\$&");\r
497         var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex\r
498                         .exec(url);\r
499         if (!results)\r
500                 return '';\r
501         if (!results[2])\r
502                 return '';\r
503         return results[2].replace(/\+/g, " ");\r
504 }\r