summaryrefslogtreecommitdiffstats
path: root/portal-FE-common/src/app/pages/functional-menu/functional-menu-dialog/functional-menu-dialog.component.ts
blob: a1839732b26750efe5445c5bb1384298f8286296 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software 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.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * 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.
 *
 * ============LICENSE_END============================================
 *
 * 
 */
 
import { Component, OnInit, Input, Output, EventEmitter  } from '@angular/core';
import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { FunctionalMenuService } from 'src/app/shared/services';
import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';

@Component({
  selector: 'app-functional-menu-dialog',
  templateUrl: './functional-menu-dialog.component.html',
  styleUrls: ['./functional-menu-dialog.component.scss']
})
export class FunctionalMenuDialogComponent implements OnInit {

  @Input() nodedata: any;
  @Input() operationName: string;
  @Output() passEntry: EventEmitter<any> = new EventEmitter();
  isEditMode: boolean = false;
  isViewMode: boolean = false;
  isAddItemMode: boolean = false;
  selectedItem: any;
  result: any;
  availableRoles: any;
  preSelectedRoles: any;
  isAllApplications: boolean  = false;
  availableApps: any
  selectedRole: any = [];
  selectedApp={
    index:null,
    isDisabled: null
  };
  selectedAppIndex: any;
  menutitle:string;
  menuLocation:string;
  nodedetails: any;
  isParentDisable:boolean =true;
  hideRoleField:boolean = true;
  conflictMessages = {};
  functionalMenuForm = {};
  
  constructor(public functionalMenuService : FunctionalMenuService, public ngbModal: NgbModal, public activeModal: NgbActiveModal) { }

  ngOnInit() {
    //console.log("nodedata in dialog ",this.nodedata);
    this.nodedetails = Object.assign({}, this.nodedata);
    this.isViewMode = true;
    this.selectedItem = this.nodedata;
    this.selectedRole = [];
    this.availableRoles = [];
    if(this.nodedata && (this.isViewMode || this.isEditMode) && this.isLeafMenuItem(this.nodedetails)){
        this.selectedApp.index = this.nodedetails.appid;
        this.selectedAppIndex=this.nodedetails.appid;
        this.getAvailableRoles(this.selectedAppIndex);
    }
    
    if(this.isViewMode || this.isEditMode){
         this.nodedetails.menutitle = this.nodedetails.name;
         this.nodedetails.menuLocation = this.isParentMenuItem(this.nodedata) ? this.nodedata.name : this.nodedata.parent.name;
    }else{
        this.nodedetails.menutitle = '';
        this.nodedetails.menuLocation = this.nodedata.name;
    }
    this.nodedetails.selectedAppIndex = (this.selectedItem.appid) ? this.selectedItem.appid : 0;
    this.getAvailableApps();
    if(this.selectedItem.appid && this.selectedItem.appid >0){
      this.getAvailableRoles(this.selectedItem.appid);
    }
  }

  switchToEditMode(){
    //console.log("switchToEditMode :: ",this.nodedata);
    this.isViewMode = false;
    this.isEditMode = true;
    this.isParentDisable =true;
    this.nodedetails.name = this.selectedItem.name;
    this.nodedetails.url = this.selectedItem.url;
    this.nodedetails.selectedAppIndex = (this.selectedItem.appid) ? this.selectedItem.appid : 0;
    this.nodedetails.selectedRole = (this.selectedItem.roles) ? this.selectedItem.roles : 0;
  }

  switchToAddMode(){
    //console.log("switchToAddMode :: ",this.nodedata);
    if(this.selectedItem != null && this.selectedItem.getLevel() >= 4){
      this.openConfirmationModal("","You are not allowed to have a menu item at a level greater than 4.");
      return ;
    }
    //this.isViewMode = false;
    this.isViewMode = false;
    this.isEditMode = true;
    this.isAddItemMode = true;
    this.nodedetails.name = "";
    this.nodedetails.url = "";
    this.nodedetails.selectedAppIndex = 0;
    this.nodedetails.selectedRole = 0;
  }

  /**
   * deleteMenuItem
   * @param selectedItem 
   */
  deleteMenuItem(){
    if(this.selectedItem.children!=null && this.selectedItem.children.length>0){
      const modalRef = this.ngbModal.open(ConfirmationModalComponent);
      modalRef.componentInstance.title = "";
      modalRef.componentInstance.message = 'You are not allowed to delete a menu item that has children. You can only delete leaf menu items.';
      modalRef.result.then((result) => { }, (resut) => {return;});
    }else{
      const modalRef = this.ngbModal.open(InformationModalComponent);
      modalRef.componentInstance.title = "Confirmation";
      modalRef.componentInstance.message = 'Are you sure you want to delete '+ this.selectedItem.name+' ?';
      modalRef.result.then((result) => {
        if (result === 'Ok') {
          this.functionalMenuService.deleteMenuItem(this.selectedItem.menuId)
          .subscribe(_data => {
              this.result = _data
              this.passEntry.emit(this.result);
              let successMsg = "Item Deleted Successfully";
              this.openConfirmationModal("Success",successMsg);
              this.ngbModal.dismissAll();
          }, error =>{
            console.log(error);
            let deleteErrorMsg  = 'There was an error while deleting the item.'+error.message;
            this.openConfirmationModal("Error",deleteErrorMsg);
            return;
          });
        }
      }, (resut) => {
        
      })
    }
  }

  updateSelectedApp(appid){
    //console.log("updateSelectedApp called with appId :: ",appid);
    if (!appid) {
      return;
    }
    this.getAvailableRoles(appid);
  }

  getAvailableRoles(appid){
    //console.log("getAvailableRoles called with appId :: ",appid);
    if (appid != null && appid >0) {
      this.functionalMenuService.getManagedRolesMenu(appid)
      .subscribe(rolesObj => {
        this.availableRoles = rolesObj;
        if(this.availableRoles && this.availableRoles.length >0){
          this.hideRoleField = false;
        }
        this.preSelectedRoles = {roles:[]};
        this.preSelectedRoles = {roles:[]};

        if((this.isEditMode) && this.isMidLevelMenuItem(this.nodedata)){
            // in Edit flow , for Midlevel menu item no need to preSelect.
            this.preSelectedRoles = {roles:[]};
        }else if(this.nodedata && this.isEditMode && this.isLeafMenuItem(this.nodedata) && this.nodedata.appid!=appid) {
            // in Edit flow , for LeafMenuItem, if appid changed then no need to preSelect.
            this.preSelectedRoles = {roles:[]};
        }else{
            if(this.nodedata && this.nodedata.roles){
                for(var i=0; i< this.nodedata.roles.length; i++){
                    var role = {"roleId": this.nodedata.roles[i]};
                    this.preSelectedRoles.roles.push(role);
                }
            }
        }
        
        if(this.nodedata.rolesObj){
          for(var i=0; i< this.nodedata.rolesObj.length;i++){
            //this.availableRoles[i].isApplied = false;
            for(var j=0;j<this.preSelectedRoles.roles.length;j++){
              if(this.preSelectedRoles.roles[j].roleId==this.availableRoles[i].roleId){
                this.availableRoles[i].isApplied=true;
                this.nodedetails.selectedRole = (this.preSelectedRoles.roles[j]) ? this.preSelectedRoles.roles[j] : 0;
                break;
              }
          }
          }
        }
      }, error =>{
        console.log(error);
        let errorMsg = 'There was an error while gettting available roles. ' + error.message;
        this.openConfirmationModal("",errorMsg);
        return;
      });
      //console.log("this.availableRoles >>>>>",this.availableRoles);
    }else{
      console.log("FunctionalMenuDialogComponent::getAvailableRoles: appid was null or -1");
    }
  }

  getAvailableApps(){
    this.isAllApplications = true;
    this.functionalMenuService.getAvailableApplications()
      .subscribe(apps => {
        this.availableApps = apps;
        if (this.nodedetails && this.nodedetails.index) {
          for (var i = 0; i < this.availableApps.length; i++) {
              if (apps[i].index === this.nodedetails.index) {
                  //console.log("MenuDetailsModalCtrl::getAvailableApps: found app with index: " + this.nodedetails.index);
                  //console.log("MenuDetailsModalCtrl::getAvailableApps: setting isDisabled to: " + !apps[i].enabled);
                  this.nodedetails.isDisabled = !apps[i].enabled;
                  break;
              }
          }
          //console.log("didn't find index: " + this.nodedetails.index);
        }
      }, error =>{
        console.log(error);
        this.isAllApplications = false;
        let errorMsg = 'There was a problem retrieving the Applications. '+error.message;
        this.openConfirmationModal("Error",errorMsg);
      });
  }

  isLeafMenuItem(menu: any){
      return menu.children.length>0 ? false : true;
  }

  isMidLevelMenuItem(menu: any){
      return menu.parentMenuId!=null && menu.children.length>0 ? true : false;
  }

  isParentMenuItem(menu: any){
      return menu.parentMenuId!=null ? false : true;
  };

 isRoleSelected(){
   var selectedRoleIds=[];
   for(var i=0;i<this.availableRoles.length;i++){
       if(this.availableRoles[i].isApplied){
          selectedRoleIds.push(this.availableRoles[i].roleId);
          return true;
       }
     }
   return false;
  }  

  getDialogTitle = (source) => {
    switch (source) {
      case 'edit':
          return "Functional Menu - Edit";
      case 'view':
          return "Functional Menu - View";
      case 'add':
          return "Functional Menu - Add";
      default:
          return "Functional Menu";
    };
  }

  saveChanges(){
    if(!this.nodedetails.menuLocation || !this.nodedetails.name 
        || !this.nodedetails.url || !this.nodedetails.selectedAppIndex 
        || !this.nodedetails.selectedAppIndex || !this.nodedetails.selectedRole){
      this.openConfirmationModal("","All fields are mandatory, please provide inputs for all the fields.");
      return;
    }
    /*
    if(!!this.nodedetails.url && (!this.selectedApp || this.selectedAppIndex <=0)) {
      this.openConfirmationModal("","Please select the appropriate app, or remove the url");
      return;
    }else if(!this.nodedetails.url && (this.selectedApp) && this.selectedApp.index>0){
        this.openConfirmationModal("","Please enter url, or select No Application");
        return;
    }else if(!this.nodedetails.menutitle){
        this.openConfirmationModal("","Please enter the Menu title");
        return;
    }
    */
    
    if(this.isAddItemMode){
      if(this.selectedItem != null && this.selectedItem.getLevel() >= 4){
        this.openConfirmationModal("","You are not allowed to have a menu item at a level greater than 4.");
        return ;
      }else{
        let data = null;
        let selectedMenuDetails = null;
        this.functionalMenuService.getFunctionalMenu(this.selectedItem.menuId)
          .subscribe(_data => {
              selectedMenuDetails = _data
              if((this.selectedItem.children===null || this.selectedItem.children.length == 0) && (!!selectedMenuDetails.url
                 || !!selectedMenuDetails.appid || !!selectedMenuDetails.roles)){
                let warning_message = 'Warning: the child menu item "' + 
                              this.selectedItem.name + '" is already configured with an application. You can create a new mid-level menu item.';
                this.openConfirmationModal("",warning_message);
                return;
              }else{
                if(this.selectedItem){
                    var selectedRoleIds=[];
                    //console.log("Selected Role ID ; ",this.nodedetails.selectedRole);
                    if(this.nodedetails.selectedRole){
                      selectedRoleIds.push(this.nodedetails.selectedRole);
                    }
                    
                    let applicationid: any = null;
                    if(!this.nodedata){
                      applicationid = null;
                    }else{
                      applicationid = this.nodedata.appid;
                    }
                    var newMenuItem = {
                        menuId:null, // this is a new menu item
                        column:this.nodedata.column,
                        text:this.nodedetails.name,
                        // We are creating this new menu item under the menu item that was clicked on.
                        parentMenuId:this.nodedata.menuId,
                        url:(this.nodedetails.url) ? this.nodedetails.url : null,
                        appid:(this.nodedetails.selectedAppIndex) ? this.nodedetails.selectedAppIndex: null,
                        roles:selectedRoleIds
                    };
                    //console.log("Add menu Item newMenuItem :: ",newMenuItem)
                    this.functionalMenuService.saveMenuItem(newMenuItem)
                    .subscribe(_data => {
                        this.result = _data
                        //console.log("add menu item response :: ",_data);
                        this.passEntry.emit(this.result);
                        let successMsg = "Item added successfully";
                        this.openConfirmationModal("Success",successMsg);
                    }, error =>{
                      console.log(error);
                      if(error.status === 409){//Conflict
                        this.handleConflictErrors(error);
                      } else {
                        let errorMsg = "There was a problem saving your menu. Please try again later. Error Status: "+error.status
                        this.openConfirmationModal("",errorMsg);
                        return;
                      }
                   });
                }
              }
          }, error =>{
            //console.log(error);
            let errorMsg = "There was a problem saving your menu. Please try again later. Error Status: "+error.status
            this.openConfirmationModal("",errorMsg );
            return;
        });
      }
    }else{
      //edit mode..
      //console.log('MenuDetailsModalCtrl::saveChanges: Will be saving an edit menu item');
      var selectedRoleIds=[];
      //console.log("Selected Role ID ---->>>> ",this.nodedetails.selectedRole);
      if(this.nodedetails.selectedRole){
        selectedRoleIds.push(this.nodedetails.selectedRole);
      }
      let activeMenuItem = {
          menuId:this.nodedata.menuId,
          column:this.nodedata.column,
          text:this.nodedetails.name,
          parentMenuId:this.nodedata.parentMenuId,
          url:(this.nodedetails.url) ? this.nodedetails.url : null,
          appid: (this.nodedetails.selectedAppIndex) ? this.nodedetails.selectedAppIndex: null,          
          roles:selectedRoleIds
      };
      if ((activeMenuItem.appid==null && activeMenuItem.url=="") || (activeMenuItem.appid==null && activeMenuItem.url=="undefined")) {
        activeMenuItem.roles = null;
      }
      //console.log("Update menu Item activeMenuItem :: ",activeMenuItem);
      this.functionalMenuService.saveEditedMenuItem(activeMenuItem)
      .subscribe(_data => {
          this.result = _data
          //console.log("Edit menu item response :: ",_data);
          this.passEntry.emit(this.result);
          let successMsg = "Item updated successfully";
          this.openConfirmationModal("Success",successMsg);
          //this.ngbModal.dismissAll();
      }, error =>{
        //console.log(error);
        let errorMsg = "There was a problem updating your menu. Please try again later. Error Status: "+error.status;
        this.openConfirmationModal("",errorMsg);
        return;
      });
    } 
  }

  //This part handles conflict errors (409)
  handleConflictErrors(error){
    if(!error.data){
      return;
    }
    if(!error.data.length){ //support objects
      error.data = [error.data]
    }
    //console.log('MenuDetailsModalCtrl::handleConflictErrors: err.data = ' + JSON.stringify(error.data));
    if(error.data){
      error.data.forEach(item => {
         //set conflict message
         this.conflictMessages[item.field.name] = item.errorCode;
         //set field as invalid
         //console.log('MenuDetailsModalCtrl::handleConflictErrors: fieldName = ' + item.field.name);
         this.functionalMenuForm[item.field.name].$setValidity('conflict', false);
      });
    }
  }

  openConfirmationModal(_title: string, _message: string) {
    const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
    modalInfoRef.componentInstance.title = _title;
    modalInfoRef.componentInstance.message = _message;
  }

  openInformationModal(_title: string, _message: string){
    const modalInfoRef = this.ngbModal.open(InformationModalComponent);
    modalInfoRef.componentInstance.title = _title;
    modalInfoRef.componentInstance.message = _message;
    return modalInfoRef;
  }
}