2 * ================================================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 * Created by nnaffar on 12/20/15.
\r
25 class WidgetDetailsModalCtrl {
\r
26 constructor($scope, $log, applicationsService, widgetsService, errorMessageByCode,
\r
27 ECOMP_URL_REGEX, $window, userProfileService, $cookies, $rootScope) {
\r
29 let newWidgetModel = {
\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
48 restrictedApp: apps[i].restrictedApp
\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
60 this.selectedApp = null;
\r
62 //init appId & appName with selectedApp
\r
63 this.updateSelectedApp();
\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
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
79 $log.debug('WidgetDetailsModalCtrl::init: New app mode');
\r
80 this.isEditMode = false;
\r
81 this.widget = _.clone(newWidgetModel);
\r
86 this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
\r
88 //This part handles conflict errors (409)
\r
89 this.conflictMessages = {};
\r
90 this.scrollApi = {};
\r
91 let handleConflictErrors = err => {
\r
95 if(!err.data.length){ //support objects
\r
96 err.data = [err.data]
\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
108 this.scrollApi.scrollTop();
\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
120 let unregisterName = $scope.$watchGroup(['widgetDetails.selectedApp','widgetDetails.widget.name'], (newVal, oldVal) => {
\r
121 if(newVal.toLowerCase() !== oldVal.toLowerCase()){
\r
122 resetConflict('name');
\r
128 let unregisterUrl = $scope.$watch('widgetDetails.widget.url', (newVal, oldVal) => {
\r
129 if(newVal.toLowerCase() !== oldVal.toLowerCase()) {
\r
130 resetConflict('url');
\r
136 //***************************
\r
138 this.updateSelectedApp = () => {
\r
139 if (!this.selectedApp) {
\r
142 this.widget.appId = this.selectedApp.id;
\r
143 this.widget.appName = this.selectedApp.name;
\r
146 let emptyCookies = () => {
\r
147 userProfileService.getUserProfile()
\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
154 $log.error('WidgetDetailsModalCtrl::emptyCookies: There was a problem emptying the cookies! No user error presented though.');
\r
158 this.saveChanges = () => {
\r
159 if($scope.widgetForm.$invalid){
\r
162 this.isSaving = true;
\r
163 if(this.isEditMode){
\r
164 widgetsService.updateWidget(this.widget.id, this.widget)
\r
166 $log.debug('WidgetDetailsModalCtrl::saveChanges: Widget update succeeded!');
\r
167 $scope.closeThisDialog(true);
\r
170 if(err.status === 409){//Conflict
\r
171 handleConflictErrors(err);
\r
173 confirmBoxService.showInformation('There was a problem saving the Widget. ' +
\r
174 'Please try again later. Error Status: ' + err.status).then(isConfirmed => {});
\r
176 $log.error('WidgetDetailsModalCtrl::saveChanges error: ', err);
\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
185 // for bug in IE 11
\r
188 widgetsService.createWidget(this.widget)
\r
190 $log.debug('WidgetDetailsModalCtrl::createWidget: Widget creation succeeded!');
\r
191 $scope.closeThisDialog(true);
\r
193 $rootScope.noWidgets = false;
\r
195 if(err.status === 409){//Conflict
\r
196 handleConflictErrors(err);
\r
198 confirmBoxService.showInformation('There was a problem creating the Widget. ' +
\r
199 'Please try again later. Error Status: ' + err.status).then(isConfirmed => {});
\r
201 $log.error('WidgetDetailsModalCtrl::createWidget error: ',err);
\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
210 // for bug in IE 11
\r
217 $scope.$on('$stateChangeStart', e => {
\r
218 //Disable navigation when modal is opened
\r
219 e.preventDefault();
\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