Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / fusion / scripts / controllers / fn_menu_add_popup_controller.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 fn_menu_popupController =  function ($scope, $modalInstance, message, $http){ /// examine the LeftMenuService
21                                 if(message.availableFnMenuItem==null)
22                                         $scope.label='Add New Menu Item'
23                                                 
24                                 else{
25                                         $scope.label='Edit Menu Item'
26                                         //$scope.disableParentId=true;
27                                 }
28                                 
29                                 $scope.getParentData = function(){
30                                         var uuu = "admin_fn_menu/get_parent_list"
31                                                   $.ajax({
32                                                                  type : 'GET',
33                                                                  url : uuu,
34                                                                  dataType: 'json',                                                      // data type expected from server
35                                                                  contentType: 'application/json',  
36                                                                  //data: JSON.stringify(postData),                      // data type sent to server     
37                                                                  success : function(data){
38                                                                         $scope.$apply(function(){
39                                                                                 //$scope.availableRoleFunctions=[];$scope.$apply();
40                                                                                 $scope.parentListSelectData=data;               // data from server
41                                                                                 menuItems = $scope.parentListSelectData;
42                                                                                 var heirarchicalMenuItems = [];
43                                                                                 var children = [];
44                                                                                 for ( var i=0; i<menuItems.length; i++){
45                                                                                         for(var j=0; j<menuItems.length; j++){
46                                                                                                 if(menuItems[j][2]==menuItems[i][0]) 
47                                                                                                         children.push(
48                                                                                                                         {
49                                                                                                                         menuId: menuItems[j][0],
50                                                                                                                         label:  menuItems[j][1]
51                                                                                                                         }
52                                                                                                         );
53                                                                                         }
54                                                                                         if(children.length!=0){
55                                                                                         heirarchicalMenuItems.push(
56                                                                                                         {
57                                                                                                                 
58                                                                                                                         menuId: menuItems[i][0],
59                                                                                                                         label:  menuItems[i][1],
60                                                                                                                         children: children.sort($scope.sortItems("label"))
61                                                                                                         }
62                                                                                         
63                                                                                         );
64                                                                                         }
65                                                                                         children = [];
66                                                                                         
67                                                                                 }
68                                                                                 heirarchicalMenuItems.sort($scope.sortItems("label"));
69                                                                                 $scope.childListSelectData = heirarchicalMenuItems;
70                                                                                 //console.log(data);
71                                                                         });  
72                                                                         //$scope.editRoleFunction = null;
73                                                                         ///$modalInstance.close({functionCDselectData:$scope.functionCDselectData});
74                                                                  },
75                                                                  error : function(data){
76                                                                          alert("Parent Data not available !"); 
77                                                                  }
78                                                           });
79                                         
80                                 };
81                                 
82                                 $scope.sortItems = function(prop){
83                                         return function(a, b) {  
84                                         if (a[prop] > b[prop]) {  
85                                             return 1;  
86                                         } else if (a[prop] < b[prop]) {  
87                                             return -1;  
88                                         }  
89                                         return 0;  
90                                     }  
91                                                                         
92                                 };
93                                 
94                                 $scope.getParentLabel = function(parentId, parentListSelectData){
95                                         var element;
96                                         element = parentListSelectData[0];
97                                     for (var i=0; i<parentListSelectData.length; i++){
98                                         
99                                         element = parentListSelectData[i];
100                                         if (element[0] == parentId)                    
101                                             return element[1];
102                                         else "---";
103                                     }
104                                 };
105
106
107                                 $scope.getFunctionCDselectData = function(){
108                                         var uuu = "admin_fn_menu/get_function_cd_list"
109                                                   $.ajax({
110                                                                  type : 'GET',
111                                                                  url : uuu,
112                                                                  dataType: 'json',                              // data type expected from server
113                                                                  contentType: 'application/json',  
114                                                                  //data: JSON.stringify(postData),   // data type sent to server        
115                                                                  success : function(data){
116                                                                         $scope.$apply(function(){
117                                                                                 //$scope.availableRoleFunctions=[];$scope.$apply();
118                                                                                 $scope.functionCDselectData=data;
119                                                                         });  
120                                                                         //alert("Update Successful.") ;
121                                                                         //$scope.editRoleFunction = null;
122                                                                         ///$modalInstance.close({functionCDselectData:$scope.functionCDselectData});
123                                                                  },
124                                                                  error : function(data){
125                                                                          alert("Function Code Data not available !"); 
126                                                                  }
127                                                           });
128                                         
129                                 };
130                                 
131                                 if(message.availableFnMenuItem){
132                                         $scope.addFnMenuItem = message.availableFnMenuItem;
133                                 } else {
134                                         $scope.addFnMenuItem = {};
135                                 }
136                                 $scope.addFnMenuItem.menuSetCode = "APP";
137                                 
138                             $scope.statusOptions = [        
139                                                         {index: 0, value: 'true', title: 'Y'},
140                                                         {index: 1, value: 'false', title: 'N'}
141                                                     ];
142                                 $scope.selectedValue = $scope.statusOptions[0];
143                                 
144                                 $scope.separator = {
145                                                         availableOptions:[
146                                                                                             {value: 'true',  name: 'Y'},
147                                                                                             {value: 'false', name: 'N'}
148                                                                                          ],
149                                                                                          
150                                                                                          selectedOption: {value: 'true', name: 'Y'}
151                                                               
152                                                                         };
153                                         
154                                         
155                             $scope.activeStatusOptions = [        
156                                                         {index: 0, value: 'true', title: 'Y'},
157                                                         {index: 1, value: 'false', title: 'N'}
158                                                     ];
159                             $scope.activeSelectedValue = $scope.activeStatusOptions[0];
160                                     
161                             $scope.separatorStatusOptions = [        
162                                                                                 {index: 0, value: 'true', title: 'Y'},
163                                                                                 {index: 1, value: 'false', title: 'N'}
164                                                                             ];
165                             $scope.separatorSelectedValue = $scope.separatorStatusOptions[1];                               
166                                 
167                                 $scope.active = {
168                                                         availableOptions:[
169                                                                                             {value: 'true',  name: 'Y'},
170                                                                                             {value: 'false', name: 'N'}
171                                                                                          ],
172                                                                                          
173                                                                                          selectedOption: {value: 'true', name: 'Y'}
174                                                               
175                                                                         };
176                                 
177                                 
178                                 $scope.updateFnMenu = function(availableFnMenuItem) { 
179                                                 // form validation
180                                                 if( document.getElementById("popupAddMenuItemImageSrc").value == "" ) availableFnMenuItem.imageSrc = "N/A";
181                                                 if( document.getElementById("popupAddMenuItemTarget").value == "" ) availableFnMenuItem.target = "N/A";
182                                                 if( document.getElementById("popupAddMenuItemExternalUrl").value == "" ) availableFnMenuItem.externalUrl = "N/A";
183                                                 if( document.getElementById("popupAddMenuItemQueryString").value == "" ) availableFnMenuItem.queryString = "N/A";
184                                                 if( document.getElementById("popupAddMenuItemServlet").value == "" ) availableFnMenuItem.servlet = "N/A";
185                                                 
186                                                 validationRule = /^\S{3,}$/;
187                                                 var selectedFunction = document.getElementById("repeatSelect");
188                                                 var selectedFunctionText = selectedFunction.options[selectedFunction.selectedIndex].text;
189                                                 if(
190                                                                 (document.getElementById("popupAddMenuItemLabel").value == "" || document.getElementById("popupAddMenuItemLabel").value.replace(" ", "").length == 0 ) ||//!validationRule.test(document.getElementById("popupAddMenuItemLabel").value)) ||
191                                                                 document.getElementById("popupAddMenuItemParentId").value == "" ||
192                                                                 document.getElementById("popupAddMenuItemAction").value == "" ||
193                                                                 selectedFunctionText == "" ||
194                                                                 document.getElementById("popupAddMenuItemSortOrder").value == "" ||
195                                                                 document.getElementById("popupAddMenuItemMenuSetCode").value == ""
196                                                   )     
197                                                         {
198                                                                 alert("please provide valid entries !");  
199                                                         }
200                                                 
201                                                 else{ 
202                                                                   var uuu = "admin_fn_menu/updateFnMenu.htm";
203                                                                   var postData={availableFnMenuItem: availableFnMenuItem};
204                                                                   
205                                                                   $.ajax({
206                                                                          type : 'POST',
207                                                                          url : uuu,
208                                                                          //dataType: 'json',                            // data type expected from server
209                                                                          contentType: 'application/json',  
210                                                                          data: JSON.stringify(postData),    // data type sent to server 
211                                                                          success : function(data){
212                                                                                 $scope.$apply(function(){ 
213                                                                                 $scope.availableFnMenuItems=data.availableFnMenuItems;
214                                                                                 });
215                                                                                 
216                                                                                 $modalInstance.close({availableFnMenuItems:$scope.availableRoleFunctions});
217                                                                          },
218                                                                          error : function(data){
219                                                                                  alert("Error while saving."); 
220                                                                          }
221                                                                   }); 
222
223                                                         }
224                                                 $scope.getLeftMenuItems();                                              // get left menu items again from database
225
226                                         };
227
228                                         $scope.getLeftMenuItems = function() {
229                                                                                                                                 return $http.get('get_menu')
230                                                                                                                                 .then(function(response) {
231                                                                                                                                                 if (typeof response.data === 'object') {
232                                                                                                                                                 var leftChildData=[];
233                                                                                                                                                 var leftParentData=[];
234                                                                                                                                                 var leftMenuItems = [];
235                                                                                                                                                         var j = response; 
236                                                                                                                                                 try{
237                                                                                                                                                         if(j && j !== "null" && j!== "undefined"){
238                                                                                                                                                                 leftParentData = JSON.parse(j.data);
239                                                                                                                                                     leftChildData = JSON.parse(j.data2);
240                                                                                                                                                         }else{
241                                                                                                                                                                 throw "Get Left Menu respsone is not an object/is empty"; 
242                                                                                                                                                         }  
243                                                                                                                                                 try{
244                                                                                                                                                                 var leftChildItemList = leftChildData;                                                                                  
245                                                                                                                                                         var pageUrl = window.location.href.split('/')[window.location.href.split('/').length-1];
246                                                                                                                                                         var leftParentList = leftParentData;                                                                                            
247                                                                                                                                                         for (var i = 0; i < leftParentList.length; i++) {
248                                                                                                                                                                 $scope.item = {
249                                                                                                                                                                         parentLabel : leftParentList[i].label,
250                                                                                                                                                                         parentAction : leftParentList[i].action,
251                                                                                                                                                                         parentImageSrc : leftParentList[i].imageSrc,                                            
252                                                                                                                                                                         open:pageUrl==leftParentList[i].action?true:false,
253                                                                                                                                                                         childItemList : leftChildItemList[i]==null?'x':leftChildItemList[i]
254                                                                                                                                                                 }
255                                                                                                                                                                 leftMenuItems.push($scope.item);                                                                                        
256                                                                                                                                                         };
257                                                                                                                                                         
258                                                                                                                                                         $scope.leftMenuItems = leftMenuItems;
259                                                                                 
260                                                                                                                                                         }catch(err){
261                                                                                                                                                                 console.log("error happened while trying to set left menu structure: "+err);                                       
262                                                                                                                                                         }
263                                                                                                                                                 }catch (e) {
264                                                                                                                                                         console.log("error happened while trying to get left menu items: "+e);
265                                                                                                                                                         reloadPageOnce();
266                                                                                                                                                         return;
267                                                                                                                                         }       
268                                                                                                                                                         return response.data;
269                                                                                                                                                 } else {
270                                                                                                                                                         return $q.reject(response.data);
271                                                                                                                                                 }
272                                                                                                                                 }, function(response) {
273                                                                                                                                         // something went wrong
274                                                                                                                                         return $q.reject(response.data);
275                                                                                                                                 });
276                                         };
277
278                                 $scope.close = function() {
279                                         $modalInstance.close();
280                                 };
281 }