[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / widgets / widgets.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 (function () {\r
22     class WidgetsCtrl {\r
23         constructor($log, applicationsService, widgetsService, ngDialog, confirmBoxService,\r
24                     userProfileService, $cookies, $scope, $rootScope) {\r
25             //$log.info('WidgetsCtrl::init: Starting Up');\r
26             $scope.infoMessage = true;\r
27             $rootScope.noWidgets = false;\r
28 \r
29             let populateAvailableApps = widgets => {\r
30                 let allPortalsFilterObject = {index: 0, title: 'All applications', value: ''};\r
31                 this.availableApps = [allPortalsFilterObject];\r
32                 this.filterByApp = this.availableApps[0];\r
33                 applicationsService.getAppsForSuperAdminAndAccountAdmin().then(myApps => {\r
34                     var reSortedApp = myApps.sort(getSortOrder("name"));\r
35                     var realAppIndex = 1;\r
36                     for (let i = 1; i <= reSortedApp.length; i++) {\r
37                         if (!reSortedApp[i-1].restrictedApp) {\r
38                             $log.debug('WidgetsCtrl::populateAvailableApps: pushing {index: ', realAppIndex, 'title: ', reSortedApp[i - 1].name,\r
39                                 'value: ', reSortedApp[i - 1].name, '}');\r
40                             this.availableApps.push({\r
41                                 index: realAppIndex,\r
42                                 title: reSortedApp[i - 1].name,\r
43                                 value: reSortedApp[i - 1].name\r
44                             })\r
45                             realAppIndex = realAppIndex + 1;\r
46                         }\r
47                     }\r
48                 }).catch(err => {\r
49                     $log.error(err);\r
50                 });\r
51             };\r
52 \r
53             let getOnboardingWidgets = () => {\r
54                 this.isLoadingTable = true;\r
55                 widgetsService.getManagedWidgets().then(res => {\r
56                     $log.debug('WidgetsCtrl.getOnboardingWidgets:: ' + JSON.stringify(res));\r
57                     // if (JSON.stringify(res) === '[]') {\r
58                     //     confirmBoxService.showInformation('There are currently no Widgets. ').then(isConfirmed => {});\r
59                     // }\r
60                     var reSortedWidget = res.sort(getSortOrder("name"));\r
61                     this.widgetsList = reSortedWidget;\r
62                     populateAvailableApps(reSortedWidget);\r
63                     // $log.info('WidgetsHomeCtrl::getUserWidgets count : ' + $scope.widgetsList.length);\r
64                     if (Object.keys(res).length === 0 ) {\r
65                         $rootScope.noWidgets = true;\r
66                         $scope.isLoadingTable = false;\r
67                         $log.info('WidgetsHomeCtrl::getUserWidgets: There are no available Widgets');\r
68                     }\r
69                 }).catch(err => {\r
70                     confirmBoxService.showInformation('There was a problem retrieving the Widgets. ' +\r
71                         'Please try again later.').then(isConfirmed => {});\r
72                     $log.error('WidgetsCtrl::getOnboardingWidgets error: ' + err);\r
73                 }).finally(()=> {\r
74                     this.isLoadingTable = false;\r
75                 });\r
76             };\r
77 \r
78             // Refactor this into a directive\r
79             let getSortOrder = (prop) => {\r
80                 return function(a, b) {\r
81                     // $log.debug('a = ' + JSON.stringify(a) + "| b = " + JSON.stringify(b));\r
82                     if (a[prop].toLowerCase() > b[prop].toLowerCase()) {\r
83                         return 1;\r
84                     } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {\r
85                         return -1;\r
86                     }\r
87                     return 0;\r
88                 }\r
89             }\r
90 \r
91             $scope.hideMe = function () {\r
92                 $scope.infoMessage = false;\r
93             }\r
94 \r
95             let init = () => {\r
96                 this.isLoadingTable = false;\r
97                 getOnboardingWidgets();\r
98 \r
99                 /*Table general configuration params*/\r
100                 this.searchString = '';\r
101                 /*Table data*/\r
102                 this.widgetsTableHeaders = [\r
103                     {name: 'Widget Name', value: 'name', isSortable: false},\r
104                     {name: 'Application', value: 'appName', isSortable: true},\r
105                     {name: 'Width', value: 'width', isSortable: false},\r
106                     {name: 'Height', value: 'height', isSortable: false}\r
107                 ];\r
108                 this.widgetsList = [];\r
109             };\r
110 \r
111             this.filterByDropdownValue = item => {\r
112                 if(this.filterByApp.value === ''){\r
113                     return true;\r
114                 }\r
115                 return item.appName === this.filterByApp.value;\r
116             };\r
117 \r
118             this.openWidgetDetailsModal = (selectedWidget) => {\r
119                 let data = null;\r
120                 if(selectedWidget){\r
121                     if(!selectedWidget.id){\r
122                         $log.error('Widget id not found');\r
123                         return;\r
124                     }\r
125                     data = {\r
126                         widget: selectedWidget\r
127                     }\r
128                 }\r
129                 ngDialog.open({\r
130                     templateUrl: 'app/views/widgets/widget-details-dialog/widget-details.modal.html',\r
131                     controller: 'WidgetDetailsModalCtrl',\r
132                     controllerAs: 'widgetDetails',\r
133                     data: data\r
134                 }).closePromise.then(needUpdate => {\r
135                     if(needUpdate.value === true){\r
136                         $log.debug('WidgetsCtrl::openWidgetDetailsModal: updating table data...');\r
137                         getOnboardingWidgets();\r
138                     }\r
139                 });\r
140             };\r
141 \r
142             this.deleteWidget = widget => {\r
143                 confirmBoxService.deleteItem(widget.name).then(isConfirmed => {\r
144                     if(isConfirmed){\r
145                         if(!widget || !widget.id){\r
146                             $log.error('WidgetsCtrl::deleteWidget: No widget or ID... cannot delete');\r
147                             return;\r
148                         }\r
149                         widgetsService.deleteWidget(widget.id).then(() => {\r
150                             this.widgetsList.splice(this.widgetsList.indexOf(widget), 1);\r
151                         }).catch(err => {\r
152                             $log.error('WidgetsCtrl::deleteWidget error:',err);\r
153                             confirmBoxService.showInformation('There was a problem deleting the Widget. ' +\r
154                                 'Please try again later.').then(isConfirmed => {});\r
155                         });\r
156                     }\r
157                 }).catch(err => {\r
158                     $log.error('WidgetsCtrl::deleteWidget error:',err);\r
159                 });\r
160             };\r
161 \r
162             init();\r
163         }\r
164     }\r
165     WidgetsCtrl.$inject = ['$log', 'applicationsService', 'widgetsService', 'ngDialog', 'confirmBoxService',\r
166         'userProfileService','$cookies', '$scope', '$rootScope'];\r
167     angular.module('ecompApp').controller('WidgetsCtrl', WidgetsCtrl);\r
168 })();\r