[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-os / client / src / views / applications / applications.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 ApplicationsCtrl {
23
24         constructor($log, $cookies, conf, ngDialog,
25                                 applicationsService, confirmBoxService, userProfileService, utilsService) {
26             this.emptyImgForPreview = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
27             let getOnboardingApps = () => {
28                 this.isLoadingTable = true;
29                 applicationsService.getOnboardingApps()
30                     .then(appsList => {
31                         // Use proper back-end URL to load image
32                         for (var i = 0; i < appsList.length; i++) {
33                                 appsList[i].imageLink = '';
34                                 if (appsList[i].imageUrl)
35                                         appsList[i].imageLink = conf.api.appThumbnail.replace(':appId', appsList[i].id);
36                         }
37                         this.appsList = appsList;
38                     }).catch(err => {
39                         confirmBoxService.showInformation('There was a problem retrieving the Applications. ' +
40                             'Please try again later. Error Status: '+ err.status).then(isConfirmed => {});
41                         $log.error('ApplicationsCtrl:openAddNewAppModal: Error: ', err);
42                     }).finally(()=> {
43                         this.isLoadingTable = false;
44                     });
45             };
46
47             let init = () => {
48                 this.isLoadingTable = false;
49                 getOnboardingApps();
50                 this.searchString = '';
51                 this.appsTableHeaders = [
52                     {name: 'Application Name', value: 'name', isSortable: true},
53                     {name: 'Active', value: 'isEnabled', isSortable: true},
54                     {name: 'Integration Type', value: 'restrictedApp', isSortable: true},
55                     {name: 'Guest Access', value: 'isOpen', isSortable: true},
56                     {name: 'URL', value: 'url', isSortable: true},
57                     {name: 'REST URL', value: 'restUrl', isSortable: true},                
58                     {name: 'Communication Topic', value: 'uebTopicName', isSortable: true},
59                     {name: 'Communication Key', value: 'uebKey', isSortable: true},
60                     {name: 'Communication Secret', value: 'uebSecret', isSortable: true},
61                 ];
62                 this.appsList = [];
63             };
64
65             init();
66
67             this.openAddNewAppModal = (selectedApp) => {
68                 let data = null;
69                 if (selectedApp) {
70                     if (!selectedApp.id) {
71                         $log.error('App id not found');
72                         return;
73                     }
74                     data = {
75                         app: selectedApp
76                     }
77                 }
78                 ngDialog.open({
79                     templateUrl: 'app/views/applications/application-details-dialog/application-details.modal.html',
80                     controller: 'AppDetailsModalCtrl',
81                     controllerAs: 'appDetails',
82                     data: data
83                 }).closePromise.then(needUpdate => {
84                     if (needUpdate.value === true) {
85                         $log.debug('ApplicationsCtrl:openAddNewAppModal: updating table data...');
86                         getOnboardingApps();
87                     }
88                 });
89
90
91             };
92
93             this.deleteApp = application => {
94                 $log.debug('ApplicationsCtrl:deleteApp:: ', application.name);
95
96                 confirmBoxService.deleteItem(application.name).then(isConfirmed => {
97                     if(isConfirmed){
98                         if(!application || !application.id){
99                             $log.error('ApplicationsCtrl:deleteApp:: No application or ID... cannot delete');
100                             return;
101                         }
102                         applicationsService.deleteOnboardingApp(application.id).then(() => {
103                             this.appsList.splice(this.appsList.indexOf(application), 1);
104                         }).catch(err => {
105                             confirmBoxService.showInformation('There was a problem deleting the Application. ' +
106                                 'Please try again later. Error Status: '+ err.status).then(isConfirmed => {});
107                             $log.error('ApplicationsCtrl:deleteApp: Error: ', err);
108                         });
109                     }
110                 }).catch(err => {
111                     confirmBoxService.showInformation('There was a problem deleting the Application. ' +
112                         'Please try again later. Error Status: '+ err.status).then(isConfirmed => {});
113                     $log.error('ApplicationsCtrl:deleteApp error:: ', err);
114                 });
115
116             };
117
118         }
119     }
120     ApplicationsCtrl.$inject = ['$log', '$cookies', 'conf', 'ngDialog',
121                                 'applicationsService', 'confirmBoxService', 'userProfileService', 'utilsService'];
122     angular.module('ecompApp').controller('ApplicationsCtrl', ApplicationsCtrl);
123 })();