aboutsummaryrefslogtreecommitdiffstats
path: root/ecomp-sdk-app/src/main/webapp/app/fusion/scripts/controllers/fn_menu_add_popup_controller.js
blob: e0179946f35b003d05610921edd6f8d9e58d42a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*-
 * ================================================================================
 * eCOMP Portal SDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
var fn_menu_popupController =  function ($scope, $modalInstance, message, $http){ /// examine the LeftMenuService
				if(message.availableFnMenuItem==null)
					$scope.label='Add New Menu Item'
						
				else{
					$scope.label='Edit Menu Item'
					//$scope.disableParentId=true;
				}
				
				$scope.getParentData = function(){
					var uuu = "admin_fn_menu/get_parent_list"
					  	  $.ajax({
						  		 type : 'GET',
						  		 url : uuu,
						  		 dataType: 'json',				     			// data type expected from server
						  		 contentType: 'application/json',  
						  		 //data: JSON.stringify(postData),   			// data type sent to server	
						  		 success : function(data){
						  			$scope.$apply(function(){
						  				//$scope.availableRoleFunctions=[];$scope.$apply();
						  				$scope.parentListSelectData=data;		// data from server
						  				menuItems = $scope.parentListSelectData;
						  				var heirarchicalMenuItems = [];
						  				var children = [];
						  				for ( var i=0; i<menuItems.length; i++){
						  					for(var j=0; j<menuItems.length; j++){
						  						if(menuItems[j][2]==menuItems[i][0]) 
						  							children.push(
						  									{
						  									menuId: menuItems[j][0],
						  									label:  menuItems[j][1]
						  									}
						  							);
						  					}
						  					if(children.length!=0){
						  					heirarchicalMenuItems.push(
						  							{
					  									
						  									menuId: menuItems[i][0],
						  									label:  menuItems[i][1],
							  								children: children.sort($scope.sortItems("label"))
						  							}
						  					
						  					);
						  					}
						  					children = [];
						  					
						  				}
						  				heirarchicalMenuItems.sort($scope.sortItems("label"));
						  				$scope.childListSelectData = heirarchicalMenuItems;
						  				//console.log(data);
						  			});  
						  			//$scope.editRoleFunction = null;
						  			///$modalInstance.close({functionCDselectData:$scope.functionCDselectData});
								 },
								 error : function(data){
									 alert("Parent Data not available !"); 
								 }
						  	  });
					
				};
				
				$scope.sortItems = function(prop){
					return function(a, b) {  
				        if (a[prop] > b[prop]) {  
				            return 1;  
				        } else if (a[prop] < b[prop]) {  
				            return -1;  
				        }  
				        return 0;  
				    }  
									
				};
				
				$scope.getParentLabel = function(parentId, parentListSelectData){
					var element;
					element = parentListSelectData[0];
				    for (var i=0; i<parentListSelectData.length; i++){
				    	
				    	element = parentListSelectData[i];
				        if (element[0] == parentId)                    
				            return element[1];
				        else "---";
				    }
				};


				$scope.getFunctionCDselectData = function(){
					var uuu = "admin_fn_menu/get_function_cd_list"
					  	  $.ajax({
						  		 type : 'GET',
						  		 url : uuu,
						  		 dataType: 'json',				// data type expected from server
						  		 contentType: 'application/json',  
						  		 //data: JSON.stringify(postData),   // data type sent to server	
						  		 success : function(data){
						  			$scope.$apply(function(){
						  				//$scope.availableRoleFunctions=[];$scope.$apply();
						  				$scope.functionCDselectData=data;
						  			});  
						  			//alert("Update Successful.") ;
						  			//$scope.editRoleFunction = null;
						  			///$modalInstance.close({functionCDselectData:$scope.functionCDselectData});
								 },
								 error : function(data){
									 alert("Function Code Data not available !"); 
								 }
						  	  });
					
				};
				
				if(message.availableFnMenuItem){
					$scope.addFnMenuItem = message.availableFnMenuItem;
				} else {
					$scope.addFnMenuItem = {};
				}
				$scope.addFnMenuItem.menuSetCode = "APP";
				
			    $scope.statusOptions = [        
				                        {index: 0, value: 'true', title: 'Y'},
				                        {index: 1, value: 'false', title: 'N'}
				                    ];
				$scope.selectedValue = $scope.statusOptions[0];
				
				$scope.separator = {
							availableOptions:[
											    {value: 'true',  name: 'Y'},
											    {value: 'false', name: 'N'}
											 ],
											 
											 selectedOption: {value: 'true', name: 'Y'}
						              
									};
					
					
			    $scope.activeStatusOptions = [        
				                        {index: 0, value: 'true', title: 'Y'},
				                        {index: 1, value: 'false', title: 'N'}
				                    ];
			    $scope.activeSelectedValue = $scope.activeStatusOptions[0];
				    
			    $scope.separatorStatusOptions = [        
							                        {index: 0, value: 'true', title: 'Y'},
							                        {index: 1, value: 'false', title: 'N'}
							                    ];
			    $scope.separatorSelectedValue = $scope.separatorStatusOptions[1];				    
				
				$scope.active = {
							availableOptions:[
											    {value: 'true',  name: 'Y'},
											    {value: 'false', name: 'N'}
											 ],
											 
											 selectedOption: {value: 'true', name: 'Y'}
						              
									};
				
	  			
				$scope.updateFnMenu = function(availableFnMenuItem) { 
						// form validation
						if( document.getElementById("popupAddMenuItemImageSrc").value == "" ) availableFnMenuItem.imageSrc = "N/A";
						if( document.getElementById("popupAddMenuItemTarget").value == "" ) availableFnMenuItem.target = "N/A";
						if( document.getElementById("popupAddMenuItemExternalUrl").value == "" ) availableFnMenuItem.externalUrl = "N/A";
						if( document.getElementById("popupAddMenuItemQueryString").value == "" ) availableFnMenuItem.queryString = "N/A";
						if( document.getElementById("popupAddMenuItemServlet").value == "" ) availableFnMenuItem.servlet = "N/A";
						
						validationRule = /^\S{3,}$/;
						var selectedFunction = document.getElementById("repeatSelect");
						var selectedFunctionText = selectedFunction.options[selectedFunction.selectedIndex].text;
						if(
								(document.getElementById("popupAddMenuItemLabel").value == "" || document.getElementById("popupAddMenuItemLabel").value.replace(" ", "").length == 0 ) ||//!validationRule.test(document.getElementById("popupAddMenuItemLabel").value)) ||
								document.getElementById("popupAddMenuItemParentId").value == "" ||
								document.getElementById("popupAddMenuItemAction").value == "" ||
								selectedFunctionText == "" ||
								document.getElementById("popupAddMenuItemSortOrder").value == "" ||
								document.getElementById("popupAddMenuItemMenuSetCode").value == ""
						  )	
							{
								alert("please provide valid entries !");  
							}
						
						else{ 
								  var uuu = "admin_fn_menu/updateFnMenu.htm";
								  var postData={availableFnMenuItem: availableFnMenuItem};
								  
							  	  $.ajax({
							  		 type : 'POST',
							  		 url : uuu,
							  		 //dataType: 'json',				// data type expected from server
							  		 contentType: 'application/json',  
							  		 data: JSON.stringify(postData),    // data type sent to server	
							  		 success : function(data){
							  			$scope.$apply(function(){ 
							  			$scope.availableFnMenuItems=data.availableFnMenuItems;
							  			});
							  			
							  			$modalInstance.close({availableFnMenuItems:$scope.availableRoleFunctions});
									 },
									 error : function(data){
										 alert("Error while saving."); 
									 }
							  	  }); 

							}
						$scope.getLeftMenuItems();						// get left menu items again from database

					};

					$scope.getLeftMenuItems = function() {
																return $http.get('get_menu')
																.then(function(response) {
																		if (typeof response.data === 'object') {
																        	var leftChildData=[];
																        	var leftParentData=[];
																        	var leftMenuItems = [];
																  			var j = response; 
															    	  		try{
															    	  			if(j && j !== "null" && j!== "undefined"){
															    	  				leftParentData = JSON.parse(j.data);
															    	      		    leftChildData = JSON.parse(j.data2);
															    	  			}else{
															    	  				throw "Get Left Menu respsone is not an object/is empty"; 
															    	  			}  
															      		    	try{
															        	  			var leftChildItemList = leftChildData; 											
															                  		var pageUrl = window.location.href.split('/')[window.location.href.split('/').length-1];
															                 		var leftParentList = leftParentData;												
															                 		for (var i = 0; i < leftParentList.length; i++) {
															                 			$scope.item = {
															                 				parentLabel : leftParentList[i].label,
															                 				parentAction : leftParentList[i].action,
															                 				parentImageSrc : leftParentList[i].imageSrc,                 				
															                 				open:pageUrl==leftParentList[i].action?true:false,
															                 				childItemList : leftChildItemList[i]==null?'x':leftChildItemList[i]
															                 			}
															                 			leftMenuItems.push($scope.item); 											
															                 		};
															                 		
															                 		$scope.leftMenuItems = leftMenuItems;
										
															        	  		}catch(err){
															        	  			console.log("error happened while trying to set left menu structure: "+err);  					   
															        	  		}
															    	  		}catch (e) {
															    	  			console.log("error happened while trying to get left menu items: "+e);
															    	  			reloadPageOnce();
															    	  			return;
															    	        }	
																			return response.data;
																		} else {
																			return $q.reject(response.data);
																		}
																}, function(response) {
																	// something went wrong
																	return $q.reject(response.data);
																});
					};

				$scope.close = function() {
					$modalInstance.close();
				};
}