Merge "Product family ID on VNF should be mandatory"
[vid.git] / vid-webpack-master / src / app / shared / components / genericFormPopup / generic-form-popup.component.ts
index d0e2d4b..7368dd6 100644 (file)
@@ -8,21 +8,24 @@ import * as _ from "lodash";
 import {NgRedux} from "@angular-redux/store";
 import {AppState} from "../../store/reducers";
 import {ServicePopupService} from "./genericFormServices/service/service.popup.service";
-import {ActivatedRoute} from "@angular/router";
+import {ActivatedRoute, Router} from "@angular/router";
 import {AaiService} from "../../services/aaiService/aai.service";
 import {GenericFormPopupService} from "./generic-form-popup.service";
 import {FormControlModel} from "../../models/formControlModels/formControl.model";
 import {FormGeneralErrorsService} from "../formGeneralErrors/formGeneralErrors.service";
+import {FeatureFlagsService, Features} from "../../services/featureFlag/feature-flags.service";
+import {InstantiationTemplatesModalComponent} from "./instantiationTemplatesModal/instantiation.templates.modal.component";
+import {updateCurrentModalModeAction} from "../../storeUtil/utils/global/global.actions";
 
 
 export interface PopupModel {
-  type : PopupType;
-  uuidData : UUIDData;
-  node : ITreeNode;
-  isUpdateMode : boolean;
+  type: PopupType;
+  uuidData: UUIDData;
+  node: ITreeNode;
+  isUpdateMode: boolean;
 }
 
-export enum PopupType{
+export enum PopupType {
   SERVICE = 'service',
   VNF = 'vnf',
   NETWORK = 'network',
@@ -33,84 +36,68 @@ export enum PopupType{
 
 
 @Component({
-  selector : 'generic-form-popup',
-  templateUrl : 'generic-form-popup.component.html',
-  styleUrls : ['generic-form-popup.component.scss']
+  selector: 'generic-form-popup',
+  templateUrl: 'generic-form-popup.component.html',
+  styleUrls: ['generic-form-popup.component.scss']
 })
 
-export class GenericFormPopupComponent extends DialogComponent<PopupModel, boolean> implements OnInit, OnDestroy{
-  formPopupDetails : FormPopupDetails = null;
-  dynamicForm : FormGroup;
-  type : PopupType;
-  uuidData : UUIDData;
-  isUpdateMode : boolean;
-  node : ITreeNode = null;
-  hasGeneralApiError : boolean = false;
+export class GenericFormPopupComponent extends DialogComponent<PopupModel, boolean> implements OnInit, OnDestroy {
+  formPopupDetails: FormPopupDetails = null;
+  dynamicForm: FormGroup;
+  type: PopupType;
+  uuidData: UUIDData;
+  showTemplateBtn: boolean = false;
+  isUpdateMode: boolean;
+  node: ITreeNode = null;
+  hasGeneralApiError: boolean = false;
   parentElementClassName = 'content';
   errorMsg = 'Page contains errors. Please see details next to the relevant fields.';
-
+  serviceModelId : string;
   servicesQty = 1;
   quantityOptions = _.range(1, 51)
-  constructor(dialogService:  DialogService ,
-              private _iframeService : IframeService,
+
+  constructor(dialogService: DialogService,
+              private _iframeService: IframeService,
               private _store: NgRedux<AppState>,
-              private _servicePopupService : ServicePopupService,
-              private _activatedRoute : ActivatedRoute,
-              private _aaiService : AaiService,
+              private _servicePopupService: ServicePopupService,
+              private _activatedRoute: ActivatedRoute,
+              private _aaiService: AaiService,
+              private _dialogService: DialogService,
               private _route: ActivatedRoute,
-              private _genericFormPopupService : GenericFormPopupService){
+              private _router : Router,
+              private _genericFormPopupService: GenericFormPopupService) {
     super(dialogService);
   }
 
-  closeDialog(that) : void{
-    this._iframeService.removeClassCloseModal(that.parentElementClassName);
-    this.dialogService.removeDialog(this);
-    setTimeout(() => {
-      window.parent.postMessage("closeIframe", "*");
-    }, 15);
+  closeDialog(that): void {
+    this._iframeService.closeIframe(this.dialogService, this);
   }
 
-  shouldShowNotification() : boolean {
+  shouldShowNotification(): boolean {
     return this.formPopupDetails && this.formPopupDetails.UUIDData['bulkSize'] > 1
   }
 
+  clearModalIsUpdateMode() : void {
+    this._store.dispatch(updateCurrentModalModeAction(null));
+  }
+
   ngOnInit(): void {
+    this._store.dispatch(updateCurrentModalModeAction(this.isUpdateMode));
     this._route
       .queryParams
       .subscribe(params => {
-        console.log('changed');
-        if(params['serviceModelId'] && params['isCreate']=="true"){
-          this._genericFormPopupService.initReduxOnCreateNewService().then((serviceModelId : string)=>{
-            this.uuidData = <any>{
-              bulkSize : 1,
-              isMacro : this._store.getState().service.serviceHierarchy[serviceModelId].service.vidNotions.instantiationType === 'Macro',
-              type : PopupType.SERVICE,
-              serviceId: serviceModelId,
-              popupService: this._servicePopupService,
-            };
-
-            this.uuidData.popupService.closeDialogEvent.subscribe((that)=>{
-              this.closeDialog(that);
-            });
-
-            this.formPopupDetails = this.uuidData.popupService.getGenericFormPopupDetails(
-              this.uuidData['serviceId'],
-              null,
-              null,
-              this.node,
-              this.uuidData,
-              false
-            );
-          });
+        this.serviceModelId = params['serviceModelId'];
+        if (this.serviceModelId && params['isCreate'] == "true") {
+          this.onInitForCreateNewServicePopup(params['isInstantiationTemplateExists']);
         }
       });
 
-    FormGeneralErrorsService.checkForErrorTrigger.subscribe(()=>{
+    FormGeneralErrorsService.checkForErrorTrigger.subscribe(() => {
       this.hasSomeError(this.formPopupDetails, this.dynamicForm);
     });
-    
-    if(!_.isNil(this.uuidData)){
-      this.uuidData.popupService.closeDialogEvent.subscribe((that)=>{
+
+    if (!_.isNil(this.uuidData)) {
+      this.uuidData.popupService.closeDialogEvent.subscribe((that) => {
         this.closeDialog(that);
       });
 
@@ -119,28 +106,61 @@ export class GenericFormPopupComponent extends DialogComponent<PopupModel, boole
     }
   }
 
-  hasSomeError(formPopupDetails : FormPopupDetails, form : FormGroup) : boolean{
-    if(_.isNil(formPopupDetails)) return false;
+  private onInitForCreateNewServicePopup(isInstantiationTemplateExists : boolean) {
+    this._genericFormPopupService.initReduxOnCreateNewService().then((serviceModelId: string) => {
+      this.uuidData = <any>{
+        bulkSize: 1,
+        isMacro: this._store.getState().service.serviceHierarchy[serviceModelId].service.vidNotions.instantiationType === 'Macro',
+        type: PopupType.SERVICE,
+        serviceId: serviceModelId,
+        popupService: this._servicePopupService,
+      };
+
+      this.showTemplateBtn = this._genericFormPopupService.shouldShowTemplateBtn(isInstantiationTemplateExists);
+
+      this.uuidData.popupService.closeDialogEvent.subscribe((that) => {
+        this.closeDialog(that);
+      });
+
+      this.formPopupDetails = this.uuidData.popupService.getGenericFormPopupDetails(
+        this.uuidData['serviceId'],
+        null,
+        null,
+        this.node,
+        this.uuidData,
+        false
+      );
+    });
+  }
+
+  hasSomeError(formPopupDetails: FormPopupDetails, form: FormGroup): boolean {
+    if (_.isNil(formPopupDetails)) return false;
     else {
-      for(let controlName in form.controls){
-        if(form.controls[controlName].errors){
+      for (let controlName in form.controls) {
+        if (form.controls[controlName].errors) {
           let error: string[] = Object.keys(form.controls[controlName].errors);
-          if(error.length === 1 && error[0] === 'required'){
+          if (error.length === 1 && error[0] === 'required') {
             continue;
-          }else if(Object.keys(form.controls[controlName].errors).length > 0  ){
+          } else if (Object.keys(form.controls[controlName].errors).length > 0) {
             return true;
           }
         }
       }
     }
 
-    return formPopupDetails.formControlList.filter((item : FormControlModel) => item.type === 'DROPDOWN' && item['hasEmptyOptions'] && item.isRequired()).length > 0
+    return formPopupDetails.formControlList.filter((item: FormControlModel) => item.type === 'DROPDOWN' && item['hasEmptyOptions'] && item.isRequired()).length > 0
   }
+
+
+  openTemplateModal = (): void => {
+    this._router.navigate(['/instantiationTemplatesPopup'], { queryParams: { serviceModelId: this.serviceModelId}, queryParamsHandling: 'merge' });
+  }
+
 }
 
 
-export class UUIDData extends Object{
-  type : string;
-  popupService : any;
+export class UUIDData extends Object {
+  type: string;
+  popupService: any;
 }