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, OnChanges, Output, SimpleChange, SimpleChanges } 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 { 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";
26 * parameter tree presents parameter of task node's input or output parameters.
29 selector: 'b4t-parameter-tree',
30 styleUrls: ['./parameter-tree.component.css'],
31 templateUrl: 'parameter-tree.component.html',
32 changeDetection: ChangeDetectionStrategy.OnPush
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[];
40 constructor(private restService: RestService, private swaggerTreeConverterService: SwaggerTreeConverterService) { }
42 public ngOnChanges(changes: SimpleChanges) {
45 public getParameter(node: any): Parameter {
46 // console.log('Parameter init:' + node.label +'.'+ node.type+'.'+JSON.stringify(node.value.value)+'.'+node.value.valueSource);
48 return new Parameter(node.label, node.value.value, node.value.valueSource, node.definition.type, node.definition.reqquired);
52 public paramChange(param: Parameter, node: any) {
53 // console.log('Parameter change:' + param.name + ', Node label is:' + node.label);
55 node.value.valueSource = param.valueSource;
56 node.value.value = param.value;
58 this.objectParameterChange(node);
60 if (node.label !== param.name) {
61 delete node.parent.value.value[node.label];
62 node.label = param.name;
65 node.parent.value.value[node.label] = node.value;
67 // this parameter is 'request param' or 'response param'
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
82 private getSwagger(): Swagger {
83 return this.restService.getSwaggerInfo(this.restConfigId);
86 public addChildNode4DynamicObject(node: any) {
87 const copyItem = WorkflowUtil.deepClone(node.definition.additionalProperties);
88 const key = Object.keys(node.value.value).length;
90 const childrenNode = this.swaggerTreeConverterService
91 .schema2TreeNode(this.getSwagger(), key, copyItem);
93 childrenNode.keyEditable = true;
94 node.value.value[key] = childrenNode.value;
96 node.children.push(childrenNode);
99 public addChildNode4ObjectArray(node: any) {
100 const copyItem = WorkflowUtil.deepClone(node.definition.items);
102 const childrenNode = this.swaggerTreeConverterService
105 node.children.length,
108 node.value.value.push(childrenNode.value);
109 node.children.push(childrenNode);
112 public deleteTreeNode(node: any) {
113 if ('array' === node.parent.type) {
115 node.parent.value.value.splice(node.label, 1);
116 node.parent.children.splice(node.label, 1);
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);
132 public canInsert(node: any) {
133 if (node.value.valueSource !== ValueSource[ValueSource.Definition]) {
136 return this.isArrayObject(node) || this.isDynamicObject(node);
140 public canDelete(node: any) {
141 const parent = node.parent;
143 (this.isArrayObject(parent) || this.isDynamicObject(parent))) {
150 public getObjectValueSource(): ValueSource[] {
152 this.valueSource.forEach(source => {
153 if (ValueSource.String != source) {
157 result.push(ValueSource.Definition);
161 private isArrayObject(node: any): boolean {
162 return node.type === 'array';
165 private isDynamicObject(node: any): boolean {
166 return node.type === 'map';