2ca149cbc52afb354a014c540bab5d1860c6f093
[portal.git] / ecomp-portal-FE-common / client / app / views / role / role-list-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 app.controller('roleListController', function ($scope,RoleService,confirmBoxService,conf,$state,$http,$log){
21         $scope.showSpinner = true;
22         
23         RoleService.getRoles().then(function(data){
24                 $scope.showSpinner = true;
25                 var j = data;
26                 $scope.data = JSON.parse(j.data);
27                 $scope.availableRoles =JSON.parse($scope.data.availableRoles);
28                 $scope.showSpinner = false;
29         
30         },function(error){
31                 $log.debug('failed');
32         });
33         
34         
35         $scope.goToUrl = function(roleIdVal) {
36                         $state.go("root.role", {"roleId":roleIdVal});
37                 }
38                 $scope.toggleRole = function(selected,availableRole) {
39                                 var toggleType = null;
40                                 if(selected) {
41                                         toggleType = "activate";
42                                 } else {
43                                         toggleType = "inactivate";
44                                 }
45                                 if((availableRole.id == "1") || (availableRole.id =="999"))
46                                 {
47                                  confirmBoxService.showInformation(availableRole.name+" role cannot be disabled");
48                                  availableRole.active=!availableRole.active
49                      return;
50                                 }
51                                 confirmBoxService.confirm("You are about to "+toggleType+" the role "+availableRole.name+". Do you want to continue?").then(
52                                 function(confirmed){
53                                                 
54                                         if(confirmed) {
55                                         var uuu = conf.api.toggleRole;
56                                                                 
57                                                                 var postData={role:availableRole};
58                                                                 $http.post(uuu, postData).then(function(response) {
59                                                                         var data = response.data;
60                                                                         if (typeof data === 'object') {
61                                                                                 $log.debug('data:'+data);
62                                                                                 $scope.availableRoles=data.availableRoles; 
63                                                                                 $log.debug('role::availableRoles:'+$scope.availableRoles);
64                                                                         } else {
65                                                                                 //
66                                                                         }
67
68                                                                 }, function(response) {
69                                                                         debug.log('response:'+response);
70                                                                         availableRole.active=!availableRole.active;
71                                                                         confirmBoxService.showInformation("Error while saving.");
72                                                                 });
73                                                                 
74                                                  }
75
76                         }) .catch(function(err) {
77                                 $log.error('roleListController::confirmBoxService.confirm error:', err);
78                                 availableRole.active=!availableRole.active;                                     
79                         });
80                                 
81                                   
82                 };
83
84                 $scope.removeRole = function(role) {
85                         
86                         confirmBoxService.confirm("You are about to delete the role "+role.name+". Do you want to continue?").then(
87                                 function(confirmed){
88                                                         var uuu = conf.api.removeRole;
89                                                           var postData={role:role};
90
91                                                           
92                                                           
93                                                           $http.post(uuu, postData).then(function(response) {
94                                                                         var data = response.data;
95                                                                         if (typeof data === 'object') {
96                                                                                 $scope.availableRoles=data.availableRoles; 
97                                                                         } else {
98                                                                                 //
99                                                                         }
100
101                                                                 }, function(response) {
102                                                                         $log.debug('response:'+response.data);
103                                                                         confirmBoxService.showInformation("Error while deleting: "+ data.responseText);
104                                                                 });
105                                 
106                 });
107                 
108                         
109                 };
110                 
111
112 });