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