[PORTAL-16 PORTAL-18] Widget ms; staging
[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         var getExternalAccess = () => {
64                 ExternalRequestAccessService.getExternalRequestAccessServiceInfo().then(
65                                 function(property) {
66                 resultAccessValue = property.accessValue;
67                                         // $log.info(res);
68                                 }).catch(err => {
69                         $log.error('CatalogCtrl: failed getExternalRequestAccessServiceInfo: ' + JSON.stringify(err));
70                     });    
71         };
72         
73
74         
75                 $scope.applyPresentationDetailsToAppsCatalog = function(res, value) {
76                         
77                         _this.apps = res;
78                         var rowNo = 0;
79
80                         // defend against error string result -
81                         // a huge list that should never happen.
82                         var maxItems = 333;
83                         if (_this.apps.length < maxItems)
84                                 maxItems = _this.apps.length;
85                         for (var i = 0; i < maxItems; i++) {                                                                                            
86                                 let imgLnk = '';
87                                 if (_this.apps[i].imageUrl)
88                                         imgLnk = conf.api.appThumbnail.replace(':appId', _this.apps[i].id);
89                                 //$log.debug('CatalogCtlr::applyPresn: imgLink = ' + imgLnk);
90                                 $scope.appCatalog[i] = {
91                                         sizeX : 2,
92                                         sizeY : 2,
93                                         id : _this.apps[i].id,
94                                         headerText : _this.apps[i].name,
95                                         mlAppName: _this.apps[i].mlAppName,
96                                         imageLink : imgLnk,
97                                         restricted : _this.apps[i].restricted,  
98                                         select : _this.apps[i].select,
99                                         access : _this.apps[i].access,
100                                         pending: _this.apps[i].pending,
101                                         mlproperty: value
102                                 };
103                         }
104                         //$log.debug('CatalogCtrl: getAppCatalog count : '
105                         //                              + $scope.appCatalog.length);
106                         _this.isLoading = false;
107                         $scope.getAppCatalogIsDone = true;
108                 }
109                 
110         let getAppsCatalog = () => {
111             catalogService.getAppCatalog()
112                 .then(appsList => {
113                         $scope.applyPresentationDetailsToAppsCatalog(appsList);
114                 }).catch(err => {
115                     confirmBoxService.showInformation('There was a problem retrieving the Applications. ' +
116                         'Please try again later. Error Status: '+ err.status).then(isConfirmed => {});
117                     $log.error('CatalogCtrl:openAddRoleModal: Error: ', err);
118                 });
119         };
120         
121         this.openAddRoleModal = (item) => { 
122                 let data = null;
123
124             if((!item.restricted) && (item.mlproperty)){
125                 data = {
126                     dialogState: 2,
127                     selectedUser:{
128                         attuid: $scope.attuid,
129                         firstName: $scope.firstName,
130                         lastName: $scope.lastName,
131                         headerText: item.headerText,
132                         haloAppName : item.mlAppName,
133                         item: item,
134                         extReqValue : externalRequest
135                         
136                     }
137                 }
138             ngDialog.open({
139                 templateUrl: 'app/views/catalog/add-catalog-dialogs/new-catalog.modal.html',
140                 controller: 'NewCatalogModalCtrl',
141                 controllerAs: 'userInfo',
142                 data: data
143             }).closePromise.then(needUpdate => {
144                 if(needUpdate.value === true){
145                         getAppsCatalog();
146                 }
147                 
148             });
149           }
150         };
151
152                 // Run this function when user clicks on checkbox.
153                 this.storeSelection = function(item) {
154                         // $log.debug('CatalogCtrl:storeSelection: item.id is ' + item.id + ', select is ' + item.select);
155                         var pendingFlag = false;
156                         
157                         if(item.access) 
158                                 pendingFlag = false;
159                         else
160                                 pendingFlag =  item.pending;
161                                                         
162                         var appData = { 
163                                         appId   : item.id,
164                                         select  : item.select,
165                                         pending : pendingFlag   // TODO
166                         };
167                         
168                         catalogService.updateManualAppSort(appData).then(
169                                         function(result) {
170                                                 // $log.debug('CatalogCtrl:storeSelection result is ', result);
171                                         })['catch'](function(err) {
172                                                 $log.error('CatalogCtrl:storeSelection: exception: ', err);
173                                         });
174                         catalogService.updateAppCatalog(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                 };
181                 
182                 userProfileService
183                                 .getUserProfile()
184                                 .then(
185                                                 function(profile) {
186                                                         $scope.attuid = profile.orgUserId;
187                                                         $scope.firstName = profile.firstName;
188                                                         $scope.lastName = profile.lastName;
189                                                         $scope.appCatalog = [];
190                                                         
191                                                         catalogService.getAppCatalog().then(
192                                                                                         function(res) { 
193                                                                         $scope.applyPresentationDetailsToAppsCatalog(res, resultAccessValue);
194                                                                                         })['catch'](function(err) {
195                                                                         $log.error('CatalogCtrl: failed getAppCatalog: ', JSON.stringify(err));
196                                                                         _this.isLoading = false;
197                                                                         $scope.getAppCatalogIsDone = true;
198                                                   });
199                                                 });
200
201                                                         // applicationsService.getUserApps()
202
203
204                 this.gridsterOpts = {
205                         columns : 12,
206                         colWidth : 95,
207                         rowHeight : 95,
208                         margins : [ 20, 20 ],
209                         outerMargin : true,
210                         pushing : true,
211                         floating : true,
212                         swapping : true,
213                         resizable: {
214                                 enabled: false,
215                         },
216                 };
217
218                 if (getParameterByName('noUserError') != null) {
219                         if (getParameterByName('noUserError') == "Show") {
220                                 $("#errorInfo").show();
221                         }
222
223                 }
224                 
225                 init();
226         };
227
228         CatalogCtrl.$inject = [ 'conf', 'catalogService', 'confirmBoxService', 'ExternalRequestAccessService', '$log',
229                         '$window', 'userProfileService', 'applicationsService', '$scope', 
230                         '$state', '$timeout', '$interval', '$modal', 'ngDialog' ];
231         angular.module('ecompApp').controller('CatalogCtrl', CatalogCtrl);
232 })();
233
234 function getParameterByName(name, url) {
235         if (!url)
236                 url = window.location.href;
237         name = name.replace(/[\[\]]/g, "\\$&");
238         var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex
239                         .exec(url);
240         if (!results)
241                 return '';
242         if (!results[2])
243                 return '';
244         return results[2].replace(/\+/g, " ");
245 }