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