[PORTAL-20,PORTAL-23,PORTAL-32] Repair defects
[portal.git] / ecomp-portal-FE-common / client / app / views / catalog / catalog.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
22 function _classCallCheck(instance, Constructor) {
23         if (!(instance instanceof Constructor)) {
24                 throw new TypeError('Cannot call a class as a function');
25         }
26 }
27
28 (function() {
29         var HTTP_PROTOCOL_RGX = /https?:\/\//;
30
31         var CatalogCtrl = function CatalogCtrl(conf, catalogService, confirmBoxService, ExternalRequestAccessService,
32                         $log, $window, userProfileService, applicationsService, $scope, $state, 
33                         $timeout, $interval, $modal, ngDialog) {
34
35                 this.conf = conf;       
36                 var _this = this;
37                 var externalRequest = true;
38                 _classCallCheck(this, CatalogCtrl);
39
40                 // activate spinner
41                 this.isLoading = true;
42                 $scope.getAppCatalogIsDone = false;
43                 $scope.radioValue = 'All';
44                 $scope.$watch('radioValue', function(newValue, oldValue) {
45                         var appCatalog = $scope.appCatalog;
46                         $scope.appCatalog = [];
47                         $scope.appCatalog = appCatalog;
48
49                         
50                 });
51                 
52                 this.catalogService = catalogService;
53                 this.userProfileService = userProfileService;
54                 this.applicationsService = applicationsService;
55                 var resultAccessValue = null;
56                 $scope.demoNum = 1;
57                 $scope.appRoles = [];
58                 
59         let init = () => {
60             getExternalAccess();
61         };
62         
63                 this.getAccess = function(item) {
64                         if(!item.access)
65                                 confirmBoxService.showDynamicInformation(item,
66                                         'app/views/catalog/information-box.tpl.html','CatalogConfirmationBoxCtrl'
67                                 ).then(isConfirmed => {});  
68                 };
69                 
70         var getExternalAccess = () => {
71                 ExternalRequestAccessService.getExternalRequestAccessServiceInfo().then(
72                                 function(property) {
73                 resultAccessValue = property.accessValue;
74                                         // $log.info(res);
75                                 }).catch(err => {
76                         $log.error('CatalogCtrl: failed getExternalRequestAccessServiceInfo: ' + JSON.stringify(err));
77                     });    
78         };
79         
80
81         
82                 $scope.applyPresentationDetailsToAppsCatalog = function(res, value) {
83                         
84                         _this.apps = res;
85                         var rowNo = 0;
86
87                         // defend against error string result -
88                         // a huge list that should never happen.
89                         var maxItems = 333;
90                         if (_this.apps.length < maxItems)
91                                 maxItems = _this.apps.length;
92                         for (var i = 0; i < maxItems; i++) {                                                                                            
93                                 let imgLnk = '';
94                                 if (_this.apps[i].imageUrl)
95                                         imgLnk = conf.api.appThumbnail.replace(':appId', _this.apps[i].id);
96                                 //$log.debug('CatalogCtlr::applyPresn: imgLink = ' + imgLnk);
97                                 $scope.appCatalog[i] = {
98                                         sizeX : 2,
99                                         sizeY : 2,
100                                         id : _this.apps[i].id,
101                                         headerText : _this.apps[i].name,
102                                         mlAppName: _this.apps[i].mlAppName,
103                                         imageLink : imgLnk,
104                                         restricted : _this.apps[i].restricted,  
105                                         select : _this.apps[i].select,
106                                         access : _this.apps[i].access,
107                                         pending: _this.apps[i].pending,
108                                         mlproperty: value
109                                 };
110                         }
111                         //$log.debug('CatalogCtrl: getAppCatalog count : '
112                         //                              + $scope.appCatalog.length);
113                         _this.isLoading = false;
114                         $scope.getAppCatalogIsDone = true;
115                 }
116                 
117         let getAppsCatalog = () => {
118             catalogService.getAppCatalog()
119                 .then(appsList => {
120                         $scope.applyPresentationDetailsToAppsCatalog(appsList);
121                 }).catch(err => {
122                     confirmBoxService.showInformation('There was a problem retrieving the Applications. ' +
123                         'Please try again later. Error Status: '+ err.status).then(isConfirmed => {});
124                     $log.error('CatalogCtrl:openAddRoleModal: Error: ', err);
125                 });
126         };
127         
128         this.openAddRoleModal = (item) => { 
129                 let data = null;
130
131             if((!item.restricted) && (item.mlproperty)){
132                 data = {
133                     dialogState: 2,
134                     selectedUser:{
135                         orgUserId: $scope.orgUserId,
136                         firstName: $scope.firstName,
137                         lastName: $scope.lastName,
138                         headerText: item.headerText,
139                         haloAppName : item.mlAppName,
140                         item: item,
141                         
142                     }
143                 }
144             ngDialog.open({
145                 templateUrl: 'app/views/catalog/request-access-catalog-dialogs/request-access-catalog.modal.html',
146                 controller: 'ExternalRequestAccessCtrl',
147                 controllerAs: 'userInfo',
148                 data: data
149             }).closePromise.then(needUpdate => {
150                 if(needUpdate.value === true){
151                         getAppsCatalog();
152                 }
153                 
154             });
155           }
156         };
157
158                 // Run this function when user clicks on checkbox.
159                 this.storeSelection = function(item) {
160                         // $log.debug('CatalogCtrl:storeSelection: item.id is ' + item.id + ', select is ' + item.select);
161                         var pendingFlag = false;
162                         
163                         if(item.access) 
164                                 pendingFlag = false;
165                         else
166                                 pendingFlag =  item.pending;
167                                                         
168                         var appData = { 
169                                         appId   : item.id,
170                                         select  : item.select,
171                                         pending : pendingFlag   // TODO
172                         };
173                         
174                         catalogService.updateManualAppSort(appData).then(
175                                         function(result) {
176                                                 // $log.debug('CatalogCtrl:storeSelection result is ', result);
177                                         })['catch'](function(err) {
178                                                 $log.error('CatalogCtrl:storeSelection: exception: ', err);
179                                         });
180                         catalogService.updateAppCatalog(appData).then(
181                                 function(result) {
182                                         // $log.debug('CatalogCtrl:storeSelection result is ', result);
183                                 })['catch'](function(err) {
184                                         $log.error('CatalogCtrl:storeSelection: exception: ', err);
185                                 });
186                 };
187                 
188                 userProfileService
189                                 .getUserProfile()
190                                 .then(
191                                                 function(profile) {
192                                                         $scope.orgUserId = profile.orgUserId;
193                                                         $scope.firstName = profile.firstName;
194                                                         $scope.lastName = profile.lastName;
195                                                         $scope.appCatalog = [];
196                                                         
197                                                         catalogService.getAppCatalog().then(
198                                                                                         function(res) { 
199                                                                         $scope.applyPresentationDetailsToAppsCatalog(res, resultAccessValue);
200                                                                                         })['catch'](function(err) {
201                                                                         $log.error('CatalogCtrl: failed getAppCatalog: ', JSON.stringify(err));
202                                                                         _this.isLoading = false;
203                                                                         $scope.getAppCatalogIsDone = true;
204                                                   });
205                                                 });
206
207                                                         // applicationsService.getUserApps()
208
209
210                 this.gridsterOpts = {
211                         columns : 12,
212                         colWidth : 95,
213                         rowHeight : 95,
214                         margins : [ 20, 20 ],
215                         outerMargin : true,
216                         pushing : true,
217                         floating : true,
218                         swapping : true,
219                         resizable: {
220                                 enabled: false,
221                         },
222                 };
223
224                 if (getParameterByName('noUserError') != null) {
225                         if (getParameterByName('noUserError') == "Show") {
226                                 $("#errorInfo").show();
227                         }
228
229                 }
230                 
231                 init();
232         };
233
234         CatalogCtrl.$inject = [ 'conf', 'catalogService', 'confirmBoxService', 'ExternalRequestAccessService', '$log',
235                         '$window', 'userProfileService', 'applicationsService', '$scope', 
236                         '$state', '$timeout', '$interval', '$modal', 'ngDialog' ];
237         angular.module('ecompApp').controller('CatalogCtrl', CatalogCtrl);
238 })();
239
240 function getParameterByName(name, url) {
241         if (!url)
242                 url = window.location.href;
243         name = name.replace(/[\[\]]/g, "\\$&");
244         var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex
245                         .exec(url);
246         if (!results)
247                 return '';
248         if (!results[2])
249                 return '';
250         return results[2].replace(/\+/g, " ");
251 }