[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / widget-onboarding / widget-onboarding.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 WidgetOnboardingCtrl {\r
23         constructor($log, applicationsService, widgetsCatalogService, ngDialog, confirmBoxService,\r
24                     userProfileService, $cookies, $scope) {\r
25             $scope.infoMessage = true;\r
26 \r
27             let populateAvailableApps = widgets => {\r
28                 let allPortalsFilterObject = {index: 0, title: 'All applications', value: ''};\r
29                 this.availableApps = [allPortalsFilterObject];\r
30                 this.filterByApp = this.availableApps[0];\r
31                 applicationsService.getAppsForSuperAdminAndAccountAdmin().then(myApps => {\r
32                         var reSortedApp = myApps.sort(getSortOrder("name"));\r
33                     var realAppIndex = 1;\r
34                     for (let i = 1; i <= reSortedApp.length; i++) {\r
35                         if (!reSortedApp[i-1].restrictedApp) {\r
36                             this.availableApps.push({\r
37                                 index: realAppIndex,\r
38                                 title: reSortedApp[i - 1].name,\r
39                                 value: reSortedApp[i - 1].name\r
40                             })\r
41                             realAppIndex = realAppIndex + 1;\r
42                         }\r
43                     }\r
44                 }).catch(err => {\r
45                     $log.error('WidgetOnboardingCtrl:getAppsForSuperAdmin failed', err);\r
46                 });\r
47             };\r
48                  \r
49             let getOnboardingWidgets = () => {\r
50                 this.isLoadingTable = true;\r
51                 this.isCommError = false;\r
52                 widgetsCatalogService.getManagedWidgets().then(res => {\r
53                         if(!(res instanceof Array)){\r
54                                         this.isCommError = true;\r
55                                         return;\r
56                                  }\r
57                         \r
58                     var reSortedWidget = res.sort(getSortOrder("name"));\r
59                     $scope.widgetsList = reSortedWidget;\r
60                     for(var i = 0; i < $scope.widgetsList.length; i++){\r
61                         let set = new Set();\r
62                         var info = "";\r
63                         var appContent = [];\r
64                         var appName = [];       \r
65                         for(var n = 0; n < $scope.widgetsList[i].widgetRoles.length; n++){\r
66                                 set.add($scope.widgetsList[i].widgetRoles[n].app.appName);\r
67                         }\r
68                         if($scope.widgetsList[i].allowAllUser == "Y"){\r
69                                 info = "All Applications";\r
70                                 appContent.push("All Applications");\r
71                                 appName.push("All Applications");\r
72                         }\r
73                         \r
74                         set.forEach(function (item) {\r
75                                 info = item.toString() + " - ";\r
76                                 for(var n = 0; n < $scope.widgetsList[i].widgetRoles.length; n++){\r
77                                         if(item.toString() == $scope.widgetsList[i].widgetRoles[n].app.appName){\r
78                                                 info += $scope.widgetsList[i].widgetRoles[n].roleName + "; ";\r
79                                         }\r
80                                 }\r
81                                 appContent.push(info);\r
82                                 appName.push(item.toString());\r
83                         });\r
84                         $scope.widgetsList[i].appContent = appContent;\r
85                         $scope.widgetsList[i].appName = appName;\r
86                         }\r
87                     populateAvailableApps(reSortedWidget);\r
88                 }).catch(err => {\r
89                         // Land here when the micro service is down\r
90                     $log.error('WidgetOnboardingCtrl::getOnboardingWidgets caught error', err);\r
91                 }).finally(()=> {\r
92                     this.isLoadingTable = false;\r
93                 });\r
94              \r
95             };\r
96             \r
97             \r
98             // Refactor this into a directive\r
99             let getSortOrder = (prop) => {\r
100                 return function(a, b) {\r
101                     if (a[prop].toLowerCase() > b[prop].toLowerCase()) {\r
102                         return 1;\r
103                     } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {\r
104                         return -1;\r
105                     }\r
106                     return 0;\r
107                 }\r
108             }\r
109 \r
110             $scope.hideMe = function () {\r
111                 $scope.infoMessage = false;\r
112             }\r
113 \r
114             let init = () => {\r
115                 this.isLoadingTable = false;\r
116                 getOnboardingWidgets();\r
117                 this.searchString = '';\r
118                 this.widgetsTableHeaders = [\r
119                     {name: 'Widget Name', value: 'name', isSortable: false}\r
120                 ];\r
121                 $scope.widgetsList = [];\r
122             };\r
123            \r
124             this.filterByDropdownValue = item => {              \r
125                 if(this.filterByApp.value === '')\r
126                     return true;\r
127                 \r
128                 for(var i = 0; i < item.appName.length; i++){\r
129                         if(item.appName[i] == this.filterByApp.value\r
130                         || item.appName[i] == 'All Applications'){\r
131                                 return true;\r
132                         }\r
133                 }\r
134                 return false;\r
135             };\r
136 \r
137             this.openWidgetCatalogDetailsModal = (selectedWidget) => {\r
138                 let data = null;\r
139                 if(selectedWidget){\r
140                     if(!selectedWidget.id){\r
141                         $log.error('WidgetOnboardingCtrl:openWidgetCatalogDetailModal: widget id not found');\r
142                         return;\r
143                     }\r
144                     data = {\r
145                         widget: selectedWidget\r
146                     }\r
147                 }\r
148                 ngDialog.open({\r
149                     templateUrl: 'app/views/widget-onboarding/widget-details-dialog/widget-details.modal.html',\r
150                     controller: 'WidgetOnboardingDetailsModalCtrl',\r
151                     controllerAs: 'widgetOnboardingDetails',\r
152                     data: data\r
153                 }).closePromise.then(needUpdate => {\r
154                         if(needUpdate.value === true){\r
155                         getOnboardingWidgets();\r
156                     }\r
157                 });\r
158             };\r
159 \r
160             this.deleteWidget = widget => { \r
161                 \r
162                confirmBoxService.deleteItem(widget.name).then(isConfirmed => {\r
163                         if(isConfirmed){\r
164                         if(!widget || !widget.id){\r
165                             $log.error('WidgetOnboardingCtrl::deleteWidget: No widget or ID... cannot delete');\r
166                             return;\r
167                         }\r
168                         widgetsCatalogService.deleteWidget(widget.id).then(() => {\r
169                                 $scope.widgetsList.splice($scope.widgetsList.indexOf(widget), 1);\r
170                         }).catch(err => {\r
171                             $log.error('WidgetOnboardingCtrl::deleteWidget error:',err);\r
172                         });\r
173                     }\r
174                 }).catch(err => {\r
175                     $log.error('WidgetOnboardingCtrl::deleteWidget error:',err);\r
176                 });\r
177                 \r
178             };\r
179             \r
180             \r
181             this.downloadWidget = widget => {\r
182                         widgetsCatalogService.downloadWidgetFile(widget.id).then(res => {\r
183                                 var data = res;\r
184                                 var filename = widget.name + ".zip";\r
185                                 \r
186                                 if (data == undefined || data == null){\r
187                                         confirmBoxService.showInformation("Could not download. Please retry.");\r
188                                         return;                 \r
189                                 }\r
190                                 var a = document.createElement('a');\r
191                                 var blob = new Blob([data], {type: 'application/octet-stream'});\r
192                                 a.href = URL.createObjectURL(blob);\r
193                                 a.download = filename;\r
194                                 a.click();\r
195                         });\r
196             };\r
197             \r
198             init();\r
199         }\r
200     }\r
201     WidgetOnboardingCtrl.$inject = ['$log', 'applicationsService', 'widgetsCatalogService', 'ngDialog', 'confirmBoxService',\r
202         'userProfileService','$cookies', '$scope'];\r
203     angular.module('ecompApp').controller('WidgetOnboardingCtrl', WidgetOnboardingCtrl);\r
204 })();\r