Fix - Declare Output button disabled after saving a default value
[sdc.git] / catalog-ui / src / app / ng2 / pages / attributes-outputs / attributes-outputs.page.component.ts
index 76001a7..c94a345 100644 (file)
@@ -44,6 +44,9 @@ import {
   InstanceBeAttributesMap,
   InstanceFeAttributesMap
 } from "app/models/attributes-outputs/attribute-fe-map";
+import {
+  InstanceBePropertiesMap
+} from "app/models/properties-inputs/property-fe-map";
 import {ModalService} from "../../services/modal.service";
 import {InstanceFeDetails} from "../../../models/instance-fe-details";
 import {HierarchyDisplayOptions} from "../../components/logic/hierarchy-navigtion/hierarchy-display-options";
@@ -57,6 +60,7 @@ import {DerivedFEAttribute} from "../../../models/attributes-outputs/derived-fe-
 import {AttributeBEModel} from "../../../models/attributes-outputs/attribute-be-model";
 import {AttributeCreatorComponent} from "app/ng2/pages/attributes-outputs/attribute-creator/attribute-creator.component";
 import {AttributeRowSelectedEvent} from "app/ng2/components/logic/attributes-table/attributes-table.component";
+import { DeclareInputComponent } from '../properties-assignment/declare-input/declare-input.component';
 
 const SERVICE_SELF_TITLE = "SELF";
 
@@ -109,22 +113,24 @@ export class AttributesOutputsComponent {
   @ViewChild('attributeOutputTabs') attributeOutputTabs: Tabs;
 
 
-  constructor(private attributesService: AttributesService,
-              private hierarchyNavService: HierarchyNavService,
-              private attributesUtils: AttributesUtils,
-              private outputsUtils: OutputsUtils,
-              private componentServiceNg2: ComponentServiceNg2,
-              private componentInstanceServiceNg2: ComponentInstanceServiceNg2,
-              @Inject("$stateParams") _stateParams,
-              @Inject("$scope") private $scope: ng.IScope,
-              @Inject("$state") private $state: ng.ui.IStateService,
-              @Inject("Notification") private Notification: any,
-              private componentModeService: ComponentModeService,
-              private EventListenerService: EventListenerService,
-              private ModalServiceSdcUI: SdcUiServices.ModalService,
-              private ModalService: ModalService,
-              private keysPipe: KeysPipe,
-              private topologyTemplateService: TopologyTemplateService) {
+  constructor(
+    private attributesService: AttributesService,
+    private hierarchyNavService: HierarchyNavService,
+    private attributesUtils: AttributesUtils,
+    private outputsUtils: OutputsUtils,
+    private componentServiceNg2: ComponentServiceNg2,
+    private componentInstanceServiceNg2: ComponentInstanceServiceNg2,
+    @Inject("$stateParams") _stateParams,
+    @Inject("$scope") private $scope: ng.IScope,
+    @Inject("$state") private $state: ng.ui.IStateService,
+    @Inject("Notification") private Notification: any,
+    private componentModeService: ComponentModeService,
+    private EventListenerService: EventListenerService,
+    private ModalServiceSdcUI: SdcUiServices.ModalService,
+    private ModalService: ModalService,
+    private keysPipe: KeysPipe,
+    private topologyTemplateService: TopologyTemplateService
+  ) {
 
     this.instanceFeAttributesMap = new InstanceFeAttributesMap();
     /* This is the way you can access the component data, please do not use any data except metadata, all other data should be received from the new api calls on the first time
@@ -234,7 +240,7 @@ export class AttributesOutputsComponent {
     .subscribe((response) => {
       this.serviceBeAttributesMap = new InstanceBeAttributesMap();
       this.serviceBeAttributesMap[this.component.uniqueId] = response;
-      this.processInstanceAttributesResponse(this.serviceBeAttributesMap, false);
+      this.processInstanceAttributesResponse(this.serviceBeAttributesMap, false, null);
     }, (error) => {
       this.Notification.error({
         message: 'Failed to get Service Attribute:' + error,
@@ -259,14 +265,15 @@ export class AttributesOutputsComponent {
   changeSelectedInstance = (instance: ComponentInstance) => {
     this.selectedInstanceData = instance;
     this.loadingAttributes = true;
+    this.instanceFeAttributesMap = undefined;
     if (instance instanceof ComponentInstance) {
-      let instanceBeAttributesMap: InstanceBeAttributesMap = new InstanceBeAttributesMap();
+      let instanceBeAttributesMap: InstanceBeAttributesMap | InstanceBePropertiesMap = new InstanceBeAttributesMap();
       if (this.isOutput(instance.originType)) {
         this.componentInstanceServiceNg2
         .getComponentInstanceOutputs(this.component, instance)
         .subscribe(response => {
           instanceBeAttributesMap[instance.uniqueId] = response;
-          this.processInstanceAttributesResponse(instanceBeAttributesMap, true);
+          this.processInstanceAttributesResponse(instanceBeAttributesMap, true, instance.uniqueId);
         }, error => {
           this.Notification.error({
             message: 'Failed to change Selected Instance:' + error,
@@ -281,8 +288,23 @@ export class AttributesOutputsComponent {
         this.componentInstanceServiceNg2
         .getComponentInstanceAttributes(this.component, instance.uniqueId)
         .subscribe(response => {
+          instanceBeAttributesMap = new InstanceBeAttributesMap();
           instanceBeAttributesMap[instance.uniqueId] = response;
-          this.processInstanceAttributesResponse(instanceBeAttributesMap, false);
+          this.processInstanceAttributesResponse(instanceBeAttributesMap, false, instance.uniqueId);
+        }, error => {
+          this.Notification.error({
+            message: 'Failed to change Selected Instance:' + error,
+            title: 'Failure'
+          });
+        }, () => {
+          this.loadingAttributes = false;
+        });
+        this.componentInstanceServiceNg2
+        .getComponentInstanceProperties(this.component, instance.uniqueId)
+        .subscribe(response => {
+          instanceBeAttributesMap = new InstanceBePropertiesMap();
+          instanceBeAttributesMap[instance.uniqueId] = response;
+          this.processInstanceAttributesResponse(instanceBeAttributesMap, false, instance.uniqueId);
         }, error => {
           this.Notification.error({
             message: 'Failed to change Selected Instance:' + error,
@@ -292,7 +314,6 @@ export class AttributesOutputsComponent {
           this.loadingAttributes = false;
         });
       }
-
       this.resourceIsReadonly = (instance.componentName === "vnfConfiguration");
     } else {
       this.loadingAttributes = false;
@@ -306,8 +327,20 @@ export class AttributesOutputsComponent {
   /**
    * Entry point handling response from server
    */
-  processInstanceAttributesResponse = (instanceBeAttributesMap: InstanceBeAttributesMap, originTypeIsVF: boolean) => {
-    this.instanceFeAttributesMap = this.attributesUtils.convertAttributesMapToFEAndCreateChildren(instanceBeAttributesMap, originTypeIsVF, this.outputs); //create flattened children, disable declared attribs, and init values
+  processInstanceAttributesResponse = (instanceBeAttributesMap: InstanceBePropertiesMap | InstanceBeAttributesMap, originTypeIsVF: boolean, instanceId: string) => {
+    let attributesMap = this.attributesUtils.convertAttributesMapToFEAndCreateChildren(instanceBeAttributesMap, originTypeIsVF, this.outputs); //create flattened children, disable declared attribs, and init values
+    if (!this.instanceFeAttributesMap) {
+      this.instanceFeAttributesMap = attributesMap;
+    } else if (instanceId) {
+      let toAdd: AttributeFEModel[];
+      if (instanceBeAttributesMap instanceof InstanceBeAttributesMap) {
+        toAdd = this.instanceFeAttributesMap[instanceId].filter(currentAtr => !attributesMap[instanceId].some(newAtr => newAtr.name === currentAtr.name))
+        this.instanceFeAttributesMap[instanceId] = attributesMap[instanceId];
+      } else {
+        toAdd = attributesMap[instanceId].filter(currentAtr => !this.instanceFeAttributesMap[instanceId].some(newAtr => newAtr.name === currentAtr.name))
+      }
+      this.instanceFeAttributesMap[instanceId] = this.instanceFeAttributesMap[instanceId].concat(toAdd);
+    }
     this.checkedAttributesCount = 0;
   };
 
@@ -400,9 +433,62 @@ export class AttributesOutputsComponent {
     this.searchQuery = '';
   };
 
+  declareOutput = (): void => {
+    if (this.checkedAttributesCount == 1) {
+        this.openAddOutputNameModal();
+    } else if (this.checkedAttributesCount > 1) {
+        this.declareAttributes();
+    }
+  }
+
+  private openAddOutputNameModal = (): void => {
+    const modalTitle = 'Enter name of the ouput to be created';
+    const modalButtons = [];
+    const modal = this.ModalService.createCustomModal(new ModalModel(
+        'sm',
+        modalTitle,
+        null,
+        modalButtons,
+        null /* type */
+    ));
+    modalButtons.push(new ButtonModel('Save', 'blue',
+        () => {
+            const outputName: string = modal.instance.dynamicContent.instance.inputNameForm.value;
+            if (outputName) {
+                this.declareAttributes(outputName);
+            } else {
+                this.Notification.warning({
+                    message: 'Failed to set input name',
+                    title: 'Warning'
+                });
+            }
+            this.ModalService.closeCurrentModal();
+        }
+    ));
+    modalButtons.push(new ButtonModel('Cancel', 'outline grey', () => {
+        this.ModalService.closeCurrentModal();
+    }));
+    this.ModalService.addDynamicContentToModal(modal, DeclareInputComponent, {defaultInputName: this.generateDefaultOutputName()});
+    modal.instance.open();
+  }
+
+  generateDefaultOutputName = (): string => {
+    let defaultInputName: string;
+    let instancesIds = this.keysPipe.transform(this.instanceFeAttributesMap, []);
+    angular.forEach(instancesIds, (instanceId: string) => {
+      const selectedOutput : AttributeBEModel = this.attributesService.getCheckedAttributes(this.instanceFeAttributesMap[instanceId])[0];
+        let selectedInstanceData: any = this.instances.find(instance => instance.uniqueId == instanceId);
+        defaultInputName = selectedOutput.name;
+        if (selectedInstanceData.invariantName) {
+          defaultInputName = selectedInstanceData.invariantName+'_'+selectedOutput.name;
+        }
+    });
+    return defaultInputName;
+  }
 
   /*** DECLARE ATTRIBUTES/OUTPUTS ***/
-  declareAttributes = (): void => {
+  declareAttributes = (outputName?: string): void => {
+    this.loadingAttributes = true;
     let selectedComponentInstancesAttributes: InstanceBeAttributesMap = new InstanceBeAttributesMap();
     let selectedComponentInstancesOutputs: InstanceBeAttributesMap = new InstanceBeAttributesMap();
     let instancesIds = this.keysPipe.transform(this.instanceFeAttributesMap, []);
@@ -413,6 +499,9 @@ export class AttributesOutputsComponent {
         if (!this.isOutput(selectedInstanceData.originType)) {
           // convert Attribute FE model -> Attribute BE model, extract only checked
           selectedComponentInstancesAttributes[instanceId] = this.attributesService.getCheckedAttributes(this.instanceFeAttributesMap[instanceId]);
+          if (outputName) {
+            selectedComponentInstancesAttributes[instanceId][0].outputName = outputName;
+          }
         } else {
           selectedComponentInstancesOutputs[instanceId] = this.attributesService.getCheckedAttributes(this.instanceFeAttributesMap[instanceId]);
         }
@@ -431,7 +520,8 @@ export class AttributesOutputsComponent {
         this.outputs.push(newOutput);
         this.updateAttributeValueAfterDeclare(newOutput);
       });
-    });
+      this.loadingAttributes = false;
+    }, error => {this.loadingAttributes = false;}); //ignore error
   };
 
   saveChangedData = (): Promise<(AttributeBEModel | OutputBEModel)[]> => {
@@ -565,6 +655,9 @@ export class AttributesOutputsComponent {
           if (this.isAttributesTabSelected) {
             this.checkedAttributesCount = 0;
           }
+          this.instanceFeAttributesMap[this.selectedInstanceData.uniqueId].forEach(prop => prop.isSelected = false);
+          this.hasChangedData = false;
+          this.isValidChangedData = false;
         },
         () => {
           this.Notification.error({