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