Replace ecomp references
[portal.git] / ecomp-portal-FE-common / client / app / views / widget-onboarding / widget-onboarding.controller.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 'use strict';
39 (function () {
40     class WidgetOnboardingCtrl {
41         constructor($log, applicationsService, widgetsCatalogService, ngDialog, confirmBoxService,
42                     userProfileService, $cookies, $scope,$modal) {
43             $scope.infoMessage = true;
44
45             let populateAvailableApps = widgets => {
46                 let allPortalsFilterObject = {index: 0, title: 'All Applications', value: ''};
47                 this.availableApps = [allPortalsFilterObject];
48                 this.filterByApp = this.availableApps[0];
49                 applicationsService.getAppsForSuperAdminAndAccountAdmin().then(myApps => {
50                         var reSortedApp = myApps.sort(getSortOrder("name"));
51                     var realAppIndex = 1;
52                     for (let i = 1; i <= reSortedApp.length; i++) {
53                         if (!reSortedApp[i-1].restrictedApp) {
54                             this.availableApps.push({
55                                 index: realAppIndex,
56                                 title: reSortedApp[i - 1].name,
57                                 value: reSortedApp[i - 1].name
58                             })
59                             realAppIndex = realAppIndex + 1;
60                         }
61                     }
62                 }).catch(err => {
63                     $log.error('WidgetOnboardingCtrl:getAppsForSuperAdmin failed', err);
64                 });
65             };
66                  
67             let getOnboardingWidgets = () => {
68                 this.isLoadingTable = true;
69                 this.isCommError = false;
70                 widgetsCatalogService.getManagedWidgets().then(res => {
71                         if(!(res instanceof Array)){
72                                         this.isCommError = true;
73                                         return;
74                                  }
75                         
76                     var reSortedWidget = res.sort(getSortOrder("name"));
77                     $scope.widgetsList = reSortedWidget;
78                     for(var i = 0; i < $scope.widgetsList.length; i++){
79                         let set = new Set();
80                         var info = "";
81                         var appContent = [];
82                         var appName = [];       
83                         for(var n = 0; n < $scope.widgetsList[i].widgetRoles.length; n++){
84                                 set.add($scope.widgetsList[i].widgetRoles[n].app.appName);
85                         }
86                         if($scope.widgetsList[i].allowAllUser == "Y"){
87                                 info = "All Applications";
88                                 appContent.push("All Applications");
89                                 appName.push("All Applications");
90                         }
91                         
92                         set.forEach(function (item) {
93                                 info = item.toString() + " - ";
94                                 for(var n = 0; n < $scope.widgetsList[i].widgetRoles.length; n++){
95                                         if(item.toString() == $scope.widgetsList[i].widgetRoles[n].app.appName){
96                                                 info += $scope.widgetsList[i].widgetRoles[n].roleName + "; ";
97                                         }
98                                 }
99                                 appContent.push(info);
100                                 appName.push(item.toString());
101                         });
102                         $scope.widgetsList[i].appContent = appContent;
103                         $scope.widgetsList[i].appName = appName;
104                         }
105                     populateAvailableApps(reSortedWidget);
106                 }).catch(err => {
107                         // Land here when the micro service is down
108                     $log.error('WidgetOnboardingCtrl::getOnboardingWidgets caught error', err);
109                 }).finally(()=> {
110                     this.isLoadingTable = false;
111                 });
112              
113             };
114             
115             
116             // Refactor this into a directive
117             let getSortOrder = (prop) => {
118                 return function(a, b) {
119                     if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
120                         return 1;
121                     } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
122                         return -1;
123                     }
124                     return 0;
125                 }
126             }
127
128             $scope.hideMe = function () {
129                 $scope.infoMessage = false;
130             }
131
132             let init = () => {
133                 this.isLoadingTable = false;
134                 getOnboardingWidgets();
135                 this.searchString = '';
136                 this.widgetsTableHeaders = [
137                     {name: 'Widget Name', value: 'name', isSortable: false}
138                 ];
139                 $scope.widgetsList = [];
140             };
141            
142             this.filterByDropdownValue = item => {              
143                 if(this.filterByApp.value === '' || this.filterByApp.value === 'All Applications')
144                     return true;
145                 
146                 for(var i = 0; i < item.appName.length; i++){
147                         if(item.appName[i] == this.filterByApp.value
148                         || item.appName[i] == 'All Applications'){
149                                 return true;
150                         }
151                 }
152                 return false;
153             };
154
155             this.openWidgetCatalogDetailsModal = (selectedWidget) => {
156                 let data = null;
157                 if(selectedWidget){
158                     if(!selectedWidget.id){
159                         $log.error('WidgetOnboardingCtrl:openWidgetCatalogDetailModal: widget id not found');
160                         return;
161                     }
162                     data = {
163                         widget: selectedWidget
164                     }
165                 }
166                 var modalInstance = $modal.open({
167                     templateUrl: 'app/views/widget-onboarding/widget-details-dialog/widget-details.modal.html',
168                     controller: 'WidgetOnboardingDetailsModalCtrl as widgetOnboardingDetails',
169                     sizeClass: 'modal-medium', 
170                     resolve: {
171                                         items: function () {
172                                   return data;
173                                         }
174                         }
175                 })
176                 
177                 modalInstance.result.finally(function () {
178                                 getOnboardingWidgets();
179                 });
180             };
181
182             this.deleteWidget = widget => { 
183                 
184                confirmBoxService.deleteItem(widget.name).then(isConfirmed => {
185                         if(isConfirmed){
186                         if(!widget || !widget.id){
187                             $log.error('WidgetOnboardingCtrl::deleteWidget: No widget or ID... cannot delete');
188                             return;
189                         }
190                         widgetsCatalogService.deleteWidget(widget.id).then(() => {
191                                 $scope.widgetsList.splice($scope.widgetsList.indexOf(widget), 1);
192                         }).catch(err => {
193                             $log.error('WidgetOnboardingCtrl::deleteWidget error:',err);
194                         });
195                     }
196                 }).catch(err => {
197                     $log.error('WidgetOnboardingCtrl::deleteWidget error:',err);
198                 });
199                 
200             };
201             
202             
203             this.downloadWidget = widget => {
204                         widgetsCatalogService.downloadWidgetFile(widget.id).then(res => {
205                                 var data = res;
206                                 var filename = widget.name + ".zip";
207                                 
208                                 if (data == undefined || data == null){
209                                         confirmBoxService.showInformation("Could not download. Please retry.");
210                                         return;                 
211                                 }
212                                 var a = document.createElement('a');
213                                 var blob = new Blob([data], {type: 'application/octet-stream'});
214                                 var url = window.URL.createObjectURL(blob);
215                                 a.href = url;
216                                 a.download = filename;
217                                 document.body.appendChild(a);
218                                 a.click();
219                                 
220                                 setTimeout(function(){
221                                 document.body.removeChild(a);
222                                 window.URL.revokeObjectURL(url);  
223                             }, 100);  
224                             
225                         });
226             };
227             
228             init();
229         }
230     }
231     WidgetOnboardingCtrl.$inject = ['$log', 'applicationsService', 'widgetsCatalogService', 'ngDialog', 'confirmBoxService',
232         'userProfileService','$cookies', '$scope','$modal'];
233     angular.module('ecompApp').controller('WidgetOnboardingCtrl', WidgetOnboardingCtrl);
234 })();