c208c1deb19cd9050ac5bd6e0ce4931a16ced0d2
[sdc/sdc-workflow-designer.git] /
1 /**
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
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12
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";
19
20 /**
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
23  */
24 @Component({
25     selector: 'wfm-parameter',
26     styleUrls: ['./parameter.component.css'],
27     templateUrl: 'parameter.component.html',
28 })
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>();
42
43     // Parameter setting
44     public currentShowLabel: boolean;
45     public currentShowValue: boolean;
46     public currentCanEditName: boolean;
47     public currentCanEditValue: boolean;
48     public currentCanInsert: boolean;
49     public currentCanDelete: boolean;
50
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 = {};
59
60     constructor(private toscaService: ToscaService) {
61     }
62
63     public ngOnInit(): void {
64         this.initSetting();
65         this.topologyOptions = this.toscaService.getTopologyProperties();
66         this.initSourceItems();
67         this.initPlanValue();
68         this.setValueSource(this.param.valueSource);
69         this.initPlanTreeViewItems(this.planItems);
70     }
71
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);
79     }
80
81     private setDefaultValue(param: boolean, value: boolean): boolean {
82         return undefined === param ? value : param;
83     }
84
85     private initSourceItems(): void {
86         this.sourceItems = [];
87         if (this.param.type !== ValueType[ValueType.object] && this.param.type !== ValueType[ValueType.array]) {
88             this.sourceItems = [{
89                 name: this.capitalizeFirstLetter(this.param.type),
90                 value: this.param.type
91             }];
92         }
93         this.valueSource.forEach(value => {
94             this.sourceItems.push({
95                 name: ValueSource[value],
96                 value: ValueSource[value]
97             });
98         });
99     }
100
101     private capitalizeFirstLetter(value: string): string {
102         const firstLetter = value.substring(0, 1);
103         const remainedLetter = value.substring(1);
104         return firstLetter.toUpperCase() + remainedLetter;
105     }
106
107     private initPlanValue(): void {
108         if (ValueSource[ValueSource.Plan] === this.param.valueSource) {
109             this.planValue = {id: this.param.value};
110         }
111     }
112
113     private setValueSource(valueSource: string): void {
114         if (ValueSource[ValueSource.Definition] === valueSource) {
115             this.showValueValue = false;
116         } else {
117             this.showValueValue = true;
118         }
119     }
120
121     private initPlanTreeViewItems(planTreeviewItems: PlanTreeviewItem[]): void {
122         this.planOptions = this.getTreeViewChild(planTreeviewItems);
123     }
124
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;
130         }
131         planTreeviewItems.forEach(item => {
132             const treeviewItem = {
133                 id: item.value,
134                 name: item.name,
135                 disabled: false,
136                 // !item.canSelect,
137                 children: this.getTreeViewChild(item.children)
138             };
139             treeviewItems.push(treeviewItem);
140         });
141         return treeviewItems;
142     }
143
144     public keyChange(key: string) {
145         this.param.name = key;
146         this.paramChange.emit(this.param);
147     }
148
149     private formatValue(value: any) {
150         let result;
151         switch (this.param.valueSource) {
152             case ValueSource[ValueSource.boolean]:
153                 result = value === "true" ? true : false;
154                 break;
155             case ValueSource[ValueSource.integer]:
156                 result = parseInt(value);
157                 break;
158             case ValueSource[ValueSource.number]:
159                 result = parseFloat(value);
160                 break;
161             default:
162                 result = value;
163         }
164         return result;
165     }
166
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;
172             } else {
173                 this.planValue = {id: ''};
174                 this.param.value = '';
175             }
176         } else {
177             this.param.value = this.formatValue(value);
178         }
179         this.paramChange.emit(this.param);
180     }
181
182     public valueSourceChange(valueSource: string) {
183         this.param.valueSource = valueSource;
184         this.setValueSource(valueSource);
185         this.valueChange('');
186     }
187
188     public insertParam(): void {
189         this.insert.emit();
190     }
191
192     public deleteParam(): void {
193         this.delete.emit();
194     }
195 }