Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / panel / panel-tabs / service-dependencies-tab / service-dependencies-tab.component.ts
1
2 import { Component, Input } from '@angular/core';
3 import { Store } from '@ngxs/store';
4 import {
5     CapabilitiesGroup,
6     Capability,
7     Component as TopologyTemplate,
8     ComponentInstance,
9     FullComponentInstance,
10     InputBEModel,
11     InputsGroup,
12     InterfaceModel,
13     PropertiesGroup,
14     PropertyBEModel,
15 } from 'app/models';
16 import { DEPENDENCY_EVENTS } from 'app/utils/constants';
17 import { ComponentMetadata } from '../../../../../../models/component-metadata';
18 import { ServiceInstanceObject } from '../../../../../../models/service-instance-properties-and-interfaces';
19 import { EventListenerService } from '../../../../../../services/event-listener-service';
20 import { ConstraintObject } from '../../../../../components/logic/service-dependencies/service-dependencies.component';
21 import { TopologyTemplateService } from '../../../../../services/component-services/topology-template.service';
22 import { ComponentGenericResponse } from '../../../../../services/responses/component-generic-response';
23 import { WorkspaceService } from '../../../../workspace/workspace.service';
24 import { SelectedComponentType } from '../../../common/store/graph.actions';
25 import { CompositionService } from '../../../composition.service';
26
27 @Component({
28     selector: 'service-dependencies-tab',
29     templateUrl: 'service-dependencies-tab.component.html',
30     styleUrls: ['service-dependencies-tab.component.less']
31 })
32 export class ServiceDependenciesTabComponent {
33     isComponentInstanceSelected: boolean;
34
35     selectedInstanceSiblings: ServiceInstanceObject[];
36     componentInstancesConstraints: any[];
37     selectedInstanceConstraints: ConstraintObject[];
38     selectedInstanceProperties: PropertyBEModel[];
39     componentInstanceProperties: PropertiesGroup;
40     metaData: ComponentMetadata;
41
42     @Input() isViewOnly: boolean;
43     @Input() componentType: SelectedComponentType;
44     @Input() component: FullComponentInstance | TopologyTemplate;
45     @Input() input: any;
46
47     constructor(private store: Store,
48                 private topologyTemplateService: TopologyTemplateService,
49                 private workspaceService: WorkspaceService,
50                 private compositionService: CompositionService,
51                 private eventListenerService: EventListenerService) {
52     }
53
54     ngOnInit() {
55         this.metaData = this.workspaceService.metadata;
56         this.isComponentInstanceSelected = this.componentType === SelectedComponentType.COMPONENT_INSTANCE;
57         this.initInstancesWithProperties();
58         this.loadConstraints();
59         this.initInstancesWithProperties();
60     }
61
62     public loadConstraints = (): void => {
63         this.topologyTemplateService.getServiceFilterConstraints(this.metaData.componentType, this.metaData.uniqueId).subscribe((response) => {
64             this.componentInstancesConstraints = response.nodeFilterData;
65         });
66     }
67
68     public notifyDependencyEventsObserver = (isChecked: boolean): void => {
69         this.eventListenerService.notifyObservers(DEPENDENCY_EVENTS.ON_DEPENDENCY_CHANGE, isChecked);
70     }
71
72     public updateSelectedInstanceConstraints = (constraintsList:Array<ConstraintObject>):void => {
73         this.componentInstancesConstraints[this.component.uniqueId].properties = constraintsList;
74         this.selectedInstanceConstraints = this.componentInstancesConstraints[this.component.uniqueId].properties;
75     }
76
77     private initInstancesWithProperties = (): void => {
78         this.topologyTemplateService.getComponentInstanceProperties(this.metaData.componentType, this.metaData.uniqueId).subscribe((genericResponse: ComponentGenericResponse) => {
79             this.componentInstanceProperties = genericResponse.componentInstancesProperties;
80             this.updateInstanceAttributes();
81         });
82     }
83
84     private updateInstanceAttributes = (): void => {
85         if (this.isComponentInstanceSelected && this.componentInstanceProperties) {
86             const instancesMappedList = this.compositionService.componentInstances.map((coInstance) => new ServiceInstanceObject({
87                 id: coInstance.uniqueId,
88                 name: coInstance.name,
89                 properties: this.componentInstanceProperties[coInstance.uniqueId] || []
90             }));
91             this.selectedInstanceProperties = this.componentInstanceProperties[this.component.uniqueId];
92             this.selectedInstanceSiblings = instancesMappedList.filter((coInstance) => coInstance.id !== this.component.uniqueId);
93         }
94     }
95 }