nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / views / widgets / widget-details-dialog / widget-details.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
21 'use strict';
22 (function () {
23     class WidgetDetailsModalCtrl {
24         constructor($scope, $log, applicationsService, widgetsService, errorMessageByCode, ECOMP_URL_REGEX, $window,userProfileService,$cookies) {
25
26             let newWidgetModel = {
27                 name: null,
28                 appId: null,
29                 appName: null,
30                 width: 360,
31                 height: 300,
32                 url: null
33             };
34
35             let getAvailableApps = () => {
36                 applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
37                     this.availableApps=[];
38                     for(var i=0;i<apps.length;i++) {
39                         if (!apps[i].restrictedApp) {
40                             $log.debug('WidgetDetailsModalCtrl::getAvailableApps: pushing {id: ', apps[i].id, 'name: ', apps[i].name,
41                                             'restrictedApp: ', apps[i].restrictedApp, '}');
42                             this.availableApps.push({
43                                 id: apps[i].id,
44                                 name: apps[i].name,
45                                 restrictedApp: apps[i].restrictedApp
46                             });
47                         }
48                     }
49                     
50                     if (this.isEditMode) {
51                         this.selectedApp = _.find(apps, {id: this.widget.appId});
52                         if(!this.selectedApp){
53                             $scope.widgetForm.app.$dirty = true;
54                         }
55                     } else {
56                         this.selectedApp = null;
57                     }
58                     this.updateSelectedApp();
59                 }).catch(err => {
60                     $log.error(err);
61                 });
62             };
63             /**/
64
65             let init = () => {
66                 $log.info('AppDetailsModalCtrl::init');
67                 this.isSaving = false;
68                 if ($scope.ngDialogData && $scope.ngDialogData.widget) {
69                     $log.debug('WidgetDetailsModalCtrl::getAvailableApps: Edit widget mode for', $scope.ngDialogData.widget);
70                     this.isEditMode = true;
71                     this.widget = _.clone($scope.ngDialogData.widget);
72                 } else {
73                     $log.debug('WidgetDetailsModalCtrl::init: New app mode');
74                     this.isEditMode = false;
75                     this.widget = _.clone(newWidgetModel);
76                 }
77                 getAvailableApps();
78             };
79
80             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
81
82             this.conflictMessages = {};
83             this.scrollApi = {};
84             let handleConflictErrors = err => {
85                 if(!err.data){
86                     return;
87                 }
88                 if(!err.data.length){
89                     err.data = [err.data]
90                 }
91                 _.forEach(err.data, item => {
92                     _.forEach(item.fields, field => {
93                         this.conflictMessages[field.name] = errorMessageByCode[item.errorCode];
94                         $scope.widgetForm[field.name].$setValidity('conflict', false);
95                         watchOnce[field.name]();
96                     });
97                 });
98                 this.scrollApi.scrollTop();
99             };
100
101             let resetConflict = fieldName => {
102                 delete this.conflictMessages[fieldName];
103                 if($scope.widgetForm[fieldName]){
104                     $scope.widgetForm[fieldName].$setValidity('conflict', true);
105                 }
106             };
107
108             let watchOnce = {
109                 name: () => {
110                     let unregisterName = $scope.$watchGroup(['widgetDetails.selectedApp','widgetDetails.widget.name'], (newVal, oldVal) => {
111                         if(newVal.toLowerCase() !== oldVal.toLowerCase()){
112                             resetConflict('name');
113                             unregisterName();
114                         }
115                     });
116                 },
117                 url: () => {
118                     let unregisterUrl = $scope.$watch('widgetDetails.widget.url', (newVal, oldVal) => {
119                         if(newVal.toLowerCase() !== oldVal.toLowerCase()) {
120                             resetConflict('url');
121                             unregisterUrl();
122                         }
123                     });
124                 }
125             };
126
127             this.updateSelectedApp = () => {
128                 if (!this.selectedApp) {
129                     return;
130                 }
131                 this.widget.appId = this.selectedApp.id;
132                 this.widget.appName = this.selectedApp.name;
133             };
134
135             let emptyCookies = () => {
136                 userProfileService.getUserProfile()
137                     .then(profile=> {
138                         $log.info('AppDetailsModalCtrl::emptyCookies profile: ', profile);
139                         $scope.userId = profile.orgUserId;
140                         $log.info('user has the following userId: ' + profile.userId);
141                         if ($cookies.getObject($scope.userId + '_widget') != undefined && $cookies.getObject($scope.userId + '_widget') != null) {
142                             $cookies.remove($scope.userId + '_widget');
143                         }
144                     });
145             };
146
147             this.saveChanges = () => {
148                 if($scope.widgetForm.$invalid){
149                     return;
150                 }
151                 this.isSaving = true;
152                 if(this.isEditMode){
153                     widgetsService.updateWidget(this.widget.id, this.widget)
154                         .then(() => {
155                             $log.debug('WidgetDetailsModalCtrl::saveChanges: Widget update succeeded!');
156                             $scope.closeThisDialog(true);
157                             emptyCookies();
158                         }).catch(err => {
159                         if(err.status === 409){
160                             handleConflictErrors(err);
161                         }
162                         $log.error(err);
163                     }).finally(()=>{
164                         this.isSaving = false;
165                         var objOffsetVersion = objAgent.indexOf("MSIE");
166                         if (objOffsetVersion != -1) {
167                             $log.debug('WidgetDetailsModalCtrl::saveChanges: Browser is IE, forcing Refresh');
168                             $window.location.reload();
169                         }
170                     });
171                 }else{
172                     widgetsService.createWidget(this.widget)
173                         .then(() => {
174                             $log.debug('WidgetDetailsModalCtrl::createWidget: Widget creation succeeded!');
175                             $scope.closeThisDialog(true);
176                             emptyCookies();
177                         }).catch(err => {
178                         if(err.status === 409){
179                             handleConflictErrors('WidgetDetailsModalCtrl::createWidget error: ',err);
180                         }
181                         $log.error('WidgetDetailsModalCtrl::createWidget error: ',err);
182                     }).finally(()=>{
183                         this.isSaving = false;
184                         var objOffsetVersion = objAgent.indexOf("MSIE");
185                         if (objOffsetVersion != -1) {
186                             $log.debug('WidgetDetailsModalCtrl::createWidget: Browser is IE, forcing Refresh');
187                             $window.location.reload();
188                         }
189                     });
190                 }
191             };
192
193             init();
194
195             $scope.$on('$stateChangeStart', e => {
196                 e.preventDefault();
197             });
198         }
199     }
200     WidgetDetailsModalCtrl.$inject = ['$scope', '$log', 'applicationsService', 'widgetsService', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window','userProfileService','$cookies'];
201     angular.module('ecompApp').controller('WidgetDetailsModalCtrl', WidgetDetailsModalCtrl);
202 })();