6e5b0c1ac3e98d53c5d9e8c4b413770de07f7b7c
[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     this.getFunctionCDselectData();
138     this.getParentData();
139   }
140
141   getParentData(){
142     this.showSpinner = true;
143     this.adminService.getParentData()
144     .subscribe( _data => {
145       this.result = _data;
146       if(_data){
147         this.parentListSelectData= _data;               // data from server
148         let menuItems = this.parentListSelectData;
149         var heirarchicalMenuItems = [];
150         var children = [];
151         for ( var i=0; i<menuItems.length; i++){
152           for(var j=0; j<menuItems.length; j++){
153             if(menuItems[j][2]==menuItems[i][0]) 
154               children.push(
155                 {
156                   menuId: menuItems[j][0],
157                   label:  menuItems[j][1]
158                 }
159               );
160           }
161           if(children.length!=0){
162             heirarchicalMenuItems.push(
163                 {
164                   menuId: menuItems[i][0],
165                   label:  menuItems[i][1],
166                   children: children.sort(this.sortItems("label"))
167                 }
168             
169             );
170           }
171           children = [];
172         }
173         heirarchicalMenuItems.sort(this.sortItems("label"));
174         this.childListSelectData = heirarchicalMenuItems;
175       }
176     }, error => {
177       this.showSpinner = false;
178       console.log(error);
179     });
180   }
181
182   sortItems(prop){
183     return function(a, b) {  
184       if (a[prop] > b[prop]) {  
185           return 1;  
186       } else if (a[prop] < b[prop]) {  
187           return -1;  
188       }  
189       return 0;  
190     }        
191   }
192   
193   getParentLabel(parentId, parentListSelectData){
194     var element;
195     element = parentListSelectData[0];
196     for (var i=0; i<parentListSelectData.length; i++){
197       element = parentListSelectData[i];
198       if (element[0] == parentId)                    
199           return element[1];
200       else "---";
201     }
202   }
203
204   updateFnMenu(){
205     this.showSpinner = true;
206     if( this.menu && this.menu.imageSrc == "" ) this.menu.imageSrc = "N/A";
207     if( this.menu && this.menu.target == "" ) this.menu.target = "N/A";
208     if( this.menu && this.menu.externalUrl == "" ) this.menu.externalUrl = "N/A";
209     if( this.menu && this.menu.queryString == "" ) this.menu.queryString = "N/A";
210     if( this.menu && this.menu.servlet == "" ) this.menu.servlet = "N/A";
211
212     let validationRule = /^\S{3,}$/;
213     let selectedFunction = this.menu.functionCd;
214     var selectedFunctionText = this.menu.functionCd; //selectedFunction.options[selectedFunction.selectedIndex].text;
215
216     if((this.menu.label == null || this.menu.label == "" || (this.menu.label && this.menu.label.trim().length == 0) ) ||
217       this.menu.parentId == null || this.menu.parentId == "" || this.menu.action == null || this.menu.action == "" ||
218       selectedFunctionText == null || selectedFunctionText == "" || this.menu.sortOrder == null || this.menu.sortOrder == "" ||
219       this.menu.menuSetCode == null ||this.menu.menuSetCode == ""){
220
221       this.openConfirmationModal('','Please provide all the mandatory (*) fields inputs !');
222       return;
223     }else{
224       this.menu.parentId=parseFloat(this.menu.parentId);
225       let data ={availableFnMenuItem: this.menu};
226       let postData = JSON.stringify(data);
227       this.adminService.updateFnMenuItem(postData)
228       .subscribe( _data => {
229         this.result = _data;
230         this.passEntry.emit(this.result);
231         this.ngmodel.dismissAll(); 
232       }, error => {
233         this.showSpinner = false;
234         console.log(error);
235         this.openConfirmationModal("Error",error);
236       });
237       this.getLeftMenuItems();
238     }
239
240   }
241
242   getLeftMenuItems(){
243     let  sidebarComponent = new SidebarComponent(this.router, this.sidebarService,this.cookieService);
244     sidebarComponent.ngOnInit();
245   }
246
247   getFunctionCDselectData = function(){
248     this.adminService.getFunctionCdList()
249     .subscribe( _data => {
250       this.result = _data;
251       if(_data){
252         this.functionCDselectData = _data;
253       }
254     }, error => {
255       this.showSpinner = false;
256       console.log(error);
257       this.openConfirmationModal("","Function Code Data not available !");
258     });
259   }
260
261   openConfirmationModal(_title: string, _message: string) {
262     const modalInfoRef = this.ngmodel.open(ConfirmationModalComponent);
263     modalInfoRef.componentInstance.title = _title;
264     modalInfoRef.componentInstance.message = _message;
265   }
266
267 }