2 * Created by rc2122 on 9/4/2017.
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";
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>();
37 types:Array<string> = [];
40 selectOptions:Array<RadioButtonModel>;
42 requirementsTypes:Array<string> = [];
43 capabilitiesTypes:Array<string> = [];
45 disabledSelectReqOrCapOption: boolean; // If we need to disable the option to choose requirement or capability
46 displayCapReqListFilterByType:RequirementCapabilityModel[];
48 capabilityProperties:InstanceFePropertiesMap;
49 loadingCapabilityProperties:boolean;
51 private _loadingCapabilityProperties: Array<Capability>;
53 constructor(private componentInstanceServiceNg2:ComponentInstanceServiceNg2,
54 private propertiesUtils:PropertiesUtils) {
55 this.selectOptions = [new RadioButtonModel(REQUIREMENT, REQUIREMENT), new RadioButtonModel(CAPABILITY, CAPABILITY)];
56 this._loadingCapabilityProperties = [];
59 private initDefaultReqOrCapSelection = (): void => {
60 if(this.selectedReqOrCapOption){//for second step
61 this.disabledSelectReqOrCapOption = true;
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;
68 this.selectedReqOrCapOption = this.selectOptions[0].value;
69 this.selectedType = (<Requirement>this.selectedReqOrCapModel).capability;
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;
79 this.selectedReqOrCapOption = this.selectedReqOrCapOption || this.selectOptions[1].value;
80 this.types = this.selectedReqOrCapOption == this.selectOptions[0].value ? this.requirementsTypes : this.capabilitiesTypes;
82 if (this.selectedType) {
83 this.initCapReqListFilterByType();
85 this.setDefaultValueType();
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);
96 this.capabilityProperties = null;
101 ngOnChanges(changes:SimpleChanges) {
102 if (changes.selectedReqOrCapModel) {
103 this.capabilityProperties = null;
104 if (this.selectedReqOrCapModel && this.selectedReqOrCapOption === CAPABILITY) {
105 this.setCapabilityProperties();
111 this.initTypesList();
112 this.initDefaultReqOrCapSelection();
113 this.initCapabilityPropertiesTable();
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');
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);
130 this.displayCapReqListFilterByType = allOptionalTypesMap[this.selectedType];
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);
140 private initCapReqListFilterByType = ():void => {
141 if (this.selectedReqOrCapOption === CAPABILITY) {
142 this.fillInDisplayCapReqListFilterByType(this.optionalCapabilitiesMap);
144 this.fillInDisplayCapReqListFilterByType(this.optionalRequirementsMap);
148 private onTypeSelected = ():void => {
149 this.initCapReqListFilterByType();
150 if (this.displayCapReqListFilterByType.indexOf(this.selectedReqOrCapModel) === -1) {
151 this.selectReqOrCapFromList(null);
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();
161 private onSelectRequirementOrCapability = ():void => {
162 this.types = this.selectedReqOrCapOption === REQUIREMENT ? this.requirementsTypes : this.capabilitiesTypes;
163 this.selectReqOrCapFromList(null);
164 this.setDefaultValueType();
167 private selectReqOrCapFromList = (selected:Requirement|Capability):void => {
168 if (this.selectedReqOrCapModel !== selected) {
169 this.selectedReqOrCapModel = selected;
170 this.updateSelectedReqOrCap.emit(selected);
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;
186 this.initCapabilityPropertiesTable();
188 if (this.selectedReqOrCapModel === selectedCapability) {
189 delete this.loadingCapabilityProperties;
192 this._loadingCapabilityProperties.splice(this._loadingCapabilityProperties.indexOf(selectedCapability), 1);
196 delete this.loadingCapabilityProperties;
197 this.initCapabilityPropertiesTable();