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