[PORTAL-16 PORTAL-18] Widget ms; staging
[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                                                          else {
92                                                                  availableRoleFunction.selected=!availableRoleFunction.selected;
93                                                          }
94                                                                 /*
95                                                                   $.ajax({
96                                                                          type : 'POST',
97                                                                          url : uuu,
98                                                                          dataType: 'json',
99                                                                          contentType: 'application/json',
100                                                                          data: JSON.stringify(postData),
101                                                                          success : function(data){
102                                                                                 $scope.$apply(function(){$scope.role=data.role;}); 
103                                                                          },
104                                                                          error : function(data){
105                                                                                  modalService.showFailure("Fail","Error while saving.");
106                                                                          }
107                                                                   });
108                                                                   */
109                                         });
110         
111                         } else {
112                                 //add role function
113                                 if(role.id==null){
114                                         $scope.role.roleFunctions.push(availableRoleFunction);
115                                         return;
116                                 }
117                                 var uuu = conf.api.addRoleRoleFunction + "?role_id=" + roleId;
118                                 
119                                 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(
120                                         function(confirmed){
121                                                 if(confirmed) {
122                                                                   var postData={roleFunction:availableRoleFunction};
123                                                                   $http.post(uuu,postData).then( function(response) {
124                                                                           $scope.role=response.data.role;
125                                                                           },
126                                                                           
127                                                                           function(data) {
128                                                                                   confirmBoxService.showInformation("Error while saving.");
129                                                                           });
130                                                         } else {
131                                                                           availableRoleFunction.selected=!availableRoleFunction.selected;
132                                                         }
133                                                                         /*                
134                                                                           $.ajax({
135                                                                                  type : 'POST',
136                                                                                  url : uuu,
137                                                                                  dataType: 'json',
138                                                                                  contentType: 'application/json',
139                                                                                  data: JSON.stringify(postData),
140                                                                                  success : function(data){
141                                                                                         $scope.$apply(function(){$scope.role=data.role;}); 
142                                                                                  },
143                                                                                  error : function(data){
144                                                                                          modalService.showFailure("Fail","Error while saving.");
145                                                                                  }
146                                                                           });
147                                                                           
148                                                                         
149                                                         },
150                                                         function(){
151                                                                 availableRoleFunction.selected=!availableRoleFunction.selected;
152                                                         })*/
153                         });
154                         
155                           
156         }
157                 };
158         
159         $scope.toggleChildRole = function(selected,availableRole) {
160                 //alert('toggleRole: '+selected);
161
162                 if(!selected) {
163                         //remove role
164                         if(role.id==null){
165                                 var index = $scope.role.childRoles.indexOf(availableRole);
166                                 if(index>=0)
167                                         $scope.role.childRoles.splice(index, 1);
168                                 return;
169                         }
170                         var uuu = conf.api.toggleRoleChildRole +"?role_id=" + roleId;
171                         
172                         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(
173                                 function(confirmed){
174                                         if(confirmed) {
175                                 
176                                                 var postData={childRole:availableRole};
177                                                  $http.post(uuu,postData).then(function(response) {
178                                                           $scope.role=response.data.role;
179                                                           },
180                                                           function(data) {
181                                                                   confirmBoxService.showInformation("Error while saving.");
182                                                           });
183                                                   } else {
184                                                           availableRole.selected=false;
185                                                   }
186                                 });
187                                                  /* $.ajax({
188                                                          type : 'POST',
189                                                          url : uuu,
190                                                          dataType: 'json',
191                                                          contentType: 'application/json',
192                                                          data: JSON.stringify(postData),
193                                                          success : function(data){
194                                                                  console.log('role',data.role);
195                                                                  $scope.$apply(function(){$scope.role=data.role;}); 
196                                                          },
197                                                          error : function(data){
198                                                                  modalService.showFailure("Fail","Error while saving.");
199                                                          }
200                                                   });
201                                                   */
202                                 
203                 
204                 } else {
205                         //add role
206                         if(role.id==null){
207                                 $scope.role.childRoles.push(availableRole);
208                                 return;
209                         }
210                         var uuu = conf.api.addRoleChildRole +"?role_id=" + roleId;
211                         
212                         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(
213                                 function(confirmed){
214                                         if(confirmed) {
215                                                   var postData={childRole:availableRole};
216                                                   $http.post(uuu,postData).then(function(response) {
217                                                           $scope.role=response.data.role;
218                                                           },
219                                                           function(data) {
220                                                                   confirmBoxService.showInformation("Error while saving.");
221                                                           });
222                                                   } else {
223                                                           availableRole.selected=false;
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                 });
240                 
241                   
242         }
243         };
244         
245         $scope.close = function() {
246                 console.log('role', $scope.role);
247                 //$modalInstance.close({role:$scope.role});
248                 this.closeThisDialog(true);
249         };
250         
251 });