93c3b72e548e8b854b35629c43136efa6668b37e
[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 { ChangeDetectionStrategy, Component, Input, OnInit, Output } from '@angular/core';
14 import { TreeNode } from 'primeng/primeng';
15
16 import { PlanTreeviewItem } from '../../../../../model/plan-treeview-item';
17 import { ValueSource } from '../../../../../model/value-source.enum';
18 import { Parameter } from '../../../../../model/workflow/parameter';
19 import { SwaggerTreeConverterService } from '../../../../../services/swagger-tree-converter.service';
20 import { WorkflowUtil } from '../../../../../util/workflow-util';
21 import { Swagger } from "../../../../../model/swagger";
22 import { RestService } from "../../../../../services/rest.service";
23
24 /**
25  * parameter tree presents parameter of task node's input or output parameters.
26  */
27 @Component({
28     selector: 'wfm-parameter-tree',
29     styleUrls: ['./parameter-tree.component.css'],
30     templateUrl: 'parameter-tree.component.html',
31     changeDetection: ChangeDetectionStrategy.OnPush
32 })
33 export class ParameterTreeComponent implements OnInit {
34     @Input() public parameters: TreeNode[];
35     @Input() public showValue: boolean;
36     @Input() public canInsert: boolean;
37     @Input() public restConfigId: string;
38     @Input() public valueSource: ValueSource[];
39     @Input() public planItems: PlanTreeviewItem[];
40
41     constructor(private restService: RestService, private swaggerTreeConverterService: SwaggerTreeConverterService) { }
42
43     public ngOnInit() {
44         if (undefined === this.showValue) {
45             this.showValue = true;
46         }
47     }
48
49     public getParameter(node: any): Parameter {
50         // console.log('Parameter init:' + node.label +'.'+ node.type+'.'+JSON.stringify(node.value.value)+'.'+node.value.valueSource);
51
52         return new Parameter(node.label, node.value.value, node.value.valueSource, node.definition.type, node.definition.reqquired);
53     }
54
55
56     public paramChange(param: Parameter, node: any) {
57         // console.log('Parameter change:' + param.name + ', Node label is:' + node.label);
58
59         node.value.valueSource = param.valueSource;
60         node.value.value = param.value;
61
62         this.objectParameterChange(node);
63
64         if (node.label !== param.name) {
65             delete node.parent.value.value[node.label];
66             node.label = param.name;
67         }
68         if (node.parent) {
69             node.parent.value.value[node.label] = node.value;
70         } else {
71             // this parameter is 'request param' or 'response param'
72         }
73
74     }
75
76     private objectParameterChange(node: any) {
77         if (node.value.valueSource === ValueSource[ValueSource.Definition]) { // value will be set by node defintion
78             const treeNode = this.swaggerTreeConverterService.schema2TreeNode(this.getSwagger(), node.label, node.definition, node.value);
79             node.value = treeNode.value;
80             node.children = treeNode.children;
81         } else {  // parameter value will be set by param
82             node.children = [];
83         }
84     }
85
86     private getSwagger(): Swagger {
87         return this.restService.getSwaggerInfo(this.restConfigId);
88     }
89
90     public addChildNode4DynamicObject(node: any) {
91         const copyItem = WorkflowUtil.deepClone(node.definition.additionalProperties);
92         const key = Object.keys(node.value.value).length;
93
94         const childrenNode = this.swaggerTreeConverterService
95             .schema2TreeNode(this.getSwagger(), key, copyItem);
96
97         childrenNode.keyEditable = true;
98         node.value.value[key] = childrenNode.value;
99
100         node.children.push(childrenNode);
101     }
102
103     public addChildNode4ObjectArray(node: any) {
104         const copyItem = WorkflowUtil.deepClone(node.definition.items);
105
106         const childrenNode = this.swaggerTreeConverterService
107             .schema2TreeNode(
108             this.getSwagger(),
109             node.children.length,
110             copyItem);
111
112         node.value.value.push(childrenNode.value);
113         node.children.push(childrenNode);
114     }
115
116     public deleteTreeNode(node: any) {
117         if ('array' === node.parent.type) {
118             // delete data
119             node.parent.value.value.splice(node.label, 1);
120             node.parent.children.splice(node.label, 1);
121
122             // update node index
123             node.parent.children.forEach((childNode, index) => childNode.label = index);
124         } else if ('map' === node.parent.type) {
125             delete node.parent.value.value[node.label];
126             for (let index = 0; index < node.parent.children.length; index++) {
127                 const element = node.parent.children[index];
128                 if (element.label === node.label) {
129                     node.parent.children.splice(index, 1);
130                     break;
131                 }
132             }
133         }
134     }
135
136     public getCanInsert(node: any) {
137         if (undefined === this.canInsert) {
138             if (node.value.valueSource !== ValueSource[ValueSource.Definition]) {
139                 return false;
140             } else {
141                 return this.isArrayObject(node) || this.isDynamicObject(node);
142             }
143         } else {
144             return this.canInsert
145         }
146     }
147
148     public getCanDelete(node: any) {
149         const parent = node.parent;
150         if (parent &&
151             (this.isArrayObject(parent) || this.isDynamicObject(parent))) {
152             return true;
153         } else {
154             return false;
155         }
156     }
157
158     public getObjectValueSource(): ValueSource[] {
159         const result = [];
160         this.valueSource.forEach(source => {
161             if (ValueSource.string != source) {
162                 result.push(source);
163             }
164         });
165         result.push(ValueSource.Definition);
166         return result;
167     }
168
169     private isArrayObject(node: any): boolean {
170         return node.type === 'array';
171     }
172
173     private isDynamicObject(node: any): boolean {
174         return node.type === 'map';
175     }
176 }