VID - Feature flag for PNF in 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, Router} 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 import {updateCurrentModalModeAction} from "../../storeUtil/utils/global/global.actions";
17 import {DrawingBoardTreeService} from "../../../drawingBoard/service-planning/drawing-board-tree/drawing-board-tree.service";
18
19
20 export interface PopupModel {
21   type: PopupType;
22   uuidData: UUIDData;
23   node: ITreeNode;
24   isUpdateMode: boolean;
25 }
26
27 export enum PopupType {
28   SERVICE = 'service',
29   VNF = 'vnf',
30   PNF = 'pnf',
31   NETWORK = 'network',
32   VF_MODULE = 'vf_module',
33   VF_MODULE_UPGRADE = 'vf_module_upgrade',
34   VNF_GROUP = 'vnf_group'
35 }
36
37
38 @Component({
39   selector: 'generic-form-popup',
40   templateUrl: 'generic-form-popup.component.html',
41   styleUrls: ['generic-form-popup.component.scss']
42 })
43
44 export class GenericFormPopupComponent extends DialogComponent<PopupModel, boolean> implements OnInit, OnDestroy {
45   formPopupDetails: FormPopupDetails = null;
46   dynamicForm: FormGroup;
47   type: PopupType;
48   uuidData: UUIDData;
49   showTemplateBtn: boolean = false;
50   isUpdateMode: boolean;
51   node: ITreeNode = null;
52   hasGeneralApiError: boolean = false;
53   parentElementClassName = 'content';
54   errorMsg = 'Page contains errors. Please see details next to the relevant fields.';
55   serviceModelId : string;
56   servicesQty = 1;
57   quantityOptions = _.range(1, 51)
58
59   constructor(dialogService: DialogService,
60               private _iframeService: IframeService,
61               private _store: NgRedux<AppState>,
62               private _servicePopupService: ServicePopupService,
63               private _activatedRoute: ActivatedRoute,
64               private _aaiService: AaiService,
65               private _dialogService: DialogService,
66               private _route: ActivatedRoute,
67               private _router : Router,
68               private _genericFormPopupService: GenericFormPopupService) {
69     super(dialogService);
70   }
71
72   closeDialog(that): void {
73     this._iframeService.closeIframe(this.dialogService, this);
74   }
75
76   shouldShowNotification(): boolean {
77     return this.formPopupDetails && this.formPopupDetails.UUIDData['bulkSize'] > 1
78   }
79
80   clearModalIsUpdateMode() : void {
81     this._store.dispatch(updateCurrentModalModeAction(null));
82   }
83
84   ngOnInit(): void {
85     this._store.dispatch(updateCurrentModalModeAction(this.isUpdateMode));
86     this._route
87       .queryParams
88       .subscribe(params => {
89         this.serviceModelId = params['serviceModelId'];
90         if (this.serviceModelId && params['isCreate'] == "true") {
91           this.onInitForCreateNewServicePopup(params['isInstantiationTemplateExists']);
92         }
93       });
94
95     FormGeneralErrorsService.checkForErrorTrigger.subscribe(() => {
96       this.hasSomeError(this.formPopupDetails, this.dynamicForm);
97     });
98
99     if (!_.isNil(this.uuidData)) {
100       this.uuidData.popupService.closeDialogEvent.subscribe((that) => {
101         DrawingBoardTreeService.triggerCheckIsDirty.next(that.uuidData.serviceId);
102         this.closeDialog(that);
103       });
104
105       this.uuidData['isMacro'] = this._store.getState().service.serviceHierarchy[this.uuidData['serviceId']].service.vidNotions.instantiationType === 'Macro';
106       this.formPopupDetails = this._genericFormPopupService.getGenericFormDetails(this.uuidData, this.node, this.isUpdateMode);
107     }
108   }
109
110   private onInitForCreateNewServicePopup(isInstantiationTemplateExists : boolean) {
111     this._genericFormPopupService.initReduxOnCreateNewService().then((serviceModelId: string) => {
112       this.uuidData = <any>{
113         bulkSize: 1,
114         isMacro: this._store.getState().service.serviceHierarchy[serviceModelId].service.vidNotions.instantiationType === 'Macro',
115         type: PopupType.SERVICE,
116         serviceId: serviceModelId,
117         popupService: this._servicePopupService,
118       };
119
120       this.showTemplateBtn = this._genericFormPopupService.shouldShowTemplateBtn(isInstantiationTemplateExists);
121
122       this.uuidData.popupService.closeDialogEvent.subscribe((that) => {
123         this.closeDialog(that);
124       });
125
126       this.formPopupDetails = this.uuidData.popupService.getGenericFormPopupDetails(
127         this.uuidData['serviceId'],
128         null,
129         null,
130         this.node,
131         this.uuidData,
132         false
133       );
134     });
135   }
136
137   hasSomeError(formPopupDetails: FormPopupDetails, form: FormGroup): boolean {
138     if (_.isNil(formPopupDetails)) return false;
139     else {
140       for (let controlName in form.controls) {
141         if (form.controls[controlName].errors) {
142           let error: string[] = Object.keys(form.controls[controlName].errors);
143           if (error.length === 1 && error[0] === 'required') {
144             continue;
145           } else if (Object.keys(form.controls[controlName].errors).length > 0) {
146             return true;
147           }
148         }
149       }
150     }
151
152     return formPopupDetails.formControlList.filter((item: FormControlModel) => item.type === 'DROPDOWN' && item['hasEmptyOptions'] && item.isRequired()).length > 0
153   }
154
155
156   openTemplateModal = (): void => {
157     this._router.navigate(['/instantiationTemplatesPopup'], { queryParams: { serviceModelId: this.serviceModelId}, queryParamsHandling: 'merge' });
158   }
159
160 }
161
162
163 export class UUIDData extends Object {
164   type: string;
165   popupService: any;
166 }
167