aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/interface-operation/interface-operation.page.component.ts
blob: c58e1de135a363f721d13809f4667c534b170b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import * as _ from "lodash";
import {Component, Input, ComponentRef, Inject} from '@angular/core';
import {Component as IComponent} from 'app/models/components/component';
import {ModalService} from 'app/ng2/services/modal.service';
import {ModalModel, ButtonModel, InputModel, OperationModel, CreateOperationResponse} from 'app/models';
import {ModalComponent} from 'app/ng2/components/ui/modal/modal.component';
import {ComponentServiceNg2} from 'app/ng2/services/component-services/component.service';
import {ComponentGenericResponse} from 'app/ng2/services/responses/component-generic-response';
import {OperationCreatorComponent} from './operation-creator/operation-creator.component';

@Component({
    selector: 'interface-operation',
    templateUrl: './interface-operation.page.component.html',
    styleUrls: ['interface-operation.page.component.less'],
    providers: [ModalService]
})

export class InterfaceOperationComponent {

    modalInstance: ComponentRef<ModalComponent>;
    operationList: Array<OperationModel> = [];
    openOperation: OperationModel;

    @Input() component: IComponent;
    @Input() readonly: boolean;

    constructor(
        @Inject('$state') private $state:ng.ui.IStateService,
        private ComponentServiceNg2: ComponentServiceNg2,
        private ModalServiceNg2: ModalService,
    ) {}

    ngOnInit(): void {
        this.ComponentServiceNg2.getInterfaceOperations(this.component).subscribe((response: ComponentGenericResponse) => {
            let {interfaceOperations} = response;
            this.component.interfaceOperations = interfaceOperations;
            this.operationList = _.toArray(interfaceOperations);
        });
    }

    getDisabled = (): boolean => {
        return !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
    }

    onEditOperation = (operation: OperationModel): void => {
        this.ComponentServiceNg2
            .getInterfaceOperation(this.component, operation)
            .subscribe(op => this.onAddOperation(op));
    }

    onAddOperation = (operation?: OperationModel): void => {
        const modalMap = {
            create: {
                modalTitle: 'Create a New Operation',
                saveBtnText: 'Create',
                submitCallback: this.createOperation,
            },
            edit: {
                modalTitle: 'Edit Operation',
                saveBtnText: 'Save',
                submitCallback: this.updateOperation,
            }
        };

        const modalData = operation ? modalMap.edit : modalMap.create;

        if (this.openOperation) {
            if (operation ? operation.uniqueId === this.openOperation.uniqueId : !this.openOperation.uniqueId) {
                operation = this.openOperation;
            }
        }

        this.ComponentServiceNg2.getComponentInputs(this.component).subscribe((response: ComponentGenericResponse) => {

            const cancelButton: ButtonModel = new ButtonModel(
                'Cancel',
                'outline white',
                () => {
                    this.openOperation = null;
                    this.ModalServiceNg2.closeCurrentModal();
                },
            );

            const saveButton: ButtonModel = new ButtonModel(
                modalData.saveBtnText,
                'blue',
                () => {
                    this.modalInstance.instance.dynamicContent.instance.createInputParamList();
                    this.ModalServiceNg2.closeCurrentModal();
                    const {operation} = this.modalInstance.instance.dynamicContent.instance;
                    this.openOperation = operation;
                    modalData.submitCallback(operation);
                },
                this.getDisabled,
            );

            const modalModel: ModalModel = new ModalModel(
                'l',
                modalData.modalTitle,
                '',
                [saveButton, cancelButton],
                'standard',
            );

            this.modalInstance = this.ModalServiceNg2.createCustomModal(modalModel);
            this.ModalServiceNg2.addDynamicContentToModal(
                this.modalInstance,
                OperationCreatorComponent,
                {
                    operation,
                    inputProperties: response.inputs,
                },
            );

            this.modalInstance.instance.open();
        });
    }

    onRemoveOperation = (event: Event, operation: OperationModel): void => {
        event.stopPropagation();

        const confirmCallback = () => {
            this.ModalServiceNg2.closeCurrentModal();
            this.ComponentServiceNg2
                .deleteInterfaceOperation(this.component, operation)
                .subscribe(() => {
                    const index = _.findIndex(this.operationList, el => el.uniqueId === operation.uniqueId);
                    this.operationList.splice(index, 1);
                    this.component.interfaceOperations = this.operationList;
                });
        }

        this.modalInstance = this.ModalServiceNg2.createActionModal(
            operation.operationType,
            'Are you sure you want to delete this operation?',
            'Delete',
            confirmCallback,
            'Cancel',
        );

        this.modalInstance.instance.open();
    }

    private createOperation = (operation: OperationModel): any => {
        this.ComponentServiceNg2.createInterfaceOperation(this.component, operation).subscribe((response: CreateOperationResponse) => {
            this.openOperation = null;

            const workflowId = response.artifactUUID;
            const operationId = response.uniqueId;
            const resourceId = this.component.uuid;

            const queryParams = {
                id: workflowId,
                operationID: operationId,
                uuid: resourceId,
                displayMode: 'create',
            };

            this.$state.go('workspace.plugins', {
                path: 'workflowDesigner',
                queryParams
            });

        });
    }

    private updateOperation = (operation: OperationModel): any => {
        this.ComponentServiceNg2.updateInterfaceOperation(this.component, operation).subscribe(newOperation => {
            this.openOperation = null;
            const index = _.findIndex(this.operationList, el => el.uniqueId === operation.uniqueId);
            this.operationList.splice(index, 1, newOperation);
            this.component.interfaceOperations = this.operationList;
        });
    }

}