ab67dc18509440c42bb700af1a0ac7248f8718e4
[sdc.git] /
1 /**
2  * Created by rc2122 on 9/4/2017.
3  */
4 import {Component, EventEmitter, Input, OnInit, Output, SimpleChanges} from '@angular/core';
5 import {RadioButtonModel, Match, PropertyModel, InstanceFePropertiesMap, Component as ComponentModel} from "app/models";
6 import {Dictionary} from "lodash";
7 import {DropdownValue} from "../../ui/form-components/dropdown/ui-element-dropdown.component";
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     @Output() updateCapabilityProperties:EventEmitter<Array<PropertyModel>> = new EventEmitter<Array<PropertyModel>>();
37
38     types:Array<string> = [];
39     selectedType:string;
40
41     selectOptions:Array<RadioButtonModel>;
42
43     requirementsTypes:Array<string> = [];
44     capabilitiesTypes:Array<string> = [];
45
46     disabledSelectReqOrCapOption: boolean; // If we need to disable the option to choose requirement or capability
47     displayCapReqListFilterByType:RequirementCapabilityModel[];
48
49     capabilityProperties:InstanceFePropertiesMap;
50
51     constructor(private componentInstanceServiceNg2:ComponentInstanceServiceNg2,
52                 private propertiesUtils:PropertiesUtils) {
53         this.selectOptions = [new RadioButtonModel(REQUIREMENT, REQUIREMENT), new RadioButtonModel(CAPABILITY, CAPABILITY)];
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){
91                 this.capabilityProperties = this.propertiesUtils.convertPropertiesMapToFEAndCreateChildren({ CAPABILITY : selectedCapability.properties}, false);
92             }
93         }
94     }
95
96     ngOnChanges(changes:SimpleChanges) {
97         if (changes.selectedReqOrCapModel) {
98             if (this.selectedReqOrCapModel && this.selectedReqOrCapOption === CAPABILITY) {
99                 this.setCapabilityProperties();
100             }
101         }
102     }
103
104     ngOnInit() {
105         this.initTypesList();
106         this.initDefaultReqOrCapSelection();
107         this.initCapabilityPropertiesTable();
108     }
109
110     private initTypesList = ():void => {
111         this.requirementsTypes = _.keys(this.optionalRequirementsMap);
112         this.requirementsTypes.unshift('All');
113         this.capabilitiesTypes = _.keys(this.optionalCapabilitiesMap);
114         this.capabilitiesTypes.unshift('All');
115     }
116
117     private fillInDisplayCapReqListFilterByType = (allOptionalTypesMap:Dictionary<RequirementCapabilityModel[]>):void => {
118         if(this.selectedType === 'All'){
119             this.displayCapReqListFilterByType = [];
120             _.map(allOptionalTypesMap,(reqOrCapArray:RequirementCapabilityModel[])=>{
121                 this.displayCapReqListFilterByType = this.displayCapReqListFilterByType.concat(reqOrCapArray);
122             })
123         }else{
124             this.displayCapReqListFilterByType = allOptionalTypesMap[this.selectedType];
125         }
126
127         // automatically select a *single* requirement or capability:
128         if (this.displayCapReqListFilterByType.length === 1) {
129             const selectedReqCap:RequirementCapabilityModel = this.displayCapReqListFilterByType[0];
130             this.selectReqOrCapFromList((this.selectedType === CAPABILITY) ? <Capability>selectedReqCap : <Requirement>selectedReqCap);
131         }
132     }
133     
134     private initCapReqListFilterByType = ():void => {
135         if (this.selectedReqOrCapOption === CAPABILITY) {
136             this.fillInDisplayCapReqListFilterByType(this.optionalCapabilitiesMap);
137         } else {
138             this.fillInDisplayCapReqListFilterByType(this.optionalRequirementsMap);
139         }
140     }
141
142     private onTypeSelected = ():void => {
143         this.initCapReqListFilterByType();
144         if (this.displayCapReqListFilterByType.indexOf(this.selectedReqOrCapModel) === -1) {
145             this.selectReqOrCapFromList(null);
146         }
147     }
148
149     private setDefaultValueType = ():void =>{
150         // automatically select a *single* type from the list:
151         this.selectedType = (this.types.length === 2) ? this.types[1] : this.types[0];
152         this.initCapReqListFilterByType();
153     }
154     
155     private onSelectRequirementOrCapability = ():void => {
156         this.types = this.selectedReqOrCapOption === REQUIREMENT ? this.requirementsTypes : this.capabilitiesTypes;
157         this.selectReqOrCapFromList(null);
158         this.setDefaultValueType();
159     }
160
161     private selectReqOrCapFromList = (selected:Requirement|Capability):void => {
162         if (this.selectedReqOrCapModel !== selected) {
163             this.selectedReqOrCapModel = selected;
164             this.updateSelectedReqOrCap.emit(selected);
165         }
166     }
167
168
169     private setCapabilityProperties = ():void => {
170         let selectedCapability = <Capability>this.selectedReqOrCapModel;
171         if (selectedCapability.properties === undefined) {
172             this.componentInstanceServiceNg2.getInstanceCapabilityProperties(this.currentComponent, this.componentInstanceId, selectedCapability.type, selectedCapability.name)
173                 .subscribe((response:Array<PropertyModel>) => {
174                     this.capabilityProperties = (response && response.length) ? this.propertiesUtils.convertPropertiesMapToFEAndCreateChildren({CAPABILITY : response}, false) : null;
175                     this.updateCapabilityProperties.emit(response);
176                 }, error => {});
177         }else{
178             this.capabilityProperties = this.propertiesUtils.convertPropertiesMapToFEAndCreateChildren({CAPABILITY : selectedCapability.properties}, false);
179             this.updateCapabilityProperties.emit(selectedCapability.properties);
180         }
181     }
182 }