Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / select-requirement-or-capability / select-requirement-or-capability.component.ts
1 /**
2  * Created by rc2122 on 9/4/2017.
3  */
4 import * as _ from "lodash";
5 import {Component, EventEmitter, Input, OnInit, Output, SimpleChanges} from '@angular/core';
6 import {RadioButtonModel, PropertyModel, InstanceFePropertiesMap, Component as ComponentModel} from "app/models";
7 import {Dictionary} from "lodash";
8 import {ComponentInstanceServiceNg2} from "../../../services/component-instance-services/component-instance.service";
9 import {PropertiesUtils} from "app/ng2/pages/properties-assignment/services/properties.utils";
10 import {Requirement} from "../../../../models/requirement";
11 import {Capability, RequirementCapabilityModel} from "../../../../models/capability";
12 import { WorkspaceService } from "app/ng2/pages/workspace/workspace.service";
13
14 const REQUIREMENT = 'Requirement';
15 const CAPABILITY = 'Capability';
16
17 @Component({
18     selector: 'select-requirement-or-capability',
19     templateUrl: './select-requirement-or-capability.component.html',
20     styleUrls: ['./select-reqiurement-or-capability.component.less']
21 })
22
23 export class SelectRequirementOrCapabilityComponent implements OnInit {
24
25
26     @Input() optionalRequirementsMap:Dictionary<Requirement[]>; //optional requirement map - key is type, value is array of requirements
27     @Input() optionalCapabilitiesMap:Dictionary<Capability[]>; //optional capabilities map - key is type, value is array of capabilities
28     @Input() selectedReqOrCapOption:string; // the selection value chosen by the user (options: requirement / capability )
29     @Input() componentInstanceId:string;
30     @Input() selectedReqOrCapModel:RequirementCapabilityModel;
31     @Output() updateSelectedReqOrCap:EventEmitter<RequirementCapabilityModel> = new EventEmitter<RequirementCapabilityModel>();
32
33     types:Array<string> = [];
34     selectedType:string;
35
36     selectOptions:Array<RadioButtonModel>;
37
38     requirementsTypes:Array<string> = [];
39     capabilitiesTypes:Array<string> = [];
40
41     disabledSelectReqOrCapOption: boolean; // If we need to disable the option to choose requirement or capability
42     displayCapReqListFilterByType:RequirementCapabilityModel[];
43
44     capabilityProperties:InstanceFePropertiesMap;
45     loadingCapabilityProperties:boolean;
46
47     private _loadingCapabilityProperties: Array<Capability>;
48
49     constructor(private componentInstanceServiceNg2:ComponentInstanceServiceNg2,
50                 private propertiesUtils:PropertiesUtils,
51                 private workspaceService: WorkspaceService) {
52         this.selectOptions = [new RadioButtonModel(REQUIREMENT, REQUIREMENT), new RadioButtonModel(CAPABILITY, CAPABILITY)];
53         this._loadingCapabilityProperties = [];
54     }
55
56     private initDefaultReqOrCapSelection = (): void => {
57         if(this.selectedReqOrCapOption){//for second step
58             this.disabledSelectReqOrCapOption = true;
59         }
60         if (this.selectedReqOrCapModel) {//init when there is selected req or cap
61             if (this.selectedReqOrCapModel instanceof Capability) {
62                 this.selectedReqOrCapOption = this.selectOptions[1].value;
63                 this.selectedType = this.selectedReqOrCapModel.type;
64             } else {
65                 this.selectedReqOrCapOption = this.selectOptions[0].value;
66                 this.selectedType = (<Requirement>this.selectedReqOrCapModel).capability;
67             }
68         }
69         if(Object.keys(this.optionalCapabilitiesMap).length === 0) { // If instance don't have capabilities
70             this.disabledSelectReqOrCapOption = true;
71             this.selectedReqOrCapOption = this.selectOptions[0].value;
72         } else if(Object.keys(this.optionalRequirementsMap).length === 0) { // If instance don't have requirements
73             this.disabledSelectReqOrCapOption = true;
74             this.selectedReqOrCapOption = this.selectOptions[1].value;
75         }
76         this.selectedReqOrCapOption = this.selectedReqOrCapOption || this.selectOptions[1].value;
77         this.types = this.selectedReqOrCapOption == this.selectOptions[0].value ? this.requirementsTypes : this.capabilitiesTypes;
78         setTimeout(() => {
79             if (this.selectedType) {
80                 this.initCapReqListFilterByType();
81             } else {
82                 this.setDefaultValueType();
83             }
84         });
85     }
86
87     initCapabilityPropertiesTable = ():void => {
88         if(this.selectedReqOrCapModel instanceof Capability ) {
89             let selectedCapability = <Capability>this.selectedReqOrCapModel;
90             if (selectedCapability.properties && selectedCapability.properties.length) {
91                 this.capabilityProperties = this.propertiesUtils.convertPropertiesMapToFEAndCreateChildren({ CAPABILITY : selectedCapability.properties}, false);
92             } else {
93                 this.capabilityProperties = null;
94             }
95         }
96     }
97
98     ngOnChanges(changes:SimpleChanges) {
99         if (changes.selectedReqOrCapModel) {
100             this.capabilityProperties = null;
101             if (this.selectedReqOrCapModel && this.selectedReqOrCapOption === CAPABILITY) {
102                 this.setCapabilityProperties();
103             }
104         }
105     }
106
107     ngOnInit() {
108         this.initTypesList();
109         this.initDefaultReqOrCapSelection();
110         this.initCapabilityPropertiesTable();
111     }
112
113     private initTypesList = ():void => {
114         this.requirementsTypes = _.keys(this.optionalRequirementsMap);
115         this.requirementsTypes.unshift('All');
116         this.capabilitiesTypes = _.keys(this.optionalCapabilitiesMap);
117         this.capabilitiesTypes.unshift('All');
118     }
119
120     private fillInDisplayCapReqListFilterByType = (allOptionalTypesMap:Dictionary<RequirementCapabilityModel[]>):void => {
121         if(this.selectedType === 'All'){
122             this.displayCapReqListFilterByType = [];
123             _.map(allOptionalTypesMap,(reqOrCapArray:RequirementCapabilityModel[])=>{
124                 this.displayCapReqListFilterByType = this.displayCapReqListFilterByType.concat(reqOrCapArray);
125             })
126         }else{
127             this.displayCapReqListFilterByType = allOptionalTypesMap[this.selectedType];
128         }
129
130         // automatically select a *single* requirement or capability:
131         if (this.displayCapReqListFilterByType.length === 1) {
132             const selectedReqCap:RequirementCapabilityModel = this.displayCapReqListFilterByType[0];
133             this.selectReqOrCapFromList((this.selectedType === CAPABILITY) ? <Capability>selectedReqCap : <Requirement>selectedReqCap);
134         }
135     }
136     
137     private initCapReqListFilterByType = ():void => {
138         if (this.selectedReqOrCapOption === CAPABILITY) {
139             this.fillInDisplayCapReqListFilterByType(this.optionalCapabilitiesMap);
140         } else {
141             this.fillInDisplayCapReqListFilterByType(this.optionalRequirementsMap);
142         }
143     }
144
145     private onTypeSelected = ():void => {
146         this.initCapReqListFilterByType();
147         if (this.displayCapReqListFilterByType.indexOf(this.selectedReqOrCapModel) === -1) {
148             this.selectReqOrCapFromList(null);
149         }
150     }
151
152     private setDefaultValueType = ():void =>{
153         // automatically select a *single* type from the list:
154         this.selectedType = (this.types.length === 2) ? this.types[1] : this.types[0];
155         this.initCapReqListFilterByType();
156     }
157     
158     private onSelectRequirementOrCapability = ():void => {
159         this.types = this.selectedReqOrCapOption === REQUIREMENT ? this.requirementsTypes : this.capabilitiesTypes;
160         this.selectReqOrCapFromList(null);
161         this.setDefaultValueType();
162     }
163
164     private selectReqOrCapFromList = (selected:Requirement|Capability):void => {
165         if (this.selectedReqOrCapModel !== selected) {
166             this.selectedReqOrCapModel = selected;
167             this.updateSelectedReqOrCap.emit(selected);
168         }
169     }
170
171     private setCapabilityProperties = ():void => {
172         let selectedCapability = <Capability>this.selectedReqOrCapModel;
173         if (!selectedCapability.properties) {
174             this.loadingCapabilityProperties = true;
175             if (this._loadingCapabilityProperties.indexOf(selectedCapability) == -1) {
176                 this._loadingCapabilityProperties.push(selectedCapability);
177                 this.componentInstanceServiceNg2.getInstanceCapabilityProperties(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, this.componentInstanceId, selectedCapability)
178                     .subscribe((response: Array<PropertyModel>) => {
179                         if (this.selectedReqOrCapModel === selectedCapability) {
180                             delete this.loadingCapabilityProperties;
181                         }
182                         this.initCapabilityPropertiesTable();
183                     }, (error) => {
184                         if (this.selectedReqOrCapModel === selectedCapability) {
185                             delete this.loadingCapabilityProperties;
186                         }
187                     }, () => {
188                         this._loadingCapabilityProperties.splice(this._loadingCapabilityProperties.indexOf(selectedCapability), 1);
189                     });
190             }
191         } else {
192             delete this.loadingCapabilityProperties;
193             this.initCapabilityPropertiesTable();
194         }
195     }
196 }