4acf2252a5a0e32b37d0240eee9686b4de3c1ddf
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38
39 import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
40 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
41 import { AdminService } from '../../admin.service';
42 import { SidebarComponent } from 'src/app/layout/components/sidebar/sidebar.component';
43 import { Router } from '@angular/router';
44 import { SidebarService } from 'src/app/shared/services';
45 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
46 import { CookieService } from 'ngx-cookie-service';
47
48 @Component({
49   selector: 'app-new-menu',
50   templateUrl: './new-menu.component.html',
51   styleUrls: ['./new-menu.component.scss']
52 })
53 export class NewMenuComponent implements OnInit {
54
55   menu = {
56     action: null,
57     active: false,
58     activeAsString: "false",
59     auditTrail: null,
60     auditUserId: null,
61     childMenus: [],
62     created: null,
63     createdId: null,
64     externalUrl: null,
65     functionCd: null,
66     id: null,
67     imageSrc: null,
68     label: null,
69     menuLevel: null,
70     menuSetCode: null,
71     modified: null,
72     modifiedId: null,
73     parentId: null,
74     parentIdAsString: null,
75     parentMenu: null,
76     queryString: null,
77     rowNum: null,
78     separator: false,
79     separatorAsString: false,
80     servlet: null,
81     sortOrder: null,
82     target: null
83   }
84   showSpinner: boolean;
85   result: any;
86   //menu: any;
87   @Input() selectedMenu: any;
88   @Input() isEditMode: boolean;
89   @Output() passEntry: EventEmitter<any> = new EventEmitter();
90
91   statusOptions = [        
92     {index: 0, value: 'true', title: 'Y'},
93     {index: 1, value: 'false', title: 'N'}
94   ];
95   selectedValue = this.statusOptions[0];
96   separator = {
97     availableOptions:[
98       {value: 'true',  name: 'Y'},
99       {value: 'false', name: 'N'}
100     ],       
101     selectedOption: {value: 'true', name: 'Y'}              
102   }
103
104   activeStatusOptions = [        
105     {index: 0, value: 'true', title: 'Y'},
106     {index: 1, value: 'false', title: 'N'}
107   ];
108   activeSelectedValue = this.activeStatusOptions[0];
109
110   separatorStatusOptions = [        
111     {index: 0, value: 'true', title: 'Y'},
112     {index: 1, value: 'false', title: 'N'}
113   ];
114   separatorSelectedValue = this.separatorStatusOptions[1];
115
116   active = {
117     availableOptions:[
118       {value: 'true',  name: 'Y'},
119       {value: 'false', name: 'N'}
120     ],
121     selectedOption: {value: 'true', name: 'Y'}           
122   };
123
124   functionCDselectData = [];
125   parentListSelectData = [];
126   childListSelectData = [];
127
128   constructor(public adminService: AdminService, public activeModal: NgbActiveModal, 
129     public ngmodel: NgbModal, public router: Router, public sidebarService: SidebarService,public cookieService:CookieService) { }
130
131   ngOnInit() {
132     if(this.selectedMenu){
133      this.menu = this.selectedMenu;
134     }else{
135       this.menu.menuSetCode='APP';
136     }
137     //console.log("selected menu >>>>",this.menu);
138     this.getFunctionCDselectData();
139     this.getParentData();
140   }
141
142   getParentData(){
143     this.showSpinner = true;
144     this.adminService.getParentData()
145     .subscribe( _data => {
146       //console.log("getParentData :: _data ",_data);
147       this.result = _data;
148       if(_data){
149         this.parentListSelectData= _data;               // data from server
150         let menuItems = this.parentListSelectData;
151         var heirarchicalMenuItems = [];
152         var children = [];
153         for ( var i=0; i<menuItems.length; i++){
154           for(var j=0; j<menuItems.length; j++){
155             if(menuItems[j][2]==menuItems[i][0]) 
156               children.push(
157                 {
158                   menuId: menuItems[j][0],
159                   label:  menuItems[j][1]
160                 }
161               );
162           }
163           if(children.length!=0){
164             heirarchicalMenuItems.push(
165                 {
166                   menuId: menuItems[i][0],
167                   label:  menuItems[i][1],
168                   children: children.sort(this.sortItems("label"))
169                 }
170             
171             );
172           }
173           children = [];
174         }
175         heirarchicalMenuItems.sort(this.sortItems("label"));
176         this.childListSelectData = heirarchicalMenuItems;
177         //console.log("childListSelectData ::: ",this.childListSelectData);
178         //console.log("heirarchicalMenuItems ::: ",heirarchicalMenuItems);
179         //console.log("parentListSelectData ::: ",this.parentListSelectData);
180       }
181     }, error => {
182       this.showSpinner = false;
183       console.log(error);
184     });
185   }
186
187   sortItems(prop){
188     return function(a, b) {  
189       if (a[prop] > b[prop]) {  
190           return 1;  
191       } else if (a[prop] < b[prop]) {  
192           return -1;  
193       }  
194       return 0;  
195     }        
196   }
197   
198   getParentLabel(parentId, parentListSelectData){
199     var element;
200     element = parentListSelectData[0];
201     for (var i=0; i<parentListSelectData.length; i++){
202       element = parentListSelectData[i];
203       if (element[0] == parentId)                    
204           return element[1];
205       else "---";
206     }
207   }
208
209   updateFnMenu(){
210     this.showSpinner = true;
211     //console.log("updateFnMenu Changes Called..",this.menu);
212     if( this.menu && this.menu.imageSrc == "" ) this.menu.imageSrc = "N/A";
213     if( this.menu && this.menu.target == "" ) this.menu.target = "N/A";
214     if( this.menu && this.menu.externalUrl == "" ) this.menu.externalUrl = "N/A";
215     if( this.menu && this.menu.queryString == "" ) this.menu.queryString = "N/A";
216     if( this.menu && this.menu.servlet == "" ) this.menu.servlet = "N/A";
217
218     let validationRule = /^\S{3,}$/;
219     let selectedFunction = this.menu.functionCd;
220     var selectedFunctionText = this.menu.functionCd; //selectedFunction.options[selectedFunction.selectedIndex].text;
221
222     if((this.menu.label == null || this.menu.label == "" || (this.menu.label && this.menu.label.trim().length == 0) ) ||
223       this.menu.parentId == null || this.menu.parentId == "" || this.menu.action == null || this.menu.action == "" ||
224       selectedFunctionText == null || selectedFunctionText == "" || this.menu.sortOrder == null || this.menu.sortOrder == "" ||
225       this.menu.menuSetCode == null ||this.menu.menuSetCode == ""){
226
227       this.openConfirmationModal('','Please provide all the mandatory (*) fields inputs !');
228       return;
229     }else{
230       this.menu.parentId=parseFloat(this.menu.parentId);
231       let data ={availableFnMenuItem: this.menu};
232       let postData = JSON.stringify(data);
233       //console.log("postData >>>>>>",postData);
234       this.adminService.updateFnMenuItem(postData)
235       .subscribe( _data => {
236         this.result = _data;
237         this.passEntry.emit(this.result);
238         this.ngmodel.dismissAll(); 
239       }, error => {
240         this.showSpinner = false;
241         console.log(error);
242         this.openConfirmationModal("Error",error);
243       });
244       this.getLeftMenuItems();
245     }
246
247   }
248
249   getLeftMenuItems(){
250     //console.log("getLeftMenuItems called after update menu");
251     let  sidebarComponent = new SidebarComponent(this.router, this.sidebarService,this.cookieService);
252     sidebarComponent.ngOnInit();
253   }
254
255   getFunctionCDselectData = function(){
256     this.adminService.getFunctionCdList()
257     .subscribe( _data => {
258       this.result = _data;
259       if(_data){
260         this.functionCDselectData = _data;
261       }
262     }, error => {
263       this.showSpinner = false;
264       console.log(error);
265       this.openConfirmationModal("","Function Code Data not available !");
266     });
267   }
268
269   openConfirmationModal(_title: string, _message: string) {
270     const modalInfoRef = this.ngmodel.open(ConfirmationModalComponent);
271     modalInfoRef.componentInstance.title = _title;
272     modalInfoRef.componentInstance.message = _message;
273   }
274
275 }