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'));
140 const workflowSubscription = this.enableWorkflowAssociation && this.workflowIsOnline ? this.WorkflowServiceNg2.getWorkflows() : Promise.resolve();
142 this.ComponentServiceNg2.getInterfaces(this.component),
143 this.ComponentServiceNg2.getComponentInputs(this.component),
144 this.ComponentServiceNg2.getInterfaceTypes(this.component),
146 ).subscribe((response: Array<any>) => {
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 = response[3];
156 initInterfaces(interfaces: Array<InterfaceModel>): void {
157 this.interfaces = _.map(interfaces, interf => new UIInterfaceModel(interf));
160 sortInterfaces(): void {
161 this.interfaces = _.filter(this.interfaces, interf => interf.operations && interf.operations.length > 0); // remove empty interfaces
162 this.interfaces.sort((a, b) => a.type.localeCompare(b.type)); // sort interfaces alphabetically
163 _.forEach(this.interfaces, interf => {
164 interf.operations.sort((a, b) => a.name.localeCompare(b.name)); // sort operations alphabetically
168 collapseAll(value: boolean = true): void {
169 _.forEach(this.interfaces, interf => {
170 interf.isCollapsed = value;
174 isAllCollapsed(): boolean {
175 return _.every(this.interfaces, interf => interf.isCollapsed);
178 isAllExpanded(): boolean {
179 return _.every(this.interfaces, interf => !interf.isCollapsed);
182 isListEmpty(): boolean {
185 interf => interf.operations && interf.operations.length > 0
189 getDisabled = (): boolean => {
190 return !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
193 onEditOperation = (operation?: OperationModel): void => {
197 modalTitle: this.modalTranslation.CREATE_TITLE,
198 saveBtnText: this.modalTranslation.CREATE_BUTTON,
199 submitCallback: this.createOperation,
202 modalTitle: this.modalTranslation.EDIT_TITLE,
203 saveBtnText: this.modalTranslation.SAVE_BUTTON,
204 submitCallback: this.updateOperation,
208 const modalData = operation ? modalMap.edit : modalMap.create;
210 if (this.openOperation) {
211 if (operation ? operation.uniqueId === this.openOperation.uniqueId : !this.openOperation.uniqueId) {
212 operation = this.openOperation;
216 const cancelButton: IModalButtonComponent = {
218 text: this.modalTranslation.CANCEL_BUTTON,
223 this.openOperation = null;
227 const saveButton: IModalButtonComponent = {
229 text: modalData.saveBtnText,
234 const modalInstance = this.ModalServiceSdcUI.getCurrentInstance().innerModalContent.instance;
236 const {operation, isUsingExistingWF, createParamLists} = modalInstance;
238 this.openOperation = {...operation};
240 if (this.enableWorkflowAssociation && !isUsingExistingWF()) {
241 operation.workflowId = null;
242 operation.workflowVersionId = null;
245 modalData.submitCallback(operation);
249 const input: OperationCreatorInput = {
250 allWorkflows: this.workflows,
251 inputOperation: operation,
252 interfaces: this.interfaces,
253 inputProperties: this.inputs,
254 enableWorkflowAssociation: this.enableWorkflowAssociation,
255 readonly: this.readonly,
256 interfaceTypes: this.interfaceTypes,
257 validityChangedCallback: this.enableOrDisableSaveButton,
258 workflowIsOnline: this.workflowIsOnline
261 const modalConfig: IModalConfig = {
262 title: modalData.modalTitle,
265 buttons: [saveButton, cancelButton] as IModalButtonComponent[]
268 this.ModalServiceSdcUI.openCustomModal(modalConfig, OperationCreatorComponent, input);
272 private enableOrDisableSaveButton = (shouldEnable: boolean): void => {
273 let saveButton: ModalButtonComponent = this.ModalServiceSdcUI.getCurrentInstance().getButtonById('saveButton');
274 saveButton.disabled = !shouldEnable;
277 onRemoveOperation = (event: Event, operation: OperationModel): void => {
278 event.stopPropagation();
280 const confirmCallback = () => {
281 this.ComponentServiceNg2
282 .deleteInterfaceOperation(this.component, operation)
284 const curInterf = _.find(this.interfaces, interf => interf.type === operation.interfaceType);
285 const index = _.findIndex(curInterf.operations, el => el.uniqueId === operation.uniqueId);
286 curInterf.operations.splice(index, 1);
287 if (!curInterf.operations.length) {
288 const interfIndex = _.findIndex(this.interfaces, interf => interf.type === operation.interfaceType);
289 this.interfaces.splice(interfIndex, 1);
294 this.ModalServiceSdcUI.openAlertModal(
295 this.modalTranslation.DELETE_TITLE,
296 this.modalTranslation.deleteText(operation.name),
297 this.modalTranslation.DELETE_BUTTON,
299 'deleteOperationModal'
303 private createOperation = (operation: OperationModel): void => {
304 this.ComponentServiceNg2.createInterfaceOperation(this.component, operation).subscribe((response: OperationModel) => {
305 this.openOperation = null;
306 let curInterf = _.find(
308 interf => interf.type === operation.interfaceType
311 curInterf = new UIInterfaceModel({
312 type: response.interfaceType,
313 uniqueId: response.uniqueId,
316 this.interfaces.push(curInterf);
318 curInterf.operations.push(new UIOperationModel(response));
319 this.sortInterfaces();
321 if (response.workflowId && operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXISTING) {
322 this.WorkflowServiceNg2.associateWorkflowArtifact(this.component, response).subscribe();
323 } else if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.NEW) {
324 this.$state.go('workspace.plugins', { path: 'workflowDesigner' });
329 private updateOperation = (operation: OperationModel): void => {
330 this.ComponentServiceNg2.updateInterfaceOperation(this.component, operation).subscribe(newOperation => {
331 this.openOperation = null;
333 _.forEach(this.interfaces, interf => {
334 _.forEach(interf.operations, op => {
335 if (op.uniqueId === newOperation.uniqueId) {
336 const oldIndex = _.findIndex(interf.operations, el => el.uniqueId === op.uniqueId);
337 interf.operations.splice(oldIndex, 1);
342 const newInterf = _.find(this.interfaces, interf => interf.type === operation.interfaceType);
343 newInterf.operations.push(new UIOperationModel(newOperation));
344 this.sortInterfaces();
346 if (newOperation.workflowId && operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXISTING) {
347 this.WorkflowServiceNg2.associateWorkflowArtifact(this.component, newOperation).subscribe();