2 * Copyright (c) 2017 ZTE Corporation.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * ZTE - initial API and implementation and/or initial documentation
13 import {Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges} from "@angular/core";
14 import {PlanTreeviewItem} from "../../model/plan-treeview-item";
15 import {ValueSource} from "../../model/value-source.enum";
16 import {Parameter} from "../../model/workflow/parameter";
17 import {ToscaService} from "../../services/tosca.service";
18 import {ValueType} from "../../model/value-type.enum";
21 * this component contains in property component if the corresponding node has parameter properties
22 * eg. task node have input and output params, start event node has input param
25 selector: 'wfm-parameter',
26 styleUrls: ['./parameter.component.css'],
27 templateUrl: 'parameter.component.html',
29 export class ParameterComponent implements OnInit {
30 @Input() public param: Parameter;
31 @Input() public valueSource: ValueSource[] = [];
32 @Input() public showLabel: boolean;
33 @Input() public showValue: boolean;
34 @Input() public canEditName: boolean;
35 @Input() public canEditValue: boolean;
36 @Input() public canInsert: boolean;
37 @Input() public canDelete: boolean;
38 @Input() public planItems: PlanTreeviewItem[];
39 @Output() paramChange = new EventEmitter<Parameter>();
40 @Output() insert = new EventEmitter<Parameter>();
41 @Output() delete = new EventEmitter<Parameter>();
44 public currentShowLabel: boolean;
45 public currentShowValue: boolean;
46 public currentCanEditName: boolean;
47 public currentCanEditValue: boolean;
48 public currentCanInsert: boolean;
49 public currentCanDelete: boolean;
51 public valueTypeEnum = ValueType;
52 public valueSourceEnum = ValueSource;
53 public sourceItems: { name: string, value: string }[] = [];
54 public planOptions = [];
55 public topologyOptions: { name: string, value: string }[] = [];
56 public showValueValue = true;
57 public showValueSource = true;
58 public planValue: any = {};
60 constructor(private toscaService: ToscaService) {
63 public ngOnInit(): void {
65 this.topologyOptions = this.toscaService.getTopologyProperties();
66 this.initSourceItems();
68 this.setValueSource(this.param.valueSource);
69 this.initPlanTreeViewItems(this.planItems);
72 private initSetting():void{
73 this.currentShowLabel = this.setDefaultValue(this.showLabel, true);
74 this.currentShowValue = this.setDefaultValue(this.showValue, true);
75 this.currentCanEditName = this.setDefaultValue(this.canEditName, false);
76 this.currentCanEditValue = this.setDefaultValue(this.canEditValue, true);
77 this.currentCanInsert = this.setDefaultValue(this.canInsert, false);
78 this.currentCanDelete = this.setDefaultValue(this.canDelete, false);
81 private setDefaultValue(param: boolean, value: boolean): boolean {
82 return undefined === param ? value : param;
85 private initSourceItems(): void {
86 this.sourceItems = [];
87 if (this.param.type !== ValueType[ValueType.object] && this.param.type !== ValueType[ValueType.array]) {
89 name: this.capitalizeFirstLetter(this.param.type),
90 value: this.param.type
93 this.valueSource.forEach(value => {
94 this.sourceItems.push({
95 name: ValueSource[value],
96 value: ValueSource[value]
101 private capitalizeFirstLetter(value: string): string {
102 const firstLetter = value.substring(0, 1);
103 const remainedLetter = value.substring(1);
104 return firstLetter.toUpperCase() + remainedLetter;
107 private initPlanValue(): void {
108 if (ValueSource[ValueSource.Plan] === this.param.valueSource) {
109 this.planValue = {id: this.param.value};
113 private setValueSource(valueSource: string): void {
114 if (ValueSource[ValueSource.Definition] === valueSource) {
115 this.showValueValue = false;
117 this.showValueValue = true;
121 private initPlanTreeViewItems(planTreeviewItems: PlanTreeviewItem[]): void {
122 this.planOptions = this.getTreeViewChild(planTreeviewItems);
125 private getTreeViewChild(planTreeviewItems: PlanTreeviewItem[]): any[] {
126 let treeviewItems = [];
127 if (undefined == planTreeviewItems || 0 === planTreeviewItems.length) {
128 // todo: debug check if it need [] or undefined.
129 return treeviewItems;
131 planTreeviewItems.forEach(item => {
132 const treeviewItem = {
137 children: this.getTreeViewChild(item.children)
139 treeviewItems.push(treeviewItem);
141 return treeviewItems;
144 public keyChange(key: string) {
145 this.param.name = key;
146 this.paramChange.emit(this.param);
149 private formatValue(value: any) {
151 switch (this.param.valueSource) {
152 case ValueSource[ValueSource.boolean]:
153 result = value === "true" ? true : false;
155 case ValueSource[ValueSource.integer]:
156 result = parseInt(value);
158 case ValueSource[ValueSource.number]:
159 result = parseFloat(value);
167 public valueChange(value: any) {
168 if (ValueSource[ValueSource.Plan] === this.param.valueSource) {
169 if ('object' === typeof (value)) {
170 this.planValue = value;
171 this.param.value = value.id;
173 this.planValue = {id: ''};
174 this.param.value = '';
177 this.param.value = this.formatValue(value);
179 this.paramChange.emit(this.param);
182 public valueSourceChange(valueSource: string) {
183 this.param.valueSource = valueSource;
184 this.setValueSource(valueSource);
185 this.valueChange('');
188 public insertParam(): void {
192 public deleteParam(): void {