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