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