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';
16 WORKFLOW_ASSOCIATION_OPTIONS,
21 // import {SdcUiComponents } from 'sdc-ui/lib/angular';
22 // import {ModalButtonComponent } from 'sdc-ui/lib/angular/components';
23 // import { IModalButtonComponent, IModalConfig } from 'sdc-ui/lib/angular/modals/models/modal-config';
25 import {ComponentServiceNg2 } from 'app/ng2/services/component-services/component.service';
26 import {PluginsService } from 'app/ng2/services/plugins.service';
27 import {WorkflowServiceNg2 } from 'app/ng2/services/workflow.service';
29 import { OperationCreatorComponent, OperationCreatorInput } from 'app/ng2/pages/interface-operation/operation-creator/operation-creator.component';
30 import { IModalButtonComponent } from 'onap-ui-angular';
31 import { ModalButtonComponent } from 'onap-ui-angular';
32 import { IModalConfig } from 'onap-ui-angular';
33 import { SdcUiServices } from 'onap-ui-angular';
35 export class UIOperationModel extends OperationModel {
36 isCollapsed: boolean = true;
41 constructor(operation: OperationModel) {
44 if (!operation.description) {
45 this.description = '';
48 if (this.description.length > this.MAX_LENGTH) {
49 this.isEllipsis = true;
51 this.isEllipsis = false;
55 getDescriptionEllipsis(): string {
56 if (this.isCollapsed && this.description.length > this.MAX_LENGTH) {
57 return this.description.substr(0, this.MAX_LENGTH - 3) + '...';
59 return this.description;
64 this.isCollapsed = !this.isCollapsed;
68 // tslint:disable-next-line:max-classes-per-file
69 class ModalTranslation {
73 CANCEL_BUTTON: string;
75 CREATE_BUTTON: string;
76 DELETE_BUTTON: string;
79 constructor(private TranslateService: TranslateService) {
80 this.TranslateService.languageChangedObservable.subscribe(lang => {
81 this.CREATE_TITLE = this.TranslateService.translate("INTERFACE_CREATE_TITLE");
82 this.EDIT_TITLE = this.TranslateService.translate('INTERFACE_EDIT_TITLE');
83 this.DELETE_TITLE = this.TranslateService.translate("INTERFACE_DELETE_TITLE");
84 this.CANCEL_BUTTON = this.TranslateService.translate("INTERFACE_CANCEL_BUTTON");
85 this.SAVE_BUTTON = this.TranslateService.translate("INTERFACE_SAVE_BUTTON");
86 this.CREATE_BUTTON = this.TranslateService.translate("INTERFACE_CREATE_BUTTON");
87 this.DELETE_BUTTON = this.TranslateService.translate("INTERFACE_DELETE_BUTTON");
88 this.deleteText = (operationName) => this.TranslateService.translate("INTERFACE_DELETE_TEXT", {operationName});
93 // tslint:disable-next-line:max-classes-per-file
94 export class UIInterfaceModel extends InterfaceModel {
95 isCollapsed: boolean = false;
97 constructor(interf?: any) {
99 this.operations = _.map(
101 (operation) => new UIOperationModel(operation)
106 this.isCollapsed = !this.isCollapsed;
110 // tslint:disable-next-line:max-classes-per-file
112 selector: 'interface-operation',
113 templateUrl: './interface-operation.page.component.html',
114 styleUrls: ['interface-operation.page.component.less'],
115 providers: [ModalService, TranslateService]
118 export class InterfaceOperationComponent {
120 interfaces: UIInterfaceModel[];
121 modalInstance: ComponentRef<ModalComponent>;
122 openOperation: OperationModel;
123 enableWorkflowAssociation: boolean;
124 inputs: InputBEModel[];
126 interfaceTypes: { [interfaceType: string]: string[] };
127 modalTranslation: ModalTranslation;
128 workflowIsOnline: boolean;
130 capabilities: CapabilitiesGroup;
132 @Input() component: IComponent;
133 @Input() readonly: boolean;
134 @Input() enableMenuItems: Function;
135 @Input() disableMenuItems: Function;
138 @Inject(SdcConfigToken) private sdcConfig: ISdcConfig,
139 @Inject("$state") private $state: ng.ui.IStateService,
140 private TranslateService: TranslateService,
141 private PluginsService: PluginsService,
142 private ComponentServiceNg2: ComponentServiceNg2,
143 private WorkflowServiceNg2: WorkflowServiceNg2,
144 private ModalServiceNg2: ModalService,
145 private ModalServiceSdcUI: SdcUiServices.ModalService
148 this.enableWorkflowAssociation = sdcConfig.enableWorkflowAssociation;
149 this.modalTranslation = new ModalTranslation(TranslateService);
153 this.isLoading = true;
154 this.workflowIsOnline = !_.isUndefined(this.PluginsService.getPluginByStateUrl('workflowDesigner'));
157 this.ComponentServiceNg2.getInterfaceOperations(this.component),
158 this.ComponentServiceNg2.getComponentInputs(this.component),
159 this.ComponentServiceNg2.getInterfaceTypes(this.component),
160 this.ComponentServiceNg2.getCapabilitiesAndRequirements(this.component.componentType, this.component.uniqueId)
161 ).subscribe((response: any[]) => {
162 const callback = (workflows) => {
163 this.isLoading = false;
164 this.initInterfaces(response[0].interfaces);
165 this.sortInterfaces();
166 this.inputs = response[1].inputs;
167 this.interfaceTypes = response[2];
168 this.workflows = workflows;
169 this.capabilities = response[3].capabilities;
171 if (this.enableWorkflowAssociation && this.workflowIsOnline) {
172 this.WorkflowServiceNg2.getWorkflows().subscribe(
175 this.workflowIsOnline = false;
185 initInterfaces(interfaces: InterfaceModel[]): void {
186 this.interfaces = _.map(interfaces, (interf) => new UIInterfaceModel(interf));
189 sortInterfaces(): void {
190 this.interfaces = _.filter(this.interfaces, (interf) => interf.operations && interf.operations.length > 0); // remove empty interfaces
191 this.interfaces.sort((a, b) => a.type.localeCompare(b.type)); // sort interfaces alphabetically
192 _.forEach(this.interfaces, (interf) => {
193 interf.operations.sort((a, b) => a.name.localeCompare(b.name)); // sort operations alphabetically
197 collapseAll(value: boolean = true): void {
198 _.forEach(this.interfaces, (interf) => {
199 interf.isCollapsed = value;
203 isAllCollapsed(): boolean {
204 return _.every(this.interfaces, (interf) => interf.isCollapsed);
207 isAllExpanded(): boolean {
208 return _.every(this.interfaces, (interf) => !interf.isCollapsed);
211 isListEmpty(): boolean {
214 (interf) => interf.operations && interf.operations.length > 0
218 getDisabled = (): boolean => {
219 return !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
222 onEditOperation = (operation?: OperationModel): void => {
226 modalTitle: this.modalTranslation.CREATE_TITLE,
227 saveBtnText: this.modalTranslation.CREATE_BUTTON,
228 submitCallback: this.createOperation,
231 modalTitle: this.modalTranslation.EDIT_TITLE,
232 saveBtnText: this.modalTranslation.SAVE_BUTTON,
233 submitCallback: this.updateOperation,
237 const modalData = operation ? modalMap.edit : modalMap.create;
239 if (this.openOperation) {
240 if (operation ? operation.uniqueId === this.openOperation.uniqueId : !this.openOperation.uniqueId) {
241 operation = this.openOperation;
245 const cancelButton: IModalButtonComponent = {
247 text: this.modalTranslation.CANCEL_BUTTON,
252 this.openOperation = null;
256 const saveButton: IModalButtonComponent = {
258 text: modalData.saveBtnText,
263 const modalInstance = this.ModalServiceSdcUI.getCurrentInstance().innerModalContent.instance;
265 const {operation, isUsingExistingWF, createParamLists} = modalInstance;
267 this.openOperation = {...operation};
269 if (this.enableWorkflowAssociation && !isUsingExistingWF()) {
270 operation.workflowId = null;
271 operation.workflowVersionId = null;
274 modalData.submitCallback(operation);
278 const input: OperationCreatorInput = {
279 allWorkflows: this.workflows,
280 inputOperation: operation,
281 interfaces: this.interfaces,
282 inputProperties: this.inputs,
283 enableWorkflowAssociation: this.enableWorkflowAssociation,
284 readonly: this.readonly,
285 interfaceTypes: this.interfaceTypes,
286 validityChangedCallback: this.enableOrDisableSaveButton,
287 workflowIsOnline: this.workflowIsOnline,
288 capabilities: _.filter(CapabilitiesGroup.getFlattenedCapabilities(this.capabilities), (capability: Capability) => capability.ownerId === this.component.uniqueId)
291 const modalConfig: IModalConfig = {
292 title: modalData.modalTitle,
295 buttons: [saveButton, cancelButton] as IModalButtonComponent[]
298 this.ModalServiceSdcUI.openCustomModal(modalConfig, OperationCreatorComponent, input);
302 onRemoveOperation = (event: Event, operation: OperationModel): void => {
303 event.stopPropagation();
305 const confirmCallback = () => {
306 this.ComponentServiceNg2
307 .deleteInterfaceOperation(this.component, operation)
309 const curInterf = _.find(this.interfaces, (interf) => interf.type === operation.interfaceType);
310 const index = _.findIndex(curInterf.operations, (el) => el.uniqueId === operation.uniqueId);
311 curInterf.operations.splice(index, 1);
312 if (!curInterf.operations.length) {
313 const interfIndex = _.findIndex(this.interfaces, (interf) => interf.type === operation.interfaceType);
314 this.interfaces.splice(interfIndex, 1);
319 this.ModalServiceSdcUI.openAlertModal(
320 this.modalTranslation.DELETE_TITLE,
321 this.modalTranslation.deleteText(operation.name),
322 this.modalTranslation.DELETE_BUTTON,
324 'deleteOperationModal'
328 private enableOrDisableSaveButton = (shouldEnable: boolean): void => {
329 const saveButton: ModalButtonComponent = this.ModalServiceSdcUI.getCurrentInstance().getButtonById('saveButton');
330 saveButton.disabled = !shouldEnable;
333 private createOperation = (operation: OperationModel): void => {
334 this.ComponentServiceNg2.createInterfaceOperation(this.component, operation).subscribe((response: OperationModel) => {
335 this.openOperation = null;
337 let curInterf = _.find(
339 (interf) => interf.type === operation.interfaceType
343 curInterf = new UIInterfaceModel({
344 type: response.interfaceType,
345 uniqueId: response.uniqueId,
348 this.interfaces.push(curInterf);
351 const newOpModel = new UIOperationModel(response);
352 curInterf.operations.push(newOpModel);
353 this.sortInterfaces();
355 if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXTERNAL && operation.artifactData) {
356 this.ComponentServiceNg2.uploadInterfaceOperationArtifact(this.component, newOpModel, operation).subscribe();
357 } else if (response.workflowId && operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXISTING) {
358 this.WorkflowServiceNg2.associateWorkflowArtifact(this.component, response).subscribe();
359 } else if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.NEW) {
360 this.$state.go('workspace.plugins', { path: 'workflowDesigner' });
365 private updateOperation = (operation: OperationModel): void => {
366 this.ComponentServiceNg2.updateInterfaceOperation(this.component, operation).subscribe((newOperation: OperationModel) => {
367 this.openOperation = null;
371 _.forEach(this.interfaces, (interf) => {
372 _.forEach(interf.operations, (op) => {
373 if (op.uniqueId === newOperation.uniqueId) {
375 oldOpIndex = _.findIndex(interf.operations, (el) => el.uniqueId === op.uniqueId);
379 oldInterf.operations.splice(oldOpIndex, 1);
381 const newInterf = _.find(this.interfaces, (interf) => interf.type === operation.interfaceType);
382 const newOpModel = new UIOperationModel(newOperation);
383 newInterf.operations.push(newOpModel);
384 this.sortInterfaces();
386 if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXTERNAL && operation.artifactData) {
387 this.ComponentServiceNg2.uploadInterfaceOperationArtifact(this.component, newOpModel, operation).subscribe();
388 } else if (newOperation.workflowId && operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXISTING) {
389 this.WorkflowServiceNg2.associateWorkflowArtifact(this.component, newOperation).subscribe();