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