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 { ChangeDetectionStrategy, Component, Input, OnInit, Output } from '@angular/core';
14 import { TreeNode } from 'primeng/primeng';
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";
25 * parameter tree presents parameter of task node's input or output parameters.
28 selector: 'wfm-parameter-tree',
29 styleUrls: ['./parameter-tree.component.css'],
30 templateUrl: 'parameter-tree.component.html',
31 changeDetection: ChangeDetectionStrategy.OnPush
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[];
41 constructor(private restService: RestService, private swaggerTreeConverterService: SwaggerTreeConverterService) { }
44 if (undefined === this.showValue) {
45 this.showValue = true;
49 public getParameter(node: any): Parameter {
50 // console.log('Parameter init:' + node.label +'.'+ node.type+'.'+JSON.stringify(node.value.value)+'.'+node.value.valueSource);
52 return new Parameter(node.label, node.value.value, node.value.valueSource, node.definition.type, node.definition.reqquired);
56 public paramChange(param: Parameter, node: any) {
57 // console.log('Parameter change:' + param.name + ', Node label is:' + node.label);
59 node.value.valueSource = param.valueSource;
60 node.value.value = param.value;
62 this.objectParameterChange(node);
64 if (node.label !== param.name) {
65 delete node.parent.value.value[node.label];
66 node.label = param.name;
69 node.parent.value.value[node.label] = node.value;
71 // this parameter is 'request param' or 'response param'
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
86 private getSwagger(): Swagger {
87 return this.restService.getSwaggerInfo(this.restConfigId);
90 public addChildNode4DynamicObject(node: any) {
91 const copyItem = WorkflowUtil.deepClone(node.definition.additionalProperties);
92 const key = Object.keys(node.value.value).length;
94 const childrenNode = this.swaggerTreeConverterService
95 .schema2TreeNode(this.getSwagger(), key, copyItem);
97 childrenNode.keyEditable = true;
98 node.value.value[key] = childrenNode.value;
100 node.children.push(childrenNode);
103 public addChildNode4ObjectArray(node: any) {
104 const copyItem = WorkflowUtil.deepClone(node.definition.items);
106 const childrenNode = this.swaggerTreeConverterService
109 node.children.length,
112 node.value.value.push(childrenNode.value);
113 node.children.push(childrenNode);
116 public deleteTreeNode(node: any) {
117 if ('array' === node.parent.type) {
119 node.parent.value.value.splice(node.label, 1);
120 node.parent.children.splice(node.label, 1);
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);
136 public getCanInsert(node: any) {
137 if (undefined === this.canInsert) {
138 if (node.value.valueSource !== ValueSource[ValueSource.Definition]) {
141 return this.isArrayObject(node) || this.isDynamicObject(node);
144 return this.canInsert
148 public getCanDelete(node: any) {
149 const parent = node.parent;
151 (this.isArrayObject(parent) || this.isDynamicObject(parent))) {
158 public getObjectValueSource(): ValueSource[] {
160 this.valueSource.forEach(source => {
161 if (ValueSource.string != source) {
165 result.push(ValueSource.Definition);
169 private isArrayObject(node: any): boolean {
170 return node.type === 'array';
173 private isDynamicObject(node: any): boolean {
174 return node.type === 'map';