2e1acf371ecae800230aea9367b10fa8efed44aa
[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){
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                 //$scope.resetMenu();
30         
31         },function(error){
32                 console.log("failed");
33                 //reloadPageOnce();
34         });
35         
36         
37                 $scope.goToUrl = function(roleIdVal) {
38                         $state.go("root.role", {"roleId":roleIdVal});
39                 }       
40         //console.log($scope.availableRoles);
41                 $scope.toggleRole = function(selected,availableRole) {
42                                 //alert('toggleRole: '+selected);
43                                 var toggleType = null;
44                                 if(selected) {
45                                         toggleType = "activate";
46                                 } else {
47                                         toggleType = "inactivate";
48                                 }
49                                 
50                                 confirmBoxService.confirm("You are about to "+toggleType+" the role "+availableRole.name+". Do you want to continue?").then(
51                                         function(confirmed){
52                                                 
53                                                  if(confirmed) {
54                                             var uuu = conf.api.toggleRole;
55                                                                 
56                                                                 var postData={role:availableRole};
57                                                                 $http.post(uuu, postData).then(function(response) {
58                                                                         var data = response.data;
59                                                                         if (typeof data === 'object') {
60                                                                                 console.log(data);
61                                                                                 $scope.availableRoles=data.availableRoles; 
62                                                                                 console.log($scope.availableRoles);
63                                                                         } else {
64                                                                                 //
65                                                                         }
66
67                                                                 }, function(response) {
68                                                                         console.log(response.data);
69                                                                         availableRole.active=!availableRole.active;
70                                                                         confirmBoxService.showInformation("Error while saving.");
71                                                                 });
72                                                                 
73                                                                 /*
74                                                                   $.ajax({
75                                                                          type : 'POST',
76                                                                          url : uuu,
77                                                                          dataType: 'json',
78                                                                          contentType: 'application/json',
79                                                                          data: JSON.stringify(postData),
80                                                                          success : function(data){
81                                                                                 console.log(data);
82                                                                                 $scope.$apply(function(){$scope.availableRoles=data.availableRoles;}); 
83                                                                                 console.log($scope.availableRoles);
84                                                                          },
85                                                                          error : function(data){
86                                                                                  console.log(data);
87                                                                                  availableRole.active=!availableRole.active;
88                                                                                  confirmBoxService.showInformation("Error while saving.");
89                                                                          }
90                                                                   });
91                                                                   */
92                                                  }
93                                                  else {
94                                                          availableRole.active=!availableRole.active;
95                                                  }
96                                         
97                         });
98                         //,
99                         //function(){
100                     //          availableRole.active=!availableRole.active;
101                         //})
102                                 
103                                   
104                 };
105
106                 $scope.removeRole = function(role) {
107                         
108                         confirmBoxService.confirm("You are about to delete the role "+role.name+". Do you want to continue?").then(
109                                 function(confirmed){
110                                                         var uuu = conf.api.removeRole;
111                                                           var postData={role:role};
112                                                   /*      $.ajax({
113                                                                  type : 'POST',
114                                                                  url : uuu,
115                                                                  dataType: 'json',
116                                                                  contentType: 'application/json',
117                                                                  data: JSON.stringify(postData),
118                                                                  success : function(data){
119                                                                         $scope.$apply(function(){$scope.availableRoles=data.availableRoles;});  
120                                                                  },
121                                                                  error : function(data){
122                                                                          console.log(data);
123                                                                          confirmBoxService.showInformation("Error while deleting: "+ data.responseText);
124                                                                  }
125                                                           }); */
126                                                           
127                                                           
128                                                           $http.post(uuu, postData).then(function(response) {
129                                                                         var data = response.data;
130                                                                         if (typeof data === 'object') {
131                                                                                 $scope.availableRoles=data.availableRoles; 
132                                                                         } else {
133                                                                                 //
134                                                                         }
135
136                                                                 }, function(response) {
137                                                                         console.log(response.data);
138                                                                         confirmBoxService.showInformation("Error while deleting: "+ data.responseText);
139                                                                 });
140                                 
141                 });
142                 
143                         
144                 };
145                 
146
147 });