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 * ================================================================================
23 class WidgetDetailsModalCtrl {
24 constructor($scope, $log, applicationsService, widgetsService, errorMessageByCode, ECOMP_URL_REGEX, $window,userProfileService,$cookies) {
26 let newWidgetModel = {
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({
45 restrictedApp: apps[i].restrictedApp
50 if (this.isEditMode) {
51 this.selectedApp = _.find(apps, {id: this.widget.appId});
52 if(!this.selectedApp){
53 $scope.widgetForm.app.$dirty = true;
56 this.selectedApp = null;
58 this.updateSelectedApp();
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);
73 $log.debug('WidgetDetailsModalCtrl::init: New app mode');
74 this.isEditMode = false;
75 this.widget = _.clone(newWidgetModel);
80 this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
82 this.conflictMessages = {};
84 let handleConflictErrors = err => {
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]();
98 this.scrollApi.scrollTop();
101 let resetConflict = fieldName => {
102 delete this.conflictMessages[fieldName];
103 if($scope.widgetForm[fieldName]){
104 $scope.widgetForm[fieldName].$setValidity('conflict', true);
110 let unregisterName = $scope.$watchGroup(['widgetDetails.selectedApp','widgetDetails.widget.name'], (newVal, oldVal) => {
111 if(newVal.toLowerCase() !== oldVal.toLowerCase()){
112 resetConflict('name');
118 let unregisterUrl = $scope.$watch('widgetDetails.widget.url', (newVal, oldVal) => {
119 if(newVal.toLowerCase() !== oldVal.toLowerCase()) {
120 resetConflict('url');
127 this.updateSelectedApp = () => {
128 if (!this.selectedApp) {
131 this.widget.appId = this.selectedApp.id;
132 this.widget.appName = this.selectedApp.name;
135 let emptyCookies = () => {
136 userProfileService.getUserProfile()
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');
147 this.saveChanges = () => {
148 if($scope.widgetForm.$invalid){
151 this.isSaving = true;
153 widgetsService.updateWidget(this.widget.id, this.widget)
155 $log.debug('WidgetDetailsModalCtrl::saveChanges: Widget update succeeded!');
156 $scope.closeThisDialog(true);
159 if(err.status === 409){
160 handleConflictErrors(err);
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();
172 widgetsService.createWidget(this.widget)
174 $log.debug('WidgetDetailsModalCtrl::createWidget: Widget creation succeeded!');
175 $scope.closeThisDialog(true);
178 if(err.status === 409){
179 handleConflictErrors('WidgetDetailsModalCtrl::createWidget error: ',err);
181 $log.error('WidgetDetailsModalCtrl::createWidget error: ',err);
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();
195 $scope.$on('$stateChangeStart', e => {
200 WidgetDetailsModalCtrl.$inject = ['$scope', '$log', 'applicationsService', 'widgetsService', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window','userProfileService','$cookies'];
201 angular.module('ecompApp').controller('WidgetDetailsModalCtrl', WidgetDetailsModalCtrl);