Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / fusion / scripts / controllers / rolepopupmodelController.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
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 var rolepopupController =  function ($scope, $modalInstance, role, roleId, availableRoles, availableRoleFunctions,AdminService,modalService){
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 = "role/removeRoleFunction.htm?role_id=" + roleId;
74                                 modalService.popupConfirmWinWithCancel("Confirm","You are about to remove the role function "+availableRoleFunction.name+" from the role for "+$scope.role.name+". Do you want to continue?",
75                                         function(){
76                                                                 var postData={roleFunction:availableRoleFunction};
77                                                                   $.ajax({
78                                                                          type : 'POST',
79                                                                          url : uuu,
80                                                                          dataType: 'json',
81                                                                          contentType: 'application/json',
82                                                                          data: JSON.stringify(postData),
83                                                                          success : function(data){
84                                                                                 $scope.$apply(function(){$scope.role=data.role;}); 
85                                                                          },
86                                                                          error : function(data){
87                                                                                  modalService.showFailure("Fail","Error while saving.");
88                                                                          }
89                                                                   });
90                                         
91                         },
92                         function(){
93                                 availableRoleFunction.selected=!availableRoleFunction.selected;
94                         })
95         
96                         } else {
97                                 //add role function
98                                 if(role.id==null){
99                                         $scope.role.roleFunctions.push(availableRoleFunction);
100                                         return;
101                                 }
102                                 var uuu = "role/addRoleFunction.htm?role_id=" + roleId;
103                                 
104                                 modalService.popupConfirmWinWithCancel("Confirm","You are about to add the role function "+availableRoleFunction.name+" to the role for "+$scope.role.name+". Do you want to continue?",
105                                         function(){
106                                           var postData={roleFunction:availableRoleFunction};
107                                           $.ajax({
108                                                  type : 'POST',
109                                                  url : uuu,
110                                                  dataType: 'json',
111                                                  contentType: 'application/json',
112                                                  data: JSON.stringify(postData),
113                                                  success : function(data){
114                                                         $scope.$apply(function(){$scope.role=data.role;}); 
115                                                  },
116                                                  error : function(data){
117                                                          modalService.showFailure("Fail","Error while saving.");
118                                                  }
119                                           });
120                                         
121                         },
122                         function(){
123                                 availableRoleFunction.selected=!availableRoleFunction.selected;
124                         })
125                         }
126                         
127                           
128         };
129         
130         $scope.toggleChildRole = function(selected,availableRole) {
131                 //alert('toggleRole: '+selected);
132
133                 if(!selected) {
134                         //remove role
135                         if(role.id==null){
136                                 var index = $scope.role.childRoles.indexOf(availableRole);
137                                 if(index>=0)
138                                         $scope.role.childRoles.splice(index, 1);
139                                 return;
140                         }
141                         var uuu = "role/removeChildRole.htm?role_id=" + roleId;
142                         
143                         modalService.popupConfirmWinWithCancel("Confirm","You are about to remove the child role "+availableRole.name+" from the role for "+$scope.role.name+". Do you want to continue?",
144                                 function(){
145                                                 var postData={childRole:availableRole};
146                                                   $.ajax({
147                                                          type : 'POST',
148                                                          url : uuu,
149                                                          dataType: 'json',
150                                                          contentType: 'application/json',
151                                                          data: JSON.stringify(postData),
152                                                          success : function(data){
153                                                                  console.log('role',data.role);
154                                                                  $scope.$apply(function(){$scope.role=data.role;}); 
155                                                          },
156                                                          error : function(data){
157                                                                  modalService.showFailure("Fail","Error while saving.");
158                                                          }
159                                                   });
160                                 
161                 },
162                 function(){
163                          availableRole.selected=true;                   
164                 })
165                 
166                 } else {
167                         //add role
168                         if(role.id==null){
169                                 $scope.role.childRoles.push(availableRole);
170                                 return;
171                         }
172                         var uuu = "role/addChildRole.htm?role_id=" + roleId;
173                         
174                         modalService.popupConfirmWinWithCancel("Confirm","You are about to add the child role "+availableRole.name+" to the role for "+$scope.role.name+". Do you want to continue?",
175                                 function(){
176                                   var postData={childRole:availableRole};
177                                   $.ajax({
178                                          type : 'POST',
179                                          url : uuu,
180                                          dataType: 'json',
181                                          contentType: 'application/json',
182                                          data: JSON.stringify(postData),
183                                          success : function(data){
184                                                 $scope.$apply(function(){$scope.role=data.role;}); 
185                                          },
186                                          error : function(data){
187                                                  modalService.showFailure("Fail","Error while saving.");
188                                          }
189                                 });
190                                 
191                 },
192                 function(){
193                         availableRole.selected=false;
194                 })
195                 }
196                 
197                   
198         };
199         
200         $scope.close = function() {
201                 console.log('role', $scope.role);
202                 $modalInstance.close({role:$scope.role});
203         };
204         
205 }