[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-os / client / src / views / applications / application-details-dialog / application-details.controller.js
diff --git a/ecomp-portal-FE-os/client/src/views/applications/application-details-dialog/application-details.controller.js b/ecomp-portal-FE-os/client/src/views/applications/application-details-dialog/application-details.controller.js
new file mode 100644 (file)
index 0000000..da7cd4a
--- /dev/null
@@ -0,0 +1,268 @@
+/*-
+ * ================================================================================
+ * eCOMP Portal
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ================================================================================
+ */
+'use strict';
+(function () {
+    class AppDetailsModalCtrl {
+        constructor($scope, $log, applicationsService, errorMessageByCode,
+                    ECOMP_URL_REGEX,userProfileService, $cookies, confirmBoxService) {
+//            let emptyImg = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
+            // empty image should really be empty, or it causes problems for the back end
+            let emptyImg = null;
+            this.emptyImgForPreview = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
+
+            let newAppModel = {
+                'id': null,
+                'name': null,
+                'imageUrl': null,
+                'description': null,
+                'notes': null,
+                'url': null,
+                'alternateUrl': null,
+                'restUrl': null,
+                'isOpen': false,
+                'username': null,
+                'appPassword': null,
+                'thumbnail': emptyImg,
+                'isEnabled': true,
+                'restrictedApp': false
+            };
+
+            let init = () => {
+                $log.info('AppDetailsModalCtrl::init');
+                this.isSaving = false;
+                if($scope.ngDialogData && $scope.ngDialogData.app){
+                    $log.debug('AppDetailsModalCtrl:init:: Edit app mode for', $scope.ngDialogData.app);
+                    this.isEditMode = true;
+                    this.app = _.clone($scope.ngDialogData.app);
+                }else{
+                    $log.debug('AppDetailsModalCtrl:init:: New app mode');
+                    this.isEditMode = false;
+                    this.app = _.clone(newAppModel);
+                }
+                this.originalImage = null
+            };
+
+            this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
+
+            this.imageApi = {};
+            this.removeImage = () => {
+                $log.debug('AppDetailsModalCtrl:removeImage:: entering removeImage');
+
+                confirmBoxService.confirm("Are you sure you want to remove the image?").then(isConfirmed => {
+                    if(isConfirmed){
+                        this.imageApi.clearFile();
+                        this.app.thumbnail = emptyImg;
+                        this.originalImage = null;
+                        this.app.imageUrl = null;
+                    }
+                }).catch(err => {
+                    $log.error('AppDetailsModalCtrl:removeImage error:: ',err);
+                });
+            };
+
+            //This part handles conflict errors (409)
+            this.conflictMessages = {};
+            this.scrollApi = {};
+            let handleConflictErrors = err => {
+                if(!err.data){
+                    return;
+                }
+                if(!err.data.length){ //support objects
+                    err.data = [err.data]
+                }
+                _.forEach(err.data, item => {
+                    _.forEach(item.fields, field => {
+                        //set conflict message
+                        this.conflictMessages[field.name] = errorMessageByCode[item.errorCode];
+                        //set field as invalid
+                        $scope.appForm[field.name].$setValidity('conflict', false);
+                        //set watch once to clear error after user correction
+                        watchOnce[field.name]();
+                    });
+                });
+                this.scrollApi.scrollTop();
+            };
+
+            let resetConflict = fieldName => {
+                delete this.conflictMessages[fieldName];
+                if($scope.appForm[fieldName]){
+                    $scope.appForm[fieldName].$setValidity('conflict', true);
+                }
+            };
+
+
+            let emptyCookies = () => {
+                $log.debug('AppDetailsModalCtrl:emptyCookies:: entering emptyCookies');
+                userProfileService.getUserProfile()
+                    .then(profile=> {
+                        // $log.info(profile);
+                        $scope.orgUserId = profile.attuid;
+                        $log.debug('AppDetailsModalCtrl:emptyCookies for the following attuid: ' + profile.attuid);
+                        if ($cookies.getObject($scope.orgUserId + '_apps') != undefined && $cookies.getObject($scope.orgUserId + '_apps') != null) {
+                            $cookies.remove($scope.orgUserId + '_apps');
+                            $log.debug('AppDetailsModalCtrl:emptyCookies removed: ' + $scope.orgUserId + '_apps');
+                        }
+                        if ($cookies.getObject($scope.orgUserId + '_widget') != undefined && $cookies.getObject($scope.orgUserId + '_widget') != null) {
+                            $cookies.remove($scope.orgUserId + '_widget');
+                            $log.debug('AppDetailsModalCtrl:emptyCookies removed: ' + $scope.orgUserId + '_widget');
+                        }
+                    }).catch(err => {
+                        $log.error('AppDetailsModalCtrl:emptyCookies error:: '+ JSON.stringify(err));
+                    });
+            };
+
+
+            let watchOnce = {
+                name: () => {
+                    let unregisterName = $scope.$watch('appDetails.app.name', (newVal, oldVal) => {
+                        // $log.debug('newVal, oldVal = ' + newVal.toLowerCase() + " | " + oldVal.toLowerCase())
+                        if(newVal.toLowerCase() !== oldVal.toLowerCase()){
+                            resetConflict('name');
+                            unregisterName();
+                        }
+                    });
+                },
+                url: () => {
+                    let unregisterUrl = $scope.$watch('appDetails.app.url', (newVal, oldVal) => {
+                        if(newVal.toLowerCase() !== oldVal.toLowerCase()) {
+                            resetConflict('url');
+                            unregisterUrl();
+                        }
+                    });
+                }
+            };
+            //***************************
+
+            this.saveChanges = () => {
+                //if valid..
+                if($scope.appForm.$invalid){
+                    return;
+                }
+                this.isSaving = true;
+                // For a restricted app, null out all irrelevant fields
+                if (this.app.restrictedApp) {
+                    this.app.restUrl = null;
+                    this.app.isOpen = true;
+                    this.app.username = null;
+                    this.app.appPassword = null;
+                    this.app.uebTopicName = null;
+                    this.app.uebKey = null;
+                    this.app.uebSecret = null;
+                }
+                if(this.isEditMode){
+                    applicationsService.updateOnboardingApp(this.app)
+                        .then(() => {
+                            $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: App update succeeded!');
+                            $scope.closeThisDialog(true);
+                            emptyCookies();
+                        }).catch(err => {
+                            switch (err.status) {
+                                case '409':         // Conflict
+                                    handleConflictErrors(err);
+                                    break;
+                                case '500':         // Internal Server Error
+                                    confirmBoxService.showInformation('There was a problem updating the application changes. ' +
+                                        'Please try again later. Error: ' + err.status).then(isConfirmed => {});
+                                    break;
+                                case '403':         // Forbidden... possible webjunction error to try again
+                                    confirmBoxService.showInformation('There was a problem updating the application changes. ' +
+                                        'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
+                                    break;
+                                default:
+                                    confirmBoxService.showInformation('There was a problem updating the application changes. ' +
+                                        'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
+                            }
+                            $log.error('applicationsService:updateOnboardingApp error:: '+ JSON.stringify(err));
+                        }).finally(()=>{
+                            this.isSaving = false;
+                            // for bug in IE 11
+                            var objOffsetVersion = objAgent.indexOf("MSIE");
+                            if (objOffsetVersion != -1) {
+                                $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: Browser is IE, forcing Refresh');
+                                $window.location.reload();            // for bug in IE 11
+                            }
+                            // for bug in IE 11
+                        });
+                }else{
+                    applicationsService.addOnboardingApp(this.app)
+                        .then(() => {
+                            $log.debug('App creation succeeded!');
+                            $scope.closeThisDialog(true);
+                            emptyCookies();
+                        }).catch(err => {
+                            switch (err.status) {
+                                case '409':         // Conflict
+                                    handleConflictErrors(err);
+                                    break;
+                                case '500':         // Internal Server Error
+                                    confirmBoxService.showInformation('There was a problem adding the application information. ' +
+                                        'Please try again later. Error: ' + err.status).then(isConfirmed => {});
+                                    break;
+                                default:
+                                    confirmBoxService.showInformation('There was a problem updating the application changes. ' +
+                                        'Please try again. If the problem persists, then try again later. Error: ' +
+                                        err.status).then(isConfirmed => {});
+                            }
+                            $log.error('applicationsService:addOnboardingApp error:: '+ JSON.stringify(err));
+                        }).finally(()=>{
+                            this.isSaving = false;
+                            // for bug in IE 11
+                            var objOffsetVersion = objAgent.indexOf("MSIE");
+                            if (objOffsetVersion != -1) {
+                                $log.debug('applicationsService:addOnboardingApp:: Browser is IE, forcing Refresh');
+                                $window.location.reload();            // for bug in IE 11
+                            }
+                            // for bug in IE 11
+                        });
+                }
+            };
+
+
+            init();
+
+            $scope.$watch('appDetails.originalImage', (newVal, oldVal) => {
+              if((!newVal || !newVal.resized) && !this.app.imageUrl){
+                  if (!newVal) {
+                      $log.debug('applicationsService:$scope.$watch:: originalImage: newVal is null');
+                  } else {
+                      $log.debug('applicationsService:$scope.$watch:: originalImage: newVal is not resized and no imageUrl');
+                  }
+                  this.app.imageUrl = null;
+                  this.app.thumbnail = emptyImg;
+                  return;
+              }
+
+                if(!(_.isEqual(newVal, oldVal))){
+                    $log.debug('applicationsService:$scope.$watch:: thumbnail updated!');
+                    this.app.imageUrl = null;
+                    this.app.thumbnail = newVal.resized.dataURL;
+                }
+            });
+
+            $scope.$on('$stateChangeStart', e => {
+                //Disable navigation when modal is opened
+                e.preventDefault();
+            });
+        }
+    }
+    AppDetailsModalCtrl.$inject = ['$scope', '$log', 'applicationsService', 'errorMessageByCode',
+        'ECOMP_URL_REGEX','userProfileService','$cookies', 'confirmBoxService'];
+    angular.module('ecompApp').controller('AppDetailsModalCtrl', AppDetailsModalCtrl);
+})();
\ No newline at end of file