2 * Created by rc2122 on 9/4/2017.
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";
13 const REQUIREMENT = 'Requirement';
14 const CAPABILITY = 'Capability';
17 selector: 'select-requirement-or-capability',
18 templateUrl: './select-requirement-or-capability.component.html',
19 styleUrls: ['./select-reqiurement-or-capability.component.less']
22 export class SelectRequirementOrCapabilityComponent implements OnInit {
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
28 @Input() selectedReqOrCapOption:string; // the selection value chosen by the user (options: requirement / capability )
30 @Input() currentComponent:ComponentModel;
31 @Input() componentInstanceId:string;
33 @Input() selectedReqOrCapModel:RequirementCapabilityModel;
35 @Output() updateSelectedReqOrCap:EventEmitter<RequirementCapabilityModel> = new EventEmitter<RequirementCapabilityModel>();
36 @Output() updateCapabilityProperties:EventEmitter<Array<PropertyModel>> = new EventEmitter<Array<PropertyModel>>();
38 types:Array<string> = [];
41 selectOptions:Array<RadioButtonModel>;
43 requirementsTypes:Array<string> = [];
44 capabilitiesTypes:Array<string> = [];
46 disabledSelectReqOrCapOption: boolean; // If we need to disable the option to choose requirement or capability
47 displayCapReqListFilterByType:RequirementCapabilityModel[];
49 capabilityProperties:InstanceFePropertiesMap;
51 constructor(private componentInstanceServiceNg2:ComponentInstanceServiceNg2,
52 private propertiesUtils:PropertiesUtils) {
53 this.selectOptions = [new RadioButtonModel(REQUIREMENT, REQUIREMENT), new RadioButtonModel(CAPABILITY, CAPABILITY)];
56 private initDefaultReqOrCapSelection = (): void => {
57 if(this.selectedReqOrCapOption){//for second step
58 this.disabledSelectReqOrCapOption = true;
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;
65 this.selectedReqOrCapOption = this.selectOptions[0].value;
66 this.selectedType = (<Requirement>this.selectedReqOrCapModel).capability;
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;
76 this.selectedReqOrCapOption = this.selectedReqOrCapOption || this.selectOptions[1].value;
77 this.types = this.selectedReqOrCapOption == this.selectOptions[0].value ? this.requirementsTypes : this.capabilitiesTypes;
79 if (this.selectedType) {
80 this.initCapReqListFilterByType();
82 this.setDefaultValueType();
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);
96 ngOnChanges(changes:SimpleChanges) {
97 if (changes.selectedReqOrCapModel) {
98 if (this.selectedReqOrCapModel && this.selectedReqOrCapOption === CAPABILITY) {
99 this.setCapabilityProperties();
105 this.initTypesList();
106 this.initDefaultReqOrCapSelection();
107 this.initCapabilityPropertiesTable();
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');
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);
124 this.displayCapReqListFilterByType = allOptionalTypesMap[this.selectedType];
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);
134 private initCapReqListFilterByType = ():void => {
135 if (this.selectedReqOrCapOption === CAPABILITY) {
136 this.fillInDisplayCapReqListFilterByType(this.optionalCapabilitiesMap);
138 this.fillInDisplayCapReqListFilterByType(this.optionalRequirementsMap);
142 private onTypeSelected = ():void => {
143 this.initCapReqListFilterByType();
144 if (this.displayCapReqListFilterByType.indexOf(this.selectedReqOrCapModel) === -1) {
145 this.selectReqOrCapFromList(null);
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();
155 private onSelectRequirementOrCapability = ():void => {
156 this.types = this.selectedReqOrCapOption === REQUIREMENT ? this.requirementsTypes : this.capabilitiesTypes;
157 this.selectReqOrCapFromList(null);
158 this.setDefaultValueType();
161 private selectReqOrCapFromList = (selected:Requirement|Capability):void => {
162 if (this.selectedReqOrCapModel !== selected) {
163 this.selectedReqOrCapModel = selected;
164 this.updateSelectedReqOrCap.emit(selected);
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);
178 this.capabilityProperties = this.propertiesUtils.convertPropertiesMapToFEAndCreateChildren({CAPABILITY : selectedCapability.properties}, false);
179 this.updateCapabilityProperties.emit(selectedCapability.properties);