1 import * as _ from "lodash";
2 import {Component, Input, Output, ComponentRef, Inject} from '@angular/core';
3 import {Component as IComponent} from 'app/models/components/component';
5 import {SdcConfigToken, ISdcConfig} from "app/ng2/config/sdc-config.config";
6 import {TranslateService} from "app/ng2/shared/translator/translate.service";
8 import {Observable} from "rxjs/Observable";
10 import {ModalComponent} from 'app/ng2/components/ui/modal/modal.component';
11 import {ModalService} from 'app/ng2/services/modal.service';
12 import {ModalModel, ButtonModel, InputBEModel, OperationModel, InterfaceModel, WORKFLOW_ASSOCIATION_OPTIONS} from 'app/models';
14 import {IModalConfig, IModalButtonComponent} from "sdc-ui/lib/angular/modals/models/modal-config";
15 import {SdcUiComponents} from "sdc-ui/lib/angular";
16 import {ModalButtonComponent} from "sdc-ui/lib/angular/components";
18 import {ComponentServiceNg2} from 'app/ng2/services/component-services/component.service';
19 import {ComponentGenericResponse} from 'app/ng2/services/responses/component-generic-response';
20 import {WorkflowServiceNg2} from 'app/ng2/services/workflow.service';
21 import {PluginsService} from "app/ng2/services/plugins.service";
23 import {OperationCreatorComponent, OperationCreatorInput} from 'app/ng2/pages/interface-operation/operation-creator/operation-creator.component';
25 export class UIOperationModel extends OperationModel {
26 isCollapsed: boolean = true;
31 constructor(operation: OperationModel) {
34 if (!operation.description) {
35 this.description = '';
38 if (this.description.length > this.MAX_LENGTH) {
39 this.isEllipsis = true;
41 this.isEllipsis = false;
45 getDescriptionEllipsis(): string {
46 if (this.isCollapsed && this.description.length > this.MAX_LENGTH) {
47 return this.description.substr(0, this.MAX_LENGTH - 3) + '...';
49 return this.description;
54 this.isCollapsed = !this.isCollapsed;
58 class ModalTranslation {
62 CANCEL_BUTTON: string;
64 CREATE_BUTTON: string;
65 DELETE_BUTTON: string;
68 constructor(private TranslateService: TranslateService) {
69 this.TranslateService.languageChangedObservable.subscribe(lang => {
70 this.CREATE_TITLE = this.TranslateService.translate("INTERFACE_CREATE_TITLE");
71 this.EDIT_TITLE = this.TranslateService.translate("INTERFACE_EDIT_TITLE");
72 this.DELETE_TITLE = this.TranslateService.translate("INTERFACE_DELETE_TITLE");
73 this.CANCEL_BUTTON = this.TranslateService.translate("INTERFACE_CANCEL_BUTTON");
74 this.SAVE_BUTTON = this.TranslateService.translate("INTERFACE_SAVE_BUTTON");
75 this.CREATE_BUTTON = this.TranslateService.translate("INTERFACE_CREATE_BUTTON");
76 this.DELETE_BUTTON = this.TranslateService.translate("INTERFACE_DELETE_BUTTON");
77 this.deleteText = (operationName) => this.TranslateService.translate("INTERFACE_DELETE_TEXT", {operationName});
82 export class UIInterfaceModel extends InterfaceModel {
83 isCollapsed: boolean = false;
85 constructor(interf?: any) {
87 this.operations = _.map(
89 operation => new UIOperationModel(operation)
94 this.isCollapsed = !this.isCollapsed;
99 selector: 'interface-operation',
100 templateUrl: './interface-operation.page.component.html',
101 styleUrls: ['interface-operation.page.component.less'],
102 providers: [ModalService, TranslateService]
105 export class InterfaceOperationComponent {
107 interfaces: Array<UIInterfaceModel>;
108 modalInstance: ComponentRef<ModalComponent>;
109 openOperation: OperationModel;
110 enableWorkflowAssociation: boolean;
111 inputs: Array<InputBEModel>;
113 interfaceTypes:{ [interfaceType: string]: Array<string> };
114 modalTranslation: ModalTranslation;
115 workflowIsOnline: boolean;
116 workflows: Array<any>;
118 @Input() component: IComponent;
119 @Input() readonly: boolean;
120 @Input() enableMenuItems: Function;
121 @Input() disableMenuItems: Function;
124 @Inject(SdcConfigToken) private sdcConfig: ISdcConfig,
125 @Inject("$state") private $state: ng.ui.IStateService,
126 private TranslateService: TranslateService,
127 private PluginsService: PluginsService,
128 private ComponentServiceNg2: ComponentServiceNg2,
129 private WorkflowServiceNg2: WorkflowServiceNg2,
130 private ModalServiceNg2: ModalService,
131 private ModalServiceSdcUI: SdcUiComponents.ModalService
133 this.enableWorkflowAssociation = sdcConfig.enableWorkflowAssociation;
134 this.modalTranslation = new ModalTranslation(TranslateService);
138 this.isLoading = true;
139 this.workflowIsOnline = !_.isUndefined(this.PluginsService.getPluginByStateUrl('workflowDesigner'));
142 this.ComponentServiceNg2.getInterfaces(this.component),
143 this.ComponentServiceNg2.getComponentInputs(this.component),
144 this.ComponentServiceNg2.getInterfaceTypes(this.component)
145 ).subscribe((response: Array<any>) => {
146 const callback = (workflows) => {
147 this.isLoading = false;
148 this.initInterfaces(response[0].interfaces);
149 this.sortInterfaces();
150 this.inputs = response[1].inputs;
151 this.interfaceTypes = response[2];
152 this.workflows = workflows;
154 if (this.enableWorkflowAssociation && this.workflowIsOnline) {
155 this.WorkflowServiceNg2.getWorkflows().subscribe(
158 this.workflowIsOnline = false;
168 initInterfaces(interfaces: Array<InterfaceModel>): void {
169 this.interfaces = _.map(interfaces, interf => new UIInterfaceModel(interf));
172 sortInterfaces(): void {
173 this.interfaces = _.filter(this.interfaces, interf => interf.operations && interf.operations.length > 0); // remove empty interfaces
174 this.interfaces.sort((a, b) => a.type.localeCompare(b.type)); // sort interfaces alphabetically
175 _.forEach(this.interfaces, interf => {
176 interf.operations.sort((a, b) => a.name.localeCompare(b.name)); // sort operations alphabetically
180 collapseAll(value: boolean = true): void {
181 _.forEach(this.interfaces, interf => {
182 interf.isCollapsed = value;
186 isAllCollapsed(): boolean {
187 return _.every(this.interfaces, interf => interf.isCollapsed);
190 isAllExpanded(): boolean {
191 return _.every(this.interfaces, interf => !interf.isCollapsed);
194 isListEmpty(): boolean {
197 interf => interf.operations && interf.operations.length > 0
201 getDisabled = (): boolean => {
202 return !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
205 onEditOperation = (operation?: OperationModel): void => {
209 modalTitle: this.modalTranslation.CREATE_TITLE,
210 saveBtnText: this.modalTranslation.CREATE_BUTTON,
211 submitCallback: this.createOperation,
214 modalTitle: this.modalTranslation.EDIT_TITLE,
215 saveBtnText: this.modalTranslation.SAVE_BUTTON,
216 submitCallback: this.updateOperation,
220 const modalData = operation ? modalMap.edit : modalMap.create;
222 if (this.openOperation) {
223 if (operation ? operation.uniqueId === this.openOperation.uniqueId : !this.openOperation.uniqueId) {
224 operation = this.openOperation;
228 const cancelButton: IModalButtonComponent = {
230 text: this.modalTranslation.CANCEL_BUTTON,
235 this.openOperation = null;
239 const saveButton: IModalButtonComponent = {
241 text: modalData.saveBtnText,
246 const modalInstance = this.ModalServiceSdcUI.getCurrentInstance().innerModalContent.instance;
248 const {operation, isUsingExistingWF, createParamLists} = modalInstance;
250 this.openOperation = {...operation};
252 if (this.enableWorkflowAssociation && !isUsingExistingWF()) {
253 operation.workflowId = null;
254 operation.workflowVersionId = null;
257 modalData.submitCallback(operation);
261 const input: OperationCreatorInput = {
262 allWorkflows: this.workflows,
263 inputOperation: operation,
264 interfaces: this.interfaces,
265 inputProperties: this.inputs,
266 enableWorkflowAssociation: this.enableWorkflowAssociation,
267 readonly: this.readonly,
268 interfaceTypes: this.interfaceTypes,
269 validityChangedCallback: this.enableOrDisableSaveButton,
270 workflowIsOnline: this.workflowIsOnline
273 const modalConfig: IModalConfig = {
274 title: modalData.modalTitle,
277 buttons: [saveButton, cancelButton] as IModalButtonComponent[]
280 this.ModalServiceSdcUI.openCustomModal(modalConfig, OperationCreatorComponent, input);
284 private enableOrDisableSaveButton = (shouldEnable: boolean): void => {
285 let saveButton: ModalButtonComponent = this.ModalServiceSdcUI.getCurrentInstance().getButtonById('saveButton');
286 saveButton.disabled = !shouldEnable;
289 onRemoveOperation = (event: Event, operation: OperationModel): void => {
290 event.stopPropagation();
292 const confirmCallback = () => {
293 this.ComponentServiceNg2
294 .deleteInterfaceOperation(this.component, operation)
296 const curInterf = _.find(this.interfaces, interf => interf.type === operation.interfaceType);
297 const index = _.findIndex(curInterf.operations, el => el.uniqueId === operation.uniqueId);
298 curInterf.operations.splice(index, 1);
299 if (!curInterf.operations.length) {
300 const interfIndex = _.findIndex(this.interfaces, interf => interf.type === operation.interfaceType);
301 this.interfaces.splice(interfIndex, 1);
306 this.ModalServiceSdcUI.openAlertModal(
307 this.modalTranslation.DELETE_TITLE,
308 this.modalTranslation.deleteText(operation.name),
309 this.modalTranslation.DELETE_BUTTON,
311 'deleteOperationModal'
315 private createOperation = (operation: OperationModel): void => {
316 this.ComponentServiceNg2.createInterfaceOperation(this.component, operation).subscribe((response: OperationModel) => {
317 this.openOperation = null;
318 let curInterf = _.find(
320 interf => interf.type === operation.interfaceType
323 curInterf = new UIInterfaceModel({
324 type: response.interfaceType,
325 uniqueId: response.uniqueId,
328 this.interfaces.push(curInterf);
330 curInterf.operations.push(new UIOperationModel(response));
331 this.sortInterfaces();
333 if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXTERNAL) {
334 this.ComponentServiceNg2.uploadInterfaceOperationArtifact(this.component, response, operation).subscribe();
335 } else if (response.workflowId && operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXISTING) {
336 this.WorkflowServiceNg2.associateWorkflowArtifact(this.component, response).subscribe();
337 } else if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.NEW) {
338 this.$state.go('workspace.plugins', { path: 'workflowDesigner' });
343 private updateOperation = (operation: OperationModel): void => {
344 this.ComponentServiceNg2.updateInterfaceOperation(this.component, operation).subscribe(newOperation => {
345 this.openOperation = null;
347 let oldOpIndex, oldInterf;
348 _.forEach(this.interfaces, interf => {
349 _.forEach(interf.operations, op => {
350 if (op.uniqueId === newOperation.uniqueId) {
352 oldOpIndex = _.findIndex(interf.operations, el => el.uniqueId === op.uniqueId);
356 oldInterf.operations.splice(oldOpIndex, 1);
358 const newInterf = _.find(this.interfaces, interf => interf.type === operation.interfaceType);
359 newInterf.operations.push(new UIOperationModel(newOperation));
360 this.sortInterfaces();
362 if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXTERNAL) {
363 this.ComponentServiceNg2.uploadInterfaceOperationArtifact(this.component, newOperation, operation).subscribe();
364 } else if (newOperation.workflowId && operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXISTING) {
365 this.WorkflowServiceNg2.associateWorkflowArtifact(this.component, newOperation).subscribe();