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";
12 import { WorkspaceService } from "app/ng2/pages/workspace/workspace.service";
14 const REQUIREMENT = 'Requirement';
15 const CAPABILITY = 'Capability';
18 selector: 'select-requirement-or-capability',
19 templateUrl: './select-requirement-or-capability.component.html',
20 styleUrls: ['./select-reqiurement-or-capability.component.less']
23 export class SelectRequirementOrCapabilityComponent implements OnInit {
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>();
33 types:Array<string> = [];
36 selectOptions:Array<RadioButtonModel>;
38 requirementsTypes:Array<string> = [];
39 capabilitiesTypes:Array<string> = [];
41 disabledSelectReqOrCapOption: boolean; // If we need to disable the option to choose requirement or capability
42 displayCapReqListFilterByType:RequirementCapabilityModel[];
44 capabilityProperties:InstanceFePropertiesMap;
45 loadingCapabilityProperties:boolean;
47 private _loadingCapabilityProperties: Array<Capability>;
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 = [];
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 && selectedCapability.properties.length) {
91 this.capabilityProperties = this.propertiesUtils.convertPropertiesMapToFEAndCreateChildren({ CAPABILITY : selectedCapability.properties}, false);
93 this.capabilityProperties = null;
98 ngOnChanges(changes:SimpleChanges) {
99 if (changes.selectedReqOrCapModel) {
100 this.capabilityProperties = null;
101 if (this.selectedReqOrCapModel && this.selectedReqOrCapOption === CAPABILITY) {
102 this.setCapabilityProperties();
108 this.initTypesList();
109 this.initDefaultReqOrCapSelection();
110 this.initCapabilityPropertiesTable();
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');
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);
127 this.displayCapReqListFilterByType = allOptionalTypesMap[this.selectedType];
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);
137 private initCapReqListFilterByType = ():void => {
138 if (this.selectedReqOrCapOption === CAPABILITY) {
139 this.fillInDisplayCapReqListFilterByType(this.optionalCapabilitiesMap);
141 this.fillInDisplayCapReqListFilterByType(this.optionalRequirementsMap);
145 private onTypeSelected = ():void => {
146 this.initCapReqListFilterByType();
147 if (this.displayCapReqListFilterByType.indexOf(this.selectedReqOrCapModel) === -1) {
148 this.selectReqOrCapFromList(null);
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();
158 private onSelectRequirementOrCapability = ():void => {
159 this.types = this.selectedReqOrCapOption === REQUIREMENT ? this.requirementsTypes : this.capabilitiesTypes;
160 this.selectReqOrCapFromList(null);
161 this.setDefaultValueType();
164 private selectReqOrCapFromList = (selected:Requirement|Capability):void => {
165 if (this.selectedReqOrCapModel !== selected) {
166 this.selectedReqOrCapModel = selected;
167 this.updateSelectedReqOrCap.emit(selected);
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;
182 this.initCapabilityPropertiesTable();
184 if (this.selectedReqOrCapModel === selectedCapability) {
185 delete this.loadingCapabilityProperties;
188 this._loadingCapabilityProperties.splice(this._loadingCapabilityProperties.indexOf(selectedCapability), 1);
192 delete this.loadingCapabilityProperties;
193 this.initCapabilityPropertiesTable();