[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / role / rolepopupmodelController.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 app.controller('rolepopupController',  function ($scope, role, roleId, confirmBoxService, $http, $state, availableRoles, conf, availableRoleFunctions,ngDialog){\r
21         \r
22                         $scope.role = role;\r
23                         console.log($scope.role);\r
24                         if($scope.role.childRoles==null){\r
25                                 $scope.role.childRoles=[];\r
26                         }\r
27                         \r
28                         $scope.ociavailableRoles=availableRoles;\r
29                         console.log($scope.ociavailableRoles);\r
30                         \r
31                         $scope.availableRoles=[];\r
32                         if($scope.ociavailableRoles)\r
33                                 $.each($scope.ociavailableRoles, function(i, a){ \r
34                                         var availableRole = a;\r
35                                         availableRole.selected = false;\r
36                                         if($scope.role.childRoles){\r
37                                     $.each($scope.role.childRoles, function(j, b){ \r
38                                         if(a.id === b.id) {\r
39                                                 availableRole.selected = true;\r
40                                         }\r
41                                     });\r
42                                         };\r
43                                     $scope.availableRoles.push(availableRole);      \r
44                                 });     \r
45                                 \r
46                         $scope.ociavailableRoleFunctions = availableRoleFunctions; \r
47                         console.log($scope.ociavailableRoleFunctions);\r
48                         $scope.availableRoleFunctions = []; \r
49                         if($scope.ociavailableRoleFunctions)\r
50                                 $.each($scope.ociavailableRoleFunctions, function(i, a){ \r
51                                         var availableRoleFunction = a;\r
52                                         availableRoleFunction.selected = false;\r
53                                     $.each($scope.role.roleFunctions, function(j, b){ \r
54                                         if(a.code === b.code) {\r
55                                                 availableRoleFunction.selected = true;\r
56                                         }\r
57                                     });\r
58                                     $scope.availableRoleFunctions.push(availableRoleFunction);      \r
59                         });\r
60                         //$scope.resetMenu();\r
61                 \r
62                 $scope.toggleRoleFunction = function(selected,availableRoleFunction) {\r
63                         //alert('toggleRole: '+selected);\r
64                          \r
65                         if(!selected) {\r
66                                 //remove role function\r
67                                 if(role.id==null){\r
68                                         var index = $scope.role.roleFunctions.indexOf(availableRoleFunction);\r
69                                         if(index>=0)\r
70                                                 $scope.role.roleFunctions.splice(index, 1);\r
71                                         return;\r
72                                 }\r
73                                 var uuu = conf.api.toggleRoleRoleFunction + "?role_id=" + roleId;\r
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(\r
75                                         function(confirmed){\r
76                                                         if(confirmed) { \r
77                                                                 var postData={roleFunction:availableRoleFunction};\r
78                                                                 $http.post(uuu, postData).then(\r
79                                                                                 function(response) {\r
80                                                                                         $scope.role= response.data.role;\r
81                                                                                 }, \r
82                                                                                 function(response) {\r
83                                                                                         confirmBoxService.showInformation("Error while saving.");\r
84                                                                                 }\r
85                                                                 );\r
86                                                                         \r
87                                                                         \r
88                                                                         \r
89                                                                         \r
90                                                                 }\r
91                                                          else {\r
92                                                                  availableRoleFunction.selected=!availableRoleFunction.selected;\r
93                                                          }\r
94                                                                 /*\r
95                                                                   $.ajax({\r
96                                                                          type : 'POST',\r
97                                                                          url : uuu,\r
98                                                                          dataType: 'json',\r
99                                                                          contentType: 'application/json',\r
100                                                                          data: JSON.stringify(postData),\r
101                                                                          success : function(data){\r
102                                                                                 $scope.$apply(function(){$scope.role=data.role;}); \r
103                                                                          },\r
104                                                                          error : function(data){\r
105                                                                                  modalService.showFailure("Fail","Error while saving.");\r
106                                                                          }\r
107                                                                   });\r
108                                                                   */\r
109                                         });\r
110         \r
111                         } else {\r
112                                 //add role function\r
113                                 if(role.id==null){\r
114                                         $scope.role.roleFunctions.push(availableRoleFunction);\r
115                                         return;\r
116                                 }\r
117                                 var uuu = conf.api.addRoleRoleFunction + "?role_id=" + roleId;\r
118                                 \r
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(\r
120                                         function(confirmed){\r
121                                                 if(confirmed) {\r
122                                                                   var postData={roleFunction:availableRoleFunction};\r
123                                                                   $http.post(uuu,postData).then( function(response) {\r
124                                                                           $scope.role=response.data.role;\r
125                                                                           },\r
126                                                                           \r
127                                                                           function(data) {\r
128                                                                                   confirmBoxService.showInformation("Error while saving.");\r
129                                                                           });\r
130                                                         } else {\r
131                                                                           availableRoleFunction.selected=!availableRoleFunction.selected;\r
132                                                         }\r
133                                                                         /*                \r
134                                                                           $.ajax({\r
135                                                                                  type : 'POST',\r
136                                                                                  url : uuu,\r
137                                                                                  dataType: 'json',\r
138                                                                                  contentType: 'application/json',\r
139                                                                                  data: JSON.stringify(postData),\r
140                                                                                  success : function(data){\r
141                                                                                         $scope.$apply(function(){$scope.role=data.role;}); \r
142                                                                                  },\r
143                                                                                  error : function(data){\r
144                                                                                          modalService.showFailure("Fail","Error while saving.");\r
145                                                                                  }\r
146                                                                           });\r
147                                                                           \r
148                                                                         \r
149                                                         },\r
150                                                         function(){\r
151                                                                 availableRoleFunction.selected=!availableRoleFunction.selected;\r
152                                                         })*/\r
153                         });\r
154                         \r
155                           \r
156         }\r
157                 };\r
158         \r
159         $scope.toggleChildRole = function(selected,availableRole) {\r
160                 //alert('toggleRole: '+selected);\r
161 \r
162                 if(!selected) {\r
163                         //remove role\r
164                         if(role.id==null){\r
165                                 var index = $scope.role.childRoles.indexOf(availableRole);\r
166                                 if(index>=0)\r
167                                         $scope.role.childRoles.splice(index, 1);\r
168                                 return;\r
169                         }\r
170                         var uuu = conf.api.toggleRoleChildRole +"?role_id=" + roleId;\r
171                         \r
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(\r
173                                 function(confirmed){\r
174                                         if(confirmed) {\r
175                                 \r
176                                                 var postData={childRole:availableRole};\r
177                                                  $http.post(uuu,postData).then(function(response) {\r
178                                                           $scope.role=response.data.role;\r
179                                                           },\r
180                                                           function(data) {\r
181                                                                   confirmBoxService.showInformation("Error while saving.");\r
182                                                           });\r
183                                                   } else {\r
184                                                           availableRole.selected=false;\r
185                                                   }\r
186                                 });\r
187                                                  /* $.ajax({\r
188                                                          type : 'POST',\r
189                                                          url : uuu,\r
190                                                          dataType: 'json',\r
191                                                          contentType: 'application/json',\r
192                                                          data: JSON.stringify(postData),\r
193                                                          success : function(data){\r
194                                                                  console.log('role',data.role);\r
195                                                                  $scope.$apply(function(){$scope.role=data.role;}); \r
196                                                          },\r
197                                                          error : function(data){\r
198                                                                  modalService.showFailure("Fail","Error while saving.");\r
199                                                          }\r
200                                                   });\r
201                                                   */\r
202                                 \r
203                 \r
204                 } else {\r
205                         //add role\r
206                         if(role.id==null){\r
207                                 $scope.role.childRoles.push(availableRole);\r
208                                 return;\r
209                         }\r
210                         var uuu = conf.api.addRoleChildRole +"?role_id=" + roleId;\r
211                         \r
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(\r
213                                 function(confirmed){\r
214                                         if(confirmed) {\r
215                                                   var postData={childRole:availableRole};\r
216                                                   $http.post(uuu,postData).then(function(response) {\r
217                                                           $scope.role=response.data.role;\r
218                                                           },\r
219                                                           function(data) {\r
220                                                                   confirmBoxService.showInformation("Error while saving.");\r
221                                                           });\r
222                                                   } else {\r
223                                                           availableRole.selected=false;\r
224                                                   }\r
225                                  /* $.ajax({\r
226                                          type : 'POST',\r
227                                          url : uuu,\r
228                                          dataType: 'json',\r
229                                          contentType: 'application/json',\r
230                                          data: JSON.stringify(postData),\r
231                                          success : function(data){\r
232                                                 $scope.$apply(function(){$scope.role=data.role;}); \r
233                                          },\r
234                                          error : function(data){\r
235                                                  modalService.showFailure("Fail","Error while saving.");\r
236                                          }\r
237                                 });*/\r
238                                 \r
239                 });\r
240                 \r
241                   \r
242         }\r
243         };\r
244         \r
245         $scope.close = function() {\r
246                 console.log('role', $scope.role);\r
247                 //$modalInstance.close({role:$scope.role});\r
248                 this.closeThisDialog(true);\r
249         };\r
250         \r
251 });\r