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