Sort drop down lists in VFC requirements and capabilities
[sdc.git] / catalog-ui / src / app / ng2 / pages / workspace / req-and-capabilities / req-and-capabilities.service.ts
1 import { Injectable } from "@angular/core";
2 import { TopologyTemplateService } from "../../../services/component-services/topology-template.service";
3 import { Store } from "@ngxs/store";
4 import { SdcUiServices } from "onap-ui-angular";
5 import { CapabilityTypeModel } from "../../../../models/capability-types";
6 import { RelationshipTypeModel } from "../../../../models/relationship-types";
7 import { NodeTypeModel } from "../../../../models/node-types";
8 import { WorkspaceService } from "../workspace.service";
9 import { ToscaTypesServiceNg2 } from "../../../services/tosca-types.service";
10
11
12
13 @Injectable()
14 export class ReqAndCapabilitiesService {
15
16     private capabilityTypesList: CapabilityTypeModel[];
17     private relationshipTypesList: RelationshipTypeModel[];
18     private nodeTypesList: NodeTypeModel[];
19
20     readonly INPUTS_FOR_REQUIREMENTS: string = 'INPUTS_FOR_REQUIREMENTS';
21     readonly INPUTS_FOR_CAPABILITIES: string = 'INPUTS_FOR_CAPABILITIES';
22
23     constructor(
24         private workspaceService: WorkspaceService,
25         private modalService: SdcUiServices.ModalService,
26         private loaderService: SdcUiServices.LoaderService,
27         private topologyTemplateService: TopologyTemplateService,
28         private store: Store,
29         private toscaTypesServiceNg2: ToscaTypesServiceNg2){}
30
31     public isViewOnly = (): boolean => {
32         return this.store.selectSnapshot((state) => state.workspace.isViewOnly);
33     }
34
35     public isDesigner = (): boolean => {
36         return this.store.selectSnapshot((state) => state.workspace.isDesigner);
37     }
38
39     public async initInputs(initInputsFor: string) {
40
41         // -- COMMON for both --
42         this.capabilityTypesList = [];
43         let capabilityTypesResult = await this.toscaTypesServiceNg2.fetchCapabilityTypes(this.workspaceService.metadata.model);
44         Object.keys(capabilityTypesResult).forEach(key => {this.capabilityTypesList.push(capabilityTypesResult[key])});
45         this.capabilityTypesList.sort((capabilityType1, capabilityType2) => capabilityType1.toscaPresentation.type.localeCompare(capabilityType2.toscaPresentation.type));
46
47         if (initInputsFor === 'INPUTS_FOR_REQUIREMENTS') {
48             this.relationshipTypesList = [];
49             let relationshipTypesResult = await this.toscaTypesServiceNg2.fetchRelationshipTypes(this.workspaceService.metadata.model);
50             Object.keys(relationshipTypesResult).forEach(key => {this.relationshipTypesList.push(relationshipTypesResult[key])});
51             this.relationshipTypesList.sort((relationship1,relationship2) => relationship1.toscaPresentation.type.localeCompare(relationship2.toscaPresentation.type));
52
53             this.nodeTypesList = [];
54             let nodeTypesResult = await this.toscaTypesServiceNg2.fetchNodeTypes(this.workspaceService.metadata.model);
55             Object.keys(nodeTypesResult).forEach(key => {this.nodeTypesList.push(nodeTypesResult[key])});
56             this.nodeTypesList.sort((node1,node2) => node1.componentMetadataDefinition.componentMetadataDataDefinition.toscaResourceName.localeCompare(node2.componentMetadataDefinition.componentMetadataDataDefinition.toscaResourceName));
57         }
58     }
59
60     getCapabilityTypesList() {
61         return this.capabilityTypesList;
62     }
63
64     getRelationsShipeTypeList() {
65         return this.relationshipTypesList;
66     }
67
68     getNodeTypesList() {
69         return this.nodeTypesList;
70     }
71 }