[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / widgets / widget-details-dialog / widget-details.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 /**\r
21  * Created by nnaffar on 12/20/15.\r
22  */\r
23 'use strict';\r
24 (function () {\r
25     class WidgetDetailsModalCtrl {\r
26         constructor($scope, $log, applicationsService, widgetsService, errorMessageByCode,\r
27                     ECOMP_URL_REGEX, $window, userProfileService, $cookies, $rootScope) {\r
28 \r
29             let newWidgetModel = {\r
30                 name: null,\r
31                 appId: null,\r
32                 appName: null,\r
33                 width: 360,\r
34                 height: 300,\r
35                 url: null\r
36             };\r
37 \r
38             let getAvailableApps = () => {\r
39                 applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {\r
40                     this.availableApps=[];\r
41                     for(var i=0;i<apps.length;i++) {\r
42                         if (!apps[i].restrictedApp) {\r
43                             $log.debug('WidgetDetailsModalCtrl::getAvailableApps: pushing {id: ', apps[i].id, 'name: ', apps[i].name,\r
44                                             'restrictedApp: ', apps[i].restrictedApp, '}');\r
45                             this.availableApps.push({\r
46                                 id: apps[i].id,\r
47                                 name: apps[i].name,\r
48                                 restrictedApp: apps[i].restrictedApp\r
49                             });\r
50                         }\r
51                     }\r
52                     \r
53                     if (this.isEditMode) {\r
54                         this.selectedApp = _.find(apps, {id: this.widget.appId});\r
55                         if(!this.selectedApp){\r
56                             //workaround to display validation errors for apps dropdown in case selectedApp isn't valid\r
57                             $scope.widgetForm.app.$dirty = true;\r
58                         }\r
59                     } else {\r
60                         this.selectedApp = null;\r
61                     }\r
62                     //init appId & appName with selectedApp\r
63                     this.updateSelectedApp();\r
64                 }).catch(err => {\r
65                     confirmBoxService.showInformation('There was a problem retrieving the Widgets. ' +\r
66                         'Please try again later.').then(isConfirmed => {});\r
67                     $log.error('WidgetDetailsModalCtrl::getAvailableApps error: '+ err);\r
68                 });\r
69             };\r
70             /**/\r
71 \r
72             let init = () => {\r
73                 this.isSaving = false;\r
74                 if ($scope.ngDialogData && $scope.ngDialogData.widget) {\r
75                     $log.debug('WidgetDetailsModalCtrl::getAvailableApps: Edit widget mode for', $scope.ngDialogData.widget);\r
76                     this.isEditMode = true;\r
77                     this.widget = _.clone($scope.ngDialogData.widget);\r
78                 } else {\r
79                     $log.debug('WidgetDetailsModalCtrl::init: New app mode');\r
80                     this.isEditMode = false;\r
81                     this.widget = _.clone(newWidgetModel);\r
82                 }\r
83                 getAvailableApps();\r
84             };\r
85 \r
86             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;\r
87 \r
88             //This part handles conflict errors (409)\r
89             this.conflictMessages = {};\r
90             this.scrollApi = {};\r
91             let handleConflictErrors = err => {\r
92                 if(!err.data){\r
93                     return;\r
94                 }\r
95                 if(!err.data.length){ //support objects\r
96                     err.data = [err.data]\r
97                 }\r
98                 _.forEach(err.data, item => {\r
99                     _.forEach(item.fields, field => {\r
100                         //set conflict message\r
101                         this.conflictMessages[field.name] = errorMessageByCode[item.errorCode];\r
102                         //set field as invalid\r
103                         $scope.widgetForm[field.name].$setValidity('conflict', false);\r
104                         //set watch once to clear error after user correction\r
105                         watchOnce[field.name]();\r
106                     });\r
107                 });\r
108                 this.scrollApi.scrollTop();\r
109             };\r
110 \r
111             let resetConflict = fieldName => {\r
112                 delete this.conflictMessages[fieldName];\r
113                 if($scope.widgetForm[fieldName]){\r
114                     $scope.widgetForm[fieldName].$setValidity('conflict', true);\r
115                 }\r
116             };\r
117 \r
118             let watchOnce = {\r
119                 name: () => {\r
120                     let unregisterName = $scope.$watchGroup(['widgetDetails.selectedApp','widgetDetails.widget.name'], (newVal, oldVal) => {\r
121                         if(newVal.toLowerCase() !== oldVal.toLowerCase()){\r
122                             resetConflict('name');\r
123                             unregisterName();\r
124                         }\r
125                     });\r
126                 },\r
127                 url: () => {\r
128                     let unregisterUrl = $scope.$watch('widgetDetails.widget.url', (newVal, oldVal) => {\r
129                         if(newVal.toLowerCase() !== oldVal.toLowerCase()) {\r
130                             resetConflict('url');\r
131                             unregisterUrl();\r
132                         }\r
133                     });\r
134                 }\r
135             };\r
136             //***************************\r
137 \r
138             this.updateSelectedApp = () => {\r
139                 if (!this.selectedApp) {\r
140                     return;\r
141                 }\r
142                 this.widget.appId = this.selectedApp.id;\r
143                 this.widget.appName = this.selectedApp.name;\r
144             };\r
145 \r
146             let emptyCookies = () => {\r
147                 userProfileService.getUserProfile()\r
148                     .then(profile=> {\r
149                         $scope.orgUserId = profile.orgUserId;\r
150                         if ($cookies.getObject($scope.orgUserId + '_widget') != undefined && $cookies.getObject($scope.orgUserId + '_widget') != null) {\r
151                             $cookies.remove($scope.orgUserId + '_widget');\r
152                         }\r
153                     }).catch(err => {\r
154                         $log.error('WidgetDetailsModalCtrl::emptyCookies: There was a problem emptying the cookies! No user error presented though.');\r
155                     });\r
156             };\r
157 \r
158             this.saveChanges = () => {\r
159                 if($scope.widgetForm.$invalid){\r
160                     return;\r
161                 }\r
162                 this.isSaving = true;\r
163                 if(this.isEditMode){\r
164                     widgetsService.updateWidget(this.widget.id, this.widget)\r
165                         .then(() => {\r
166                             $log.debug('WidgetDetailsModalCtrl::saveChanges: Widget update succeeded!');\r
167                             $scope.closeThisDialog(true);\r
168                             emptyCookies();\r
169                         }).catch(err => {\r
170                             if(err.status === 409){//Conflict\r
171                                 handleConflictErrors(err);\r
172                             } else {\r
173                                 confirmBoxService.showInformation('There was a problem saving the Widget. ' +\r
174                                     'Please try again later. Error Status: ' + err.status).then(isConfirmed => {});\r
175                             }\r
176                             $log.error('WidgetDetailsModalCtrl::saveChanges error: ', err);\r
177                         }).finally(()=>{\r
178                             this.isSaving = false;\r
179                             // for bug in IE 11\r
180                             var objOffsetVersion = objAgent.indexOf("MSIE");\r
181                             if (objOffsetVersion != -1) {\r
182                                 $log.debug('WidgetDetailsModalCtrl::saveChanges: Browser is IE, forcing Refresh');\r
183                                 $window.location.reload();            // for bug in IE 11\r
184                             }\r
185                             // for bug in IE 11\r
186                         });\r
187                 } else {\r
188                     widgetsService.createWidget(this.widget)\r
189                         .then(() => {\r
190                             $log.debug('WidgetDetailsModalCtrl::createWidget: Widget creation succeeded!');\r
191                             $scope.closeThisDialog(true);\r
192                             emptyCookies();\r
193                             $rootScope.noWidgets = false;\r
194                         }).catch(err => {\r
195                         if(err.status === 409){//Conflict\r
196                             handleConflictErrors(err);\r
197                         } else {\r
198                             confirmBoxService.showInformation('There was a problem creating the Widget. ' +\r
199                                 'Please try again later. Error Status: ' + err.status).then(isConfirmed => {});\r
200                         }\r
201                         $log.error('WidgetDetailsModalCtrl::createWidget error: ',err);\r
202                     }).finally(()=>{\r
203                         this.isSaving = false;\r
204                         // for bug in IE 11\r
205                         var objOffsetVersion = objAgent.indexOf("MSIE");\r
206                         if (objOffsetVersion != -1) {\r
207                             $log.debug('WidgetDetailsModalCtrl::createWidget: Browser is IE, forcing Refresh');\r
208                             $window.location.reload();            // for bug in IE 11\r
209                         }\r
210                         // for bug in IE 11\r
211                     });\r
212                 }\r
213             };\r
214 \r
215             init();\r
216 \r
217             $scope.$on('$stateChangeStart', e => {\r
218                 //Disable navigation when modal is opened\r
219                 e.preventDefault();\r
220             });\r
221         }\r
222     }\r
223     WidgetDetailsModalCtrl.$inject = ['$scope', '$log', 'applicationsService', 'widgetsService', 'errorMessageByCode',\r
224         'ECOMP_URL_REGEX', '$window','userProfileService','$cookies', '$rootScope'];\r
225     angular.module('ecompApp').controller('WidgetDetailsModalCtrl', WidgetDetailsModalCtrl);\r
226 })();\r