Merge from ecomp 718fd196 - Modern UI
[vid.git] / vid-webpack-master / src / app / shared / components / genericFormPopup / generic-form-popup.component.ts
1 import {Component, OnDestroy, OnInit} from '@angular/core';
2 import {FormPopupDetails} from "../../models/formControlModels/formPopupDetails.model";
3 import {DialogComponent, DialogService} from "ng2-bootstrap-modal";
4 import {FormGroup} from "@angular/forms";
5 import {IframeService} from "../../utils/iframe.service";
6 import {ITreeNode} from "angular-tree-component/dist/defs/api";
7 import * as _ from "lodash";
8 import {NgRedux} from "@angular-redux/store";
9 import {AppState} from "../../store/reducers";
10 import {ServicePopupService} from "./genericFormServices/service/service.popup.service";
11 import {ActivatedRoute} from "@angular/router";
12 import {AaiService} from "../../services/aaiService/aai.service";
13 import {GenericFormPopupService} from "./generic-form-popup.service";
14 import {FormControlModel} from "../../models/formControlModels/formControl.model";
15 import {FormGeneralErrorsService} from "../formGeneralErrors/formGeneralErrors.service";
16
17
18 export interface PopupModel {
19   type : PopupType;
20   uuidData : UUIDData;
21   node : ITreeNode;
22   isUpdateMode : boolean;
23 }
24
25 export enum PopupType{
26   SERVICE = 'service',
27   VNF = 'vnf',
28   NETWORK = 'network',
29   VF_MODULE = 'vf_module',
30   VNF_GROUP = 'vnf_group'
31 }
32
33
34 @Component({
35   selector : 'generic-form-popup',
36   templateUrl : 'generic-form-popup.component.html',
37   styleUrls : ['generic-form-popup.component.scss']
38 })
39
40 export class GenericFormPopupComponent extends DialogComponent<PopupModel, boolean> implements OnInit, OnDestroy{
41   formPopupDetails : FormPopupDetails = null;
42   dynamicForm : FormGroup;
43   type : PopupType;
44   uuidData : UUIDData;
45   isUpdateMode : boolean;
46   node : ITreeNode = null;
47   hasGeneralApiError : boolean = false;
48   parentElementClassName = 'content';
49   errorMsg = 'Page contains errors. Please see details next to the relevant fields.';
50
51   servicesQty = 1;
52   quantityOptions = _.range(1, 51)
53   constructor(dialogService:  DialogService ,
54               private _iframeService : IframeService,
55               private _store: NgRedux<AppState>,
56               private _servicePopupService : ServicePopupService,
57               private _activatedRoute : ActivatedRoute,
58               private _aaiService : AaiService,
59               private _route: ActivatedRoute,
60               private _genericFormPopupService : GenericFormPopupService){
61     super(dialogService);
62   }
63
64   closeDialog(that) : void{
65     this._iframeService.removeClassCloseModal(that.parentElementClassName);
66     this.dialogService.removeDialog(this);
67     setTimeout(() => {
68       window.parent.postMessage("closeIframe", "*");
69     }, 15);
70   }
71
72   shouldShowNotification() : boolean {
73     return this.formPopupDetails && this.formPopupDetails.UUIDData['bulkSize'] > 1
74   }
75
76   ngOnInit(): void {
77     this._route
78       .queryParams
79       .subscribe(params => {
80         console.log('changed');
81         if(params['serviceModelId'] && params['isCreate']=="true"){
82           this._genericFormPopupService.initReduxOnCreateNewService().then((serviceModelId : string)=>{
83             this.uuidData = <any>{
84               bulkSize : 1,
85               isMacro : this._store.getState().service.serviceHierarchy[serviceModelId].service.vidNotions.instantiationType === 'Macro',
86               type : PopupType.SERVICE,
87               serviceId: serviceModelId,
88               popupService: this._servicePopupService,
89             };
90
91             this.uuidData.popupService.closeDialogEvent.subscribe((that)=>{
92               this.closeDialog(that);
93             });
94
95             this.formPopupDetails = this.uuidData.popupService.getGenericFormPopupDetails(
96               this.uuidData['serviceId'],
97               null,
98               null,
99               this.node,
100               this.uuidData,
101               false
102             );
103           });
104         }
105       });
106
107     FormGeneralErrorsService.checkForErrorTrigger.subscribe(()=>{
108       this.hasSomeError(this.formPopupDetails, this.dynamicForm);
109     });
110     
111     if(!_.isNil(this.uuidData)){
112       this.uuidData.popupService.closeDialogEvent.subscribe((that)=>{
113         this.closeDialog(that);
114       });
115
116       this.uuidData['isMacro'] = this._store.getState().service.serviceHierarchy[this.uuidData['serviceId']].service.vidNotions.instantiationType === 'Macro';
117       this.formPopupDetails = this._genericFormPopupService.getGenericFormDetails(this.uuidData, this.node, this.isUpdateMode);
118     }
119   }
120
121   hasSomeError(formPopupDetails : FormPopupDetails, form : FormGroup) : boolean{
122     if(_.isNil(formPopupDetails)) return false;
123     else {
124       for(let controlName in form.controls){
125         if(form.controls[controlName].errors){
126           let error: string[] = Object.keys(form.controls[controlName].errors);
127           if(error.length === 1 && error[0] === 'required'){
128             continue;
129           }else if(Object.keys(form.controls[controlName].errors).length > 0  ){
130             return true;
131           }
132         }
133       }
134     }
135
136     return formPopupDetails.formControlList.filter((item : FormControlModel) => item.type === 'DROPDOWN' && item['hasEmptyOptions'] && item.isRequired()).length > 0
137   }
138 }
139
140
141 export class UUIDData extends Object{
142   type : string;
143   popupService : any;
144 }
145