Block interface operation edit in checkedin VFC
[sdc.git] / catalog-ui / src / app / ng2 / pages / interface-definition / interface-definition.page.component.ts
index c9a6d07..eab8b18 100644 (file)
@@ -29,6 +29,7 @@ import {ModalComponent} from 'app/ng2/components/ui/modal/modal.component';
 
 import {ModalService} from 'app/ng2/services/modal.service';
 import {
+    ArtifactModel,
     ButtonModel,
     CapabilitiesGroup,
     InputBEModel,
@@ -41,17 +42,11 @@ import {
 import {ComponentServiceNg2} from 'app/ng2/services/component-services/component.service';
 import {TopologyTemplateService} from "../../services/component-services/topology-template.service";
 import {InterfaceOperationModel} from "../../../models/interfaceOperation";
-import {
-    InterfaceOperationHandlerComponent
-} from "../composition/interface-operatons/operation-creator/interface-operation-handler.component";
-import {
-    DropdownValue
-} from "../../components/ui/form-components/dropdown/ui-element-dropdown.component";
+import {InterfaceOperationHandlerComponent} from "../composition/interface-operatons/operation-creator/interface-operation-handler.component";
+import {DropdownValue} from "../../components/ui/form-components/dropdown/ui-element-dropdown.component";
 import {ToscaArtifactModel} from "../../../models/toscaArtifact";
 import {ToscaArtifactService} from "../../services/tosca-artifact.service";
-import {
-    InterfaceOperationComponent
-} from "../interface-operation/interface-operation.page.component";
+import {InterfaceOperationComponent} from "../interface-operation/interface-operation.page.component";
 import {Observable} from "rxjs/Observable";
 import {PluginsService} from 'app/ng2/services/plugins.service';
 
@@ -62,7 +57,6 @@ export class UIOperationModel extends OperationModel {
 
     constructor(operation: OperationModel) {
         super(operation);
-
         if (!operation.description) {
             this.description = '';
         }
@@ -87,7 +81,6 @@ export class UIOperationModel extends OperationModel {
     }
 }
 
-// tslint:disable-next-line:max-classes-per-file
 class ModalTranslation {
     CREATE_TITLE: string;
     EDIT_TITLE: string;
@@ -127,7 +120,6 @@ export class UIInterfaceModel extends InterfaceModel {
     }
 }
 
-// tslint:disable-next-line:max-classes-per-file
 @Component({
     selector: 'interface-definition',
     templateUrl: './interface-definition.page.component.html',
@@ -151,7 +143,6 @@ export class InterfaceDefinitionComponent {
     modalTranslation: ModalTranslation;
     workflows: any[];
     capabilities: CapabilitiesGroup;
-    isViewOnly: boolean;
 
     openOperation: OperationModel;
     enableWorkflowAssociation: boolean;
@@ -212,6 +203,7 @@ export class InterfaceDefinitionComponent {
                 callback([]);
             }
         });
+        this.loadToscaArtifacts();
     }
 
     initInterfaces(interfaces: InterfaceModel[]): void {
@@ -225,7 +217,7 @@ export class InterfaceDefinitionComponent {
     }
 
     private disableSaveButton = (): boolean => {
-        return this.isViewOnly ||
+        return this.readonly ||
             (this.isEnableAddArtifactImplementation()
                 && (!this.modalInstance.instance.dynamicContent.toscaArtifactTypeSelected ||
                     !this.modalInstance.instance.dynamicContent.artifactName)
@@ -234,13 +226,17 @@ export class InterfaceDefinitionComponent {
 
     onSelectInterfaceOperation(interfaceModel: UIInterfaceModel, operation: InterfaceOperationModel) {
         const isEdit = operation !== undefined;
-        const cancelButton: ButtonModel = new ButtonModel(this.modalTranslation.CANCEL_BUTTON, 'outline white', this.cancelAndCloseModal);
-        const saveButton: ButtonModel = new ButtonModel(this.modalTranslation.SAVE_BUTTON, 'blue',
-            () => isEdit ? this.updateOperation() : this.createOperationCallback(),
-            this.disableSaveButton
-        );
+        const modalButtons = [];
+        if (!this.readonly) {
+            const saveButton: ButtonModel = new ButtonModel(this.modalTranslation.SAVE_BUTTON, 'blue',
+                () => isEdit ? this.updateOperation() : this.createOperationCallback(),
+                this.disableSaveButton
+            );
+            modalButtons.push(saveButton);
+        }
+        modalButtons.push(new ButtonModel(this.modalTranslation.CANCEL_BUTTON, 'outline white', this.cancelAndCloseModal));
         const interfaceDataModal: ModalModel =
-            new ModalModel('l', this.modalTranslation.EDIT_TITLE, '', [saveButton, cancelButton], 'custom');
+            new ModalModel('l', this.modalTranslation.EDIT_TITLE, '', modalButtons, 'custom');
         this.modalInstance = this.modalServiceNg2.createCustomModal(interfaceDataModal);
 
         this.modalServiceNg2.addDynamicContentToModal(
@@ -252,16 +248,25 @@ export class InterfaceDefinitionComponent {
                 selectedInterface: interfaceModel ? interfaceModel : new UIInterfaceModel(),
                 selectedInterfaceOperation: operation ? operation : new InterfaceOperationModel(),
                 validityChangedCallback: this.disableSaveButton,
-                isViewOnly: this.isViewOnly,
-                isEdit: isEdit,
+                isViewOnly: this.readonly,
+                'isEdit': isEdit,
                 interfaceTypesMap: this.interfaceTypesMap,
+                modelName: this.component.model
             }
         );
         this.modalInstance.instance.open();
     }
 
     private updateOperation = (): void => {
-        let operationToUpdate = this.modalInstance.instance.dynamicContent.instance.operationToUpdate;
+        this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = true;
+        const interfaceOperationHandlerComponentInstance: InterfaceOperationHandlerComponent = this.modalInstance.instance.dynamicContent.instance;
+        const operationToUpdate = this.modalInstance.instance.dynamicContent.instance.operationToUpdate;
+        const isArtifactChecked = interfaceOperationHandlerComponentInstance.enableAddArtifactImplementation;
+        if (!isArtifactChecked) {
+            const artifactName = interfaceOperationHandlerComponentInstance.artifactName ?
+                interfaceOperationHandlerComponentInstance.artifactName : '';
+            operationToUpdate.implementation = new ArtifactModel({'artifactName': artifactName, 'artifactVersion': ''} as ArtifactModel);
+        }
         this.componentServiceNg2.updateComponentInterfaceOperation(this.component.uniqueId, operationToUpdate)
         .subscribe((newOperation: InterfaceOperationModel) => {
             let oldOpIndex;
@@ -274,14 +279,19 @@ export class InterfaceDefinitionComponent {
                     }
                 });
             });
-            newOperation = this.handleEnableAddArtifactImplementation(newOperation);
             oldInterf.operations.splice(oldOpIndex, 1);
             oldInterf.operations.push(new InterfaceOperationModel(newOperation));
+        }, error => {
+            this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = false;
+        }, () => {
+            this.sortInterfaces();
+            this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = false;
+            this.modalServiceNg2.closeCurrentModal();
         });
-        this.modalServiceNg2.closeCurrentModal();
     }
 
     private createOperationCallback(): void {
+        this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = true;
         const operationToUpdate = this.modalInstance.instance.dynamicContent.instance.operationToUpdate;
         console.log('createOperationCallback', operationToUpdate);
         console.log('this.component', this.component);
@@ -298,8 +308,12 @@ export class InterfaceDefinitionComponent {
                 uiInterfaceModel.operations.push(new UIOperationModel(new OperationModel(newOperation)));
                 this.interfaces.push(uiInterfaceModel);
             }
+        }, error => {
+            this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = false;
+        }, () => {
+            this.modalServiceNg2.currentModal.instance.dynamicContent.instance.isLoading = false;
+            this.modalServiceNg2.closeCurrentModal();
         });
-        this.modalServiceNg2.closeCurrentModal();
     }
 
     private handleEnableAddArtifactImplementation = (newOperation: InterfaceOperationModel): InterfaceOperationModel => {
@@ -379,8 +393,10 @@ export class InterfaceDefinitionComponent {
         return this.interfaces.filter((interfaceData) => interfaceData.operations && interfaceData.operations.length > 0).length > 0;
     }
 
-    onRemoveOperation = (event: Event, operation: OperationModel): void => {
-        event.stopPropagation();
+    onRemoveOperation(operation: OperationModel): void {
+        if (this.readonly) {
+            return;
+        }
 
         const deleteButton: IModalButtonComponent = {
             id: 'deleteButton',