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