Deliver centralized role management feature
[portal.git] / ecomp-portal-FE-common / client / app / views / role / rolepopupmodelController.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('rolepopupController',  function ($scope, role, roleId, confirmBoxService, $http, $state, availableRoles, conf, availableRoleFunctions,ngDialog){
21         
22                         $scope.role = role;
23                         console.log($scope.role);
24                         if($scope.role.childRoles==null){
25                                 $scope.role.childRoles=[];
26                         }
27                         
28                         $scope.ociavailableRoles=availableRoles;
29                         console.log($scope.ociavailableRoles);
30                         
31                         $scope.availableRoles=[];
32                         if($scope.ociavailableRoles)
33                                 $.each($scope.ociavailableRoles, function(i, a){ 
34                                         var availableRole = a;
35                                         availableRole.selected = false;
36                                         if($scope.role.childRoles){
37                                     $.each($scope.role.childRoles, function(j, b){ 
38                                         if(a.id === b.id) {
39                                                 availableRole.selected = true;
40                                         }
41                                     });
42                                         };
43                                     $scope.availableRoles.push(availableRole);      
44                                 });     
45                                 
46                         $scope.ociavailableRoleFunctions = availableRoleFunctions; 
47                         console.log($scope.ociavailableRoleFunctions);
48                         $scope.availableRoleFunctions = []; 
49                         if($scope.ociavailableRoleFunctions)
50                                 $.each($scope.ociavailableRoleFunctions, function(i, a){ 
51                                         var availableRoleFunction = a;
52                                         availableRoleFunction.selected = false;
53                                     $.each($scope.role.roleFunctions, function(j, b){ 
54                                         if(a.code === b.code) {
55                                                 availableRoleFunction.selected = true;
56                                         }
57                                     });
58                                     $scope.availableRoleFunctions.push(availableRoleFunction);      
59                         });
60                         //$scope.resetMenu();
61                 
62                 $scope.toggleRoleFunction = function(selected,availableRoleFunction) {
63                         //alert('toggleRole: '+selected);
64                          
65                         if(!selected) {
66                                 //remove role function
67                                 if(role.id==null){
68                                         var index = $scope.role.roleFunctions.indexOf(availableRoleFunction);
69                                         if(index>=0)
70                                                 $scope.role.roleFunctions.splice(index, 1);
71                                         return;
72                                 }
73                                 var uuu = conf.api.toggleRoleRoleFunction + "?role_id=" + roleId;
74                                 confirmBoxService.confirm("You are about to remove the role function "+availableRoleFunction.name+" from the role for "+$scope.role.name+". Do you want to continue?").then(
75                                         function(confirmed){
76                                                         if(confirmed) { 
77                                                                 var postData={roleFunction:availableRoleFunction};
78                                                                 $http.post(uuu, postData).then(
79                                                                                 function(response) {
80                                                                                         $scope.role= response.data.role;
81                                                                                 }, 
82                                                                                 function(response) {
83                                                                                         confirmBoxService.showInformation("Error while saving.");
84                                                                                 }
85                                                                 );
86                                                                         
87                                                                         
88                                                                         
89                                                                         
90                                                                 }
91                                                                 /*
92                                                                   $.ajax({
93                                                                          type : 'POST',
94                                                                          url : uuu,
95                                                                          dataType: 'json',
96                                                                          contentType: 'application/json',
97                                                                          data: JSON.stringify(postData),
98                                                                          success : function(data){
99                                                                                 $scope.$apply(function(){$scope.role=data.role;}); 
100                                                                          },
101                                                                          error : function(data){
102                                                                                  modalService.showFailure("Fail","Error while saving.");
103                                                                          }
104                                                                   });
105                                                                   */
106                                         }).catch(function(err) {
107                                                           availableRoleFunction.selected=!availableRoleFunction.selected;
108                                                         $log.error('roleListController::confirmBoxService.confirm error:', err);
109
110                                         });
111         
112                         } else {
113                                 //add role function
114                                 if(role.id==null){
115                                         $scope.role.roleFunctions.push(availableRoleFunction);
116                                         return;
117                                 }
118                                 var uuu = conf.api.addRoleRoleFunction + "?role_id=" + roleId;
119                                 
120                                 confirmBoxService.confirm("You are about to add the role function "+availableRoleFunction.name+" to the role for "+$scope.role.name+". Do you want to continue?").then(
121                                         function(confirmed){
122                                                 if(confirmed) {
123                                                                   var postData={roleFunction:availableRoleFunction};
124                                                                   $http.post(uuu,postData).then( function(response) {
125                                                                           $scope.role=response.data.role;
126                                                                           },
127                                                                           
128                                                                           function(data) {
129                                                                                   confirmBoxService.showInformation("Error while saving.");
130                                                                           });
131                                                         }       /*                
132                                                                           $.ajax({
133                                                                                  type : 'POST',
134                                                                                  url : uuu,
135                                                                                  dataType: 'json',
136                                                                                  contentType: 'application/json',
137                                                                                  data: JSON.stringify(postData),
138                                                                                  success : function(data){
139                                                                                         $scope.$apply(function(){$scope.role=data.role;}); 
140                                                                                  },
141                                                                                  error : function(data){
142                                                                                          modalService.showFailure("Fail","Error while saving.");
143                                                                                  }
144                                                                           });
145                                                                           
146                                                                         
147                                                         },
148                                                         function(){
149                                                                 availableRoleFunction.selected=!availableRoleFunction.selected;
150                                                         })*/
151                         }).catch(function(err) {
152                                   availableRoleFunction.selected=!availableRoleFunction.selected;
153                                 $log.error('roleListController::confirmBoxService.confirm error:', err);
154                 });
155                         
156                           
157         }
158                 };
159         
160         $scope.toggleChildRole = function(selected,availableRole) {
161                 //alert('toggleRole: '+selected);
162
163                 if(!selected) {
164                         //remove role
165                         if(role.id==null){
166                                 var index = $scope.role.childRoles.indexOf(availableRole);
167                                 if(index>=0)
168                                         $scope.role.childRoles.splice(index, 1);
169                                 return;
170                         }
171                         var uuu = conf.api.toggleRoleChildRole +"?role_id=" + roleId;
172                         
173                         confirmBoxService.confirm("You are about to remove the child role "+availableRole.name+" from the role for "+$scope.role.name+". Do you want to continue?").then(
174                                 function(confirmed){
175                                         if(confirmed) {
176                                 
177                                                 var postData={childRole:availableRole};
178                                                  $http.post(uuu,postData).then(function(response) {
179                                                           $scope.role=response.data.role;
180                                                           },
181                                                           function(data) {
182                                                                   confirmBoxService.showInformation("Error while saving.");
183                                                           });
184                                                   }
185                                 }).catch(function(err) {
186                                         availableRole.selected=!availableRole.selected;
187                                         $log.error('roleListController::confirmBoxService.confirm error:', err);
188                         });
189                                                  /* $.ajax({
190                                                          type : 'POST',
191                                                          url : uuu,
192                                                          dataType: 'json',
193                                                          contentType: 'application/json',
194                                                          data: JSON.stringify(postData),
195                                                          success : function(data){
196                                                                  console.log('role',data.role);
197                                                                  $scope.$apply(function(){$scope.role=data.role;}); 
198                                                          },
199                                                          error : function(data){
200                                                                  modalService.showFailure("Fail","Error while saving.");
201                                                          }
202                                                   });
203                                                   */
204                                 
205                 
206                 } else {
207                         //add role
208                         if(role.id==null){
209                                 $scope.role.childRoles.push(availableRole);
210                                 return;
211                         }
212                         var uuu = conf.api.addRoleChildRole +"?role_id=" + roleId;
213                         
214                         confirmBoxService.confirm("You are about to add the child role "+availableRole.name+" to the role for "+$scope.role.name+". Do you want to continue?").then(
215                                 function(confirmed){
216                                         if(confirmed) {
217                                                   var postData={childRole:availableRole};
218                                                   $http.post(uuu,postData).then(function(response) {
219                                                           $scope.role=response.data.role;
220                                                           },
221                                                           function(data) {
222                                                                   confirmBoxService.showInformation("Error while saving.");
223                                                           });
224                                                   }
225                                  /* $.ajax({
226                                          type : 'POST',
227                                          url : uuu,
228                                          dataType: 'json',
229                                          contentType: 'application/json',
230                                          data: JSON.stringify(postData),
231                                          success : function(data){
232                                                 $scope.$apply(function(){$scope.role=data.role;}); 
233                                          },
234                                          error : function(data){
235                                                  modalService.showFailure("Fail","Error while saving.");
236                                          }
237                                 });*/
238                                 
239                 }).catch(function(err) {
240                                 availableRole.selected=!availableRole.selected;
241                         $log.error('roleListController::confirmBoxService.confirm error:', err);
242         });
243                 
244                   
245         }
246         };
247         
248         $scope.close = function() {
249                 console.log('role', $scope.role);
250                 //$modalInstance.close({role:$scope.role});
251                 this.closeThisDialog(true);
252         };
253         
254 });