[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / catalog / catalog.controller.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 'use strict';\r
21 \r
22 function _classCallCheck(instance, Constructor) {\r
23         if (!(instance instanceof Constructor)) {\r
24                 throw new TypeError('Cannot call a class as a function');\r
25         }\r
26 }\r
27 \r
28 (function() {\r
29         var HTTP_PROTOCOL_RGX = /https?:\/\//;\r
30 \r
31         var CatalogCtrl = function CatalogCtrl(conf, catalogService, confirmBoxService, ExternalRequestAccessService,\r
32                         $log, $window, userProfileService, applicationsService, $scope, $state, \r
33                         $timeout, $interval, $modal, ngDialog) {\r
34 \r
35                 this.conf = conf;       \r
36                 var _this = this;\r
37                 _classCallCheck(this, CatalogCtrl);\r
38 \r
39                 // activate spinner\r
40                 this.isLoading = true;\r
41                 $scope.getAppCatalogIsDone = false;\r
42                 $scope.radioValue = 'All';\r
43                 $scope.$watch('radioValue', function(newValue, oldValue) {\r
44                         var appCatalog = $scope.appCatalog;\r
45                         $scope.appCatalog = [];\r
46                         $scope.appCatalog = appCatalog;\r
47 \r
48                         \r
49                 });\r
50                 \r
51                 this.catalogService = catalogService;\r
52                 this.userProfileService = userProfileService;\r
53                 this.applicationsService = applicationsService;\r
54                 var resultAccessValue = null;\r
55                 $scope.demoNum = 1;\r
56                 $scope.appRoles = [];\r
57                 \r
58         let init = () => {\r
59             getExternalAccess();\r
60         };\r
61         \r
62         var getExternalAccess = () => {\r
63                 ExternalRequestAccessService.getExternalRequestAccessServiceInfo().then(\r
64                                 function(property) {\r
65                 resultAccessValue = property.accessValue;\r
66                                         // $log.info(res);\r
67                                 }).catch(err => {\r
68                         $log.error('CatalogCtrl: failed getExternalRequestAccessServiceInfo: ' + JSON.stringify(err));\r
69                     });    \r
70         };\r
71         \r
72 \r
73         \r
74                 $scope.applyPresentationDetailsToAppsCatalog = function(res, value) {\r
75                         \r
76                         _this.apps = res;\r
77                         var rowNo = 0;\r
78 \r
79                         // defend against error string result -\r
80                         // a huge list that should never happen.\r
81                         var maxItems = 333;\r
82                         if (_this.apps.length < maxItems)\r
83                                 maxItems = _this.apps.length;\r
84                         for (var i = 0; i < maxItems; i++) {                                                                                            \r
85                                 let imgLnk = '';\r
86                                 if (_this.apps[i].imageUrl)\r
87                                         imgLnk = conf.api.appThumbnail.replace(':appId', _this.apps[i].id);\r
88                                 //$log.debug('CatalogCtlr::applyPresn: imgLink = ' + imgLnk);\r
89                                 $scope.appCatalog[i] = {\r
90                                         sizeX : 2,\r
91                                         sizeY : 2,\r
92                                         id : _this.apps[i].id,\r
93                                         headerText : _this.apps[i].name,\r
94                                         imageLink : imgLnk,\r
95                                         restricted : _this.apps[i].restricted,  \r
96                                         select : _this.apps[i].select,\r
97                                         access : _this.apps[i].access,\r
98                                         pending: _this.apps[i].pending,\r
99                                         mlproperty: value\r
100                                 };\r
101                         }\r
102                         //$log.debug('CatalogCtrl: getAppCatalog count : '\r
103                         //                              + $scope.appCatalog.length);\r
104                         _this.isLoading = false;\r
105                         $scope.getAppCatalogIsDone = true;\r
106                 }\r
107                 \r
108         let getAppsCatalog = () => {\r
109             catalogService.getAppCatalog()\r
110                 .then(appsList => {\r
111                         $scope.applyPresentationDetailsToAppsCatalog(appsList);\r
112                 }).catch(err => {\r
113                     confirmBoxService.showInformation('There was a problem retrieving the Applications. ' +\r
114                         'Please try again later. Error Status: '+ err.status).then(isConfirmed => {});\r
115                     $log.error('CatalogCtrl:openAddRoleModal: Error: ', err);\r
116                 });\r
117         };\r
118         \r
119         this.openAddRoleModal = (item) => { \r
120                 let data = null;\r
121 \r
122             if((!item.restricted) && (item.mlproperty)){\r
123                 data = {\r
124                     dialogState: 2,\r
125                     selectedUser:{\r
126                         attuid: $scope.attuid,\r
127                         firstName: $scope.firstName,\r
128                         lastName: $scope.lastName,\r
129                         headerText: item.headerText,\r
130                         item: item\r
131                         \r
132                     }\r
133                 }\r
134             ngDialog.open({\r
135                 templateUrl: 'app/views/catalog/add-catalog-dialogs/new-catalog.modal.html',\r
136                 controller: 'NewCatalogModalCtrl',\r
137                 controllerAs: 'userInfo',\r
138                 data: data\r
139             }).closePromise.then(needUpdate => {\r
140                 if(needUpdate.value === true){\r
141                         getAppsCatalog();\r
142                 }\r
143                 \r
144             });\r
145           }\r
146         };\r
147 \r
148                 // Run this function when user clicks on checkbox.\r
149                 this.storeSelection = function(item) {\r
150                         // $log.debug('CatalogCtrl:storeSelection: item.id is ' + item.id + ', select is ' + item.select);\r
151                         var pendingFlag = false;\r
152                         \r
153                         if(item.access) \r
154                                 pendingFlag = false;\r
155                         else\r
156                                 pendingFlag =  item.pending;\r
157                                                         \r
158                         var appData = { \r
159                                         appId   : item.id,\r
160                                         select  : item.select,\r
161                                         pending : pendingFlag   // TODO\r
162                         };\r
163                         \r
164                         catalogService.updateManualAppSort(appData).then(\r
165                                         function(result) {\r
166                                                 // $log.debug('CatalogCtrl:storeSelection result is ', result);\r
167                                         })['catch'](function(err) {\r
168                                                 $log.error('CatalogCtrl:storeSelection: exception: ', err);\r
169                                         });\r
170                         catalogService.updateAppCatalog(appData).then(\r
171                                 function(result) {\r
172                                         // $log.debug('CatalogCtrl:storeSelection result is ', result);\r
173                                 })['catch'](function(err) {\r
174                                         $log.error('CatalogCtrl:storeSelection: exception: ', err);\r
175                                 });\r
176                 };\r
177                 \r
178                 userProfileService\r
179                                 .getUserProfile()\r
180                                 .then(\r
181                                                 function(profile) {\r
182                                                         $scope.attuid = profile.orgUserId;\r
183                                                         $scope.firstName = profile.firstName;\r
184                                                         $scope.lastName = profile.lastName;\r
185                                                         $scope.appCatalog = [];\r
186                                                         \r
187                                                         catalogService.getAppCatalog().then(\r
188                                                                                         function(res) { \r
189                                                                         $scope.applyPresentationDetailsToAppsCatalog(res, resultAccessValue);\r
190                                                                                         })['catch'](function(err) {\r
191                                                                         $log.error('CatalogCtrl: failed getAppCatalog: ', JSON.stringify(err));\r
192                                                                         _this.isLoading = false;\r
193                                                                         $scope.getAppCatalogIsDone = true;\r
194                                                   });\r
195                                                 });\r
196 \r
197                                                         // applicationsService.getUserApps()\r
198 \r
199 \r
200                 this.gridsterOpts = {\r
201                         columns : 12,\r
202                         colWidth : 95,\r
203                         rowHeight : 95,\r
204                         margins : [ 20, 20 ],\r
205                         outerMargin : true,\r
206                         pushing : true,\r
207                         floating : true,\r
208                         swapping : true,\r
209                 };\r
210 \r
211                 if (getParameterByName('noUserError') != null) {\r
212                         if (getParameterByName('noUserError') == "Show") {\r
213                                 $("#errorInfo").show();\r
214                         }\r
215 \r
216                 }\r
217                 \r
218                 init();\r
219         };\r
220 \r
221         CatalogCtrl.$inject = [ 'conf', 'catalogService', 'confirmBoxService', 'ExternalRequestAccessService', '$log',\r
222                         '$window', 'userProfileService', 'applicationsService', '$scope', \r
223                         '$state', '$timeout', '$interval', '$modal', 'ngDialog' ];\r
224         angular.module('ecompApp').controller('CatalogCtrl', CatalogCtrl);\r
225 })();\r
226 \r
227 function getParameterByName(name, url) {\r
228         if (!url)\r
229                 url = window.location.href;\r
230         name = name.replace(/[\[\]]/g, "\\$&");\r
231         var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex\r
232                         .exec(url);\r
233         if (!results)\r
234                 return '';\r
235         if (!results[2])\r
236                 return '';\r
237         return results[2].replace(/\+/g, " ");\r
238 }\r