2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
21 * Created by nnaffar on 12/20/15.
25 class WidgetDetailsModalCtrl {
26 constructor($scope, $log, applicationsService, widgetsService, errorMessageByCode,
27 ECOMP_URL_REGEX, $window, userProfileService, $cookies, $rootScope) {
29 let newWidgetModel = {
38 let getAvailableApps = () => {
39 applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
40 this.availableApps=[];
41 for(var i=0;i<apps.length;i++) {
42 if (!apps[i].restrictedApp) {
43 $log.debug('WidgetDetailsModalCtrl::getAvailableApps: pushing {id: ', apps[i].id, 'name: ', apps[i].name,
44 'restrictedApp: ', apps[i].restrictedApp, '}');
45 this.availableApps.push({
48 restrictedApp: apps[i].restrictedApp
53 if (this.isEditMode) {
54 this.selectedApp = _.find(apps, {id: this.widget.appId});
55 if(!this.selectedApp){
56 //workaround to display validation errors for apps dropdown in case selectedApp isn't valid
57 $scope.widgetForm.app.$dirty = true;
60 this.selectedApp = null;
62 //init appId & appName with selectedApp
63 this.updateSelectedApp();
65 confirmBoxService.showInformation('There was a problem retrieving the Widgets. ' +
66 'Please try again later.').then(isConfirmed => {});
67 $log.error('WidgetDetailsModalCtrl::getAvailableApps error: '+ err);
73 this.isSaving = false;
74 if ($scope.ngDialogData && $scope.ngDialogData.widget) {
75 $log.debug('WidgetDetailsModalCtrl::getAvailableApps: Edit widget mode for', $scope.ngDialogData.widget);
76 this.isEditMode = true;
77 this.widget = _.clone($scope.ngDialogData.widget);
79 $log.debug('WidgetDetailsModalCtrl::init: New app mode');
80 this.isEditMode = false;
81 this.widget = _.clone(newWidgetModel);
86 this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
88 //This part handles conflict errors (409)
89 this.conflictMessages = {};
91 let handleConflictErrors = err => {
95 if(!err.data.length){ //support objects
98 _.forEach(err.data, item => {
99 _.forEach(item.fields, field => {
100 //set conflict message
101 this.conflictMessages[field.name] = errorMessageByCode[item.errorCode];
102 //set field as invalid
103 $scope.widgetForm[field.name].$setValidity('conflict', false);
104 //set watch once to clear error after user correction
105 watchOnce[field.name]();
108 this.scrollApi.scrollTop();
111 let resetConflict = fieldName => {
112 delete this.conflictMessages[fieldName];
113 if($scope.widgetForm[fieldName]){
114 $scope.widgetForm[fieldName].$setValidity('conflict', true);
120 let unregisterName = $scope.$watchGroup(['widgetDetails.selectedApp','widgetDetails.widget.name'], (newVal, oldVal) => {
121 if(newVal.toLowerCase() !== oldVal.toLowerCase()){
122 resetConflict('name');
128 let unregisterUrl = $scope.$watch('widgetDetails.widget.url', (newVal, oldVal) => {
129 if(newVal.toLowerCase() !== oldVal.toLowerCase()) {
130 resetConflict('url');
136 //***************************
138 this.updateSelectedApp = () => {
139 if (!this.selectedApp) {
142 this.widget.appId = this.selectedApp.id;
143 this.widget.appName = this.selectedApp.name;
146 let emptyCookies = () => {
147 userProfileService.getUserProfile()
149 $scope.orgUserId = profile.orgUserId;
150 if ($cookies.getObject($scope.orgUserId + '_widget') != undefined && $cookies.getObject($scope.orgUserId + '_widget') != null) {
151 $cookies.remove($scope.orgUserId + '_widget');
154 $log.error('WidgetDetailsModalCtrl::emptyCookies: There was a problem emptying the cookies! No user error presented though.');
158 this.saveChanges = () => {
159 if($scope.widgetForm.$invalid){
162 this.isSaving = true;
164 widgetsService.updateWidget(this.widget.id, this.widget)
166 $log.debug('WidgetDetailsModalCtrl::saveChanges: Widget update succeeded!');
167 $scope.closeThisDialog(true);
170 if(err.status === 409){//Conflict
171 handleConflictErrors(err);
173 confirmBoxService.showInformation('There was a problem saving the Widget. ' +
174 'Please try again later. Error Status: ' + err.status).then(isConfirmed => {});
176 $log.error('WidgetDetailsModalCtrl::saveChanges error: ', err);
178 this.isSaving = false;
180 var objOffsetVersion = objAgent.indexOf("MSIE");
181 if (objOffsetVersion != -1) {
182 $log.debug('WidgetDetailsModalCtrl::saveChanges: Browser is IE, forcing Refresh');
183 $window.location.reload(); // for bug in IE 11
188 widgetsService.createWidget(this.widget)
190 $log.debug('WidgetDetailsModalCtrl::createWidget: Widget creation succeeded!');
191 $scope.closeThisDialog(true);
193 $rootScope.noWidgets = false;
195 if(err.status === 409){//Conflict
196 handleConflictErrors(err);
198 confirmBoxService.showInformation('There was a problem creating the Widget. ' +
199 'Please try again later. Error Status: ' + err.status).then(isConfirmed => {});
201 $log.error('WidgetDetailsModalCtrl::createWidget error: ',err);
203 this.isSaving = false;
205 var objOffsetVersion = objAgent.indexOf("MSIE");
206 if (objOffsetVersion != -1) {
207 $log.debug('WidgetDetailsModalCtrl::createWidget: Browser is IE, forcing Refresh');
208 $window.location.reload(); // for bug in IE 11
217 $scope.$on('$stateChangeStart', e => {
218 //Disable navigation when modal is opened
223 WidgetDetailsModalCtrl.$inject = ['$scope', '$log', 'applicationsService', 'widgetsService', 'errorMessageByCode',
224 'ECOMP_URL_REGEX', '$window','userProfileService','$cookies', '$rootScope'];
225 angular.module('ecompApp').controller('WidgetDetailsModalCtrl', WidgetDetailsModalCtrl);