1 import * as _ from "lodash";
2 import {Component, Input, ComponentRef, Inject} from '@angular/core';
3 import {Component as IComponent} from 'app/models/components/component';
5 import {ModalComponent} from 'app/ng2/components/ui/modal/modal.component';
6 import {ModalService} from 'app/ng2/services/modal.service';
7 import {ModalModel, ButtonModel, InputModel, OperationModel, CreateOperationResponse} from 'app/models';
9 import {ComponentServiceNg2} from 'app/ng2/services/component-services/component.service';
10 import {ComponentGenericResponse} from 'app/ng2/services/responses/component-generic-response';
11 import {WorkflowServiceNg2} from 'app/ng2/services/workflow.service';
13 import {OperationCreatorComponent} from './operation-creator/operation-creator.component';
16 selector: 'interface-operation',
17 templateUrl: './interface-operation.page.component.html',
18 styleUrls: ['interface-operation.page.component.less'],
19 providers: [ModalService]
22 export class InterfaceOperationComponent {
24 modalInstance: ComponentRef<ModalComponent>;
25 operationList: Array<OperationModel> = [];
26 openOperation: OperationModel;
28 @Input() component: IComponent;
29 @Input() readonly: boolean;
32 @Inject('$state') private $state:ng.ui.IStateService,
33 private ComponentServiceNg2: ComponentServiceNg2,
34 private WorkflowServiceNg2: WorkflowServiceNg2,
35 private ModalServiceNg2: ModalService,
39 this.ComponentServiceNg2.getInterfaceOperations(this.component).subscribe((response: ComponentGenericResponse) => {
40 let {interfaceOperations} = response;
41 this.component.interfaceOperations = interfaceOperations;
42 this.operationList = _.toArray(interfaceOperations);
46 getDisabled = (): boolean => {
47 return !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
50 onEditOperation = (operation: OperationModel): void => {
51 this.ComponentServiceNg2
52 .getInterfaceOperation(this.component, operation)
53 .subscribe(op => this.onAddOperation(op));
56 onAddOperation = (operation?: OperationModel): void => {
59 modalTitle: 'Create a New Operation',
60 saveBtnText: 'Create',
61 submitCallback: this.createOperation,
64 modalTitle: 'Edit Operation',
66 submitCallback: this.updateOperation,
70 const modalData = operation ? modalMap.edit : modalMap.create;
72 if (this.openOperation) {
73 if (operation ? operation.uniqueId === this.openOperation.uniqueId : !this.openOperation.uniqueId) {
74 operation = this.openOperation;
78 this.ComponentServiceNg2.getComponentInputs(this.component).subscribe((response: ComponentGenericResponse) => {
80 const cancelButton: ButtonModel = new ButtonModel(
84 this.openOperation = null;
85 this.ModalServiceNg2.closeCurrentModal();
89 const saveButton: ButtonModel = new ButtonModel(
90 modalData.saveBtnText,
93 this.modalInstance.instance.dynamicContent.instance.createInputParamList();
94 this.ModalServiceNg2.closeCurrentModal();
96 const {operation, isAssociateWorkflow} = this.modalInstance.instance.dynamicContent.instance;
97 this.openOperation = {...operation};
99 if (!isAssociateWorkflow) {
100 operation.workflowId = null;
101 operation.workflowVersionId = null;
104 modalData.submitCallback(operation);
109 const modalModel: ModalModel = new ModalModel(
111 modalData.modalTitle,
113 [saveButton, cancelButton],
117 this.modalInstance = this.ModalServiceNg2.createCustomModal(modalModel);
119 this.ModalServiceNg2.addDynamicContentToModal(
121 OperationCreatorComponent,
124 inputProperties: response.inputs,
128 this.modalInstance.instance.open();
132 onRemoveOperation = (event: Event, operation: OperationModel): void => {
133 event.stopPropagation();
135 const confirmCallback = () => {
136 this.ModalServiceNg2.closeCurrentModal();
137 this.ComponentServiceNg2
138 .deleteInterfaceOperation(this.component, operation)
140 const index = _.findIndex(this.operationList, el => el.uniqueId === operation.uniqueId);
141 this.operationList.splice(index, 1);
142 this.component.interfaceOperations = this.operationList;
146 this.modalInstance = this.ModalServiceNg2.createActionModal(
147 operation.operationType,
148 'Are you sure you want to delete this operation?',
154 this.modalInstance.instance.open();
157 private createOperation = (operation: OperationModel): any => {
158 this.ComponentServiceNg2.createInterfaceOperation(this.component, operation).subscribe((response: CreateOperationResponse) => {
159 this.openOperation = null;
160 this.operationList.push(new OperationModel(response));
161 if (response.workflowId) {
162 const resourceId = this.component.uuid;
163 const operationId = response.uniqueId;
164 const workflowId = response.workflowId;
165 const versionId = response.workflowVersionId;
166 const artifactId = response.artifactUUID;
167 this.WorkflowServiceNg2.associateWorkflowArtifact(resourceId, operationId, workflowId, versionId, artifactId).subscribe();
172 private updateOperation = (operation: OperationModel): any => {
173 this.ComponentServiceNg2.updateInterfaceOperation(this.component, operation).subscribe(newOperation => {
174 this.openOperation = null;
175 const index = _.findIndex(this.operationList, el => el.uniqueId === operation.uniqueId);
176 this.operationList.splice(index, 1, newOperation);
177 this.component.interfaceOperations = this.operationList;