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