908ae4adf7f90b021608869576fb49e25379090d
[vid.git] / vid-webpack-master / src / app / components / service-popup / service-popup.component.ts
1 import {Component, ViewChild} from '@angular/core';
2 import {DialogComponent, DialogService} from 'ng2-bootstrap-modal';
3 import {ServiceModel} from '../../shared/models/serviceModel';
4 import {Constants} from '../../shared/utils/constants';
5 import {ServiceInstanceDetailsComponent} from './service-instance-details/service-instance-details.component';
6 import {ActivatedRoute} from "@angular/router";
7 import {AaiService} from "../../services/aaiService/aai.service";
8 import {Utils} from "../../utils/utils";
9 import {ServicePlanningService} from "../../services/service-planning.service";
10 import * as _ from 'lodash';
11 import {ModelInformationItem} from '../../shared/components/model-information/model-information.component';
12 import {deleteServiceInstance} from '../../service.actions';
13
14 import {InstancePopup} from "../instance-popup/instance-popup.components";
15 import {NgRedux} from "@angular-redux/store";
16 import {AppState} from "../../store/reducers";
17 import {ServicePopupService} from './service-popup.service';
18 import {IframeService} from "../../shared/utils/iframe.service";
19
20 export interface ServicePopupModel {
21   serviceModel: ServiceModel
22 }
23
24 @Component({
25   selector: 'service-popup',
26   templateUrl: 'service-popup.html',
27   styleUrls: ['service-popup.scss'],
28   providers: [AaiService, ServicePopupService]
29 })
30
31 export class ServicePopupComponent extends DialogComponent<ServicePopupModel, boolean>
32                                     implements ServicePopupModel, InstancePopup{
33   @ViewChild(ServiceInstanceDetailsComponent) serviceInstanceDetails: ServiceInstanceDetailsComponent;
34
35   serviceModel: ServiceModel;
36   serviceModelId: string;
37   serviceInstance: any = {
38     'rollbackOnFailure' : 'false'
39   };
40   title: string = Constants.ServicePopup.TITLE;
41   dynamicInputs: any[] = null;
42
43   maxServiceQty:number = 50;
44   minServiceQty:number = 1;
45   servicesQty = 1; //default
46   quantityOptions = this.getQuantityOptions();
47
48   modelInformationItems: Array<ModelInformationItem> = [];
49   hasGeneralApiError : boolean = false;
50   parentElementClassName = 'content';
51
52   constructor(dialogService: DialogService, private route: ActivatedRoute, private _aaiService: AaiService,
53               private _iframeService : IframeService,
54               private _servicePlanningService: ServicePlanningService, private store: NgRedux<AppState>, private _servicePopupService : ServicePopupService) {
55     super(dialogService);
56     this.title = Constants.ServicePopup.TITLE;
57   }
58
59   updateGeneralErrorSection() : void {
60     this.hasGeneralApiError = this._servicePopupService.onControlError(this.serviceInstanceDetails, this.serviceInstanceDetails.serviceInstanceDetailsFormGroup);
61   }
62
63
64   ngOnInit() {
65     this.route
66       .queryParams
67       .subscribe(params => {
68         this.serviceModelId = params['serviceModelId'];
69         if(params['isCreate']=="true") {
70           this.store.dispatch(deleteServiceInstance(this.serviceModelId));
71         }
72         this.updateServiceModelById(this.serviceModelId);
73         this.updateInstanceFromStore();
74       });
75   }
76
77   updateInstanceFromStore() {
78     let serviceInstance;
79     if (_.has(this.store.getState().service.serviceInstance, this.serviceModelId)) {
80       serviceInstance = Object.assign({}, this.store.getState().service.serviceInstance[this.serviceModelId]);
81     }
82
83     this.serviceInstance = serviceInstance ? serviceInstance : this.serviceInstance;
84     this.servicesQty = serviceInstance ? serviceInstance.bulkSize : 1;
85     if (serviceInstance && serviceInstance.instanceParams && serviceInstance.instanceParams[0]) {
86       this.dynamicInputs = this.dynamicInputs.map(function (x) {
87         x.value = (serviceInstance.instanceParams[0][x.id]) ? serviceInstance.instanceParams[0][x.id] : x.value;
88         return x;
89       });
90     }
91   }
92
93   updateServiceModelById(serviceModelId) {
94     this._aaiService.getServiceModelById(serviceModelId).subscribe(
95       value => {
96         const convertedModel = Utils.convertModel(value);
97         this.serviceModel = new ServiceModel(convertedModel);
98         let displayInputs = Object.assign({},convertedModel.service.inputs);
99         this.dynamicInputs = _.isEmpty(displayInputs)? [] : this._servicePlanningService.getArbitraryInputs(displayInputs);
100         this.modelInformationItems = this.createModelInformationItems();
101       },
102       error => {console.log('error is ', error)},
103       () => {console.log('completed')}
104     );
105   }
106
107   createModelInformationItems() : Array<ModelInformationItem> {
108      return [
109       new ModelInformationItem("Model version", "modelVersion", [this.serviceModel.version], "", true),
110       new ModelInformationItem("Description", "description", [this.serviceModel.description]),
111       new ModelInformationItem("Category", "category", [this.serviceModel.category]),
112       new ModelInformationItem("UUID", "uuid", [this.serviceModel.uuid], Constants.ServicePopup.TOOLTIP_UUID, true),
113       new ModelInformationItem("Invariant UUID", "invariantUuid", [this.serviceModel.invariantUuid], Constants.ServicePopup.TOOLTIP_INVARIANT_UUID, true),
114       new ModelInformationItem("Service type", "serviceType", [this.serviceModel.serviceType]),
115       new ModelInformationItem("Service role", "serviceRole", [this.serviceModel.serviceRole])
116     ];
117   }
118
119   onCancelClick() {
120     this._iframeService.removeClassCloseModal(this.parentElementClassName);
121     this.dialogService.removeDialog(this);
122     this.serviceInstance = this.serviceInstanceDetails.oldServiceInstance;
123
124     this._servicePopupService.resetDynamicInputs(this.serviceInstanceDetails, this.dynamicInputs);
125     // Delaying the iframe close in few milliseconds.
126     // This should workaround a problem in Selenium tests' that
127     // blocks after click because the iframe goes out before
128     // the driver understands it was clicked. Similar bug is
129     // described here:
130     //  - https://github.com/mozilla/geckodriver/issues/611
131     //  - https://bugzilla.mozilla.org/show_bug.cgi?id=1223277
132     setTimeout(() => {
133       window.parent.postMessage("closeIframe", "*");
134     }, 15);
135   }
136
137   getModelName(): string {
138     return (this.serviceModel && this.serviceModel.name) || "";
139   }
140
141   getQuantityOptions(){
142     return _.range(this.minServiceQty, this.maxServiceQty + 1);
143   }
144 }