summaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/app/components/property/rest-task/node-parameters/parameter-tree/parameter-tree.component.ts
blob: 93c3b72e548e8b854b35629c43136efa6668b37e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 * Copyright (c) 2017 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial API and implementation and/or initial documentation
 */

import { ChangeDetectionStrategy, Component, Input, OnInit, Output } from '@angular/core';
import { TreeNode } from 'primeng/primeng';

import { PlanTreeviewItem } from '../../../../../model/plan-treeview-item';
import { ValueSource } from '../../../../../model/value-source.enum';
import { Parameter } from '../../../../../model/workflow/parameter';
import { SwaggerTreeConverterService } from '../../../../../services/swagger-tree-converter.service';
import { WorkflowUtil } from '../../../../../util/workflow-util';
import { Swagger } from "../../../../../model/swagger";
import { RestService } from "../../../../../services/rest.service";

/**
 * parameter tree presents parameter of task node's input or output parameters.
 */
@Component({
    selector: 'wfm-parameter-tree',
    styleUrls: ['./parameter-tree.component.css'],
    templateUrl: 'parameter-tree.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class ParameterTreeComponent implements OnInit {
    @Input() public parameters: TreeNode[];
    @Input() public showValue: boolean;
    @Input() public canInsert: boolean;
    @Input() public restConfigId: string;
    @Input() public valueSource: ValueSource[];
    @Input() public planItems: PlanTreeviewItem[];

    constructor(private restService: RestService, private swaggerTreeConverterService: SwaggerTreeConverterService) { }

    public ngOnInit() {
        if (undefined === this.showValue) {
            this.showValue = true;
        }
    }

    public getParameter(node: any): Parameter {
        // console.log('Parameter init:' + node.label +'.'+ node.type+'.'+JSON.stringify(node.value.value)+'.'+node.value.valueSource);

        return new Parameter(node.label, node.value.value, node.value.valueSource, node.definition.type, node.definition.reqquired);
    }


    public paramChange(param: Parameter, node: any) {
        // console.log('Parameter change:' + param.name + ', Node label is:' + node.label);

        node.value.valueSource = param.valueSource;
        node.value.value = param.value;

        this.objectParameterChange(node);

        if (node.label !== param.name) {
            delete node.parent.value.value[node.label];
            node.label = param.name;
        }
        if (node.parent) {
            node.parent.value.value[node.label] = node.value;
        } else {
            // this parameter is 'request param' or 'response param'
        }

    }

    private objectParameterChange(node: any) {
        if (node.value.valueSource === ValueSource[ValueSource.Definition]) { // value will be set by node defintion
            const treeNode = this.swaggerTreeConverterService.schema2TreeNode(this.getSwagger(), node.label, node.definition, node.value);
            node.value = treeNode.value;
            node.children = treeNode.children;
        } else {  // parameter value will be set by param
            node.children = [];
        }
    }

    private getSwagger(): Swagger {
        return this.restService.getSwaggerInfo(this.restConfigId);
    }

    public addChildNode4DynamicObject(node: any) {
        const copyItem = WorkflowUtil.deepClone(node.definition.additionalProperties);
        const key = Object.keys(node.value.value).length;

        const childrenNode = this.swaggerTreeConverterService
            .schema2TreeNode(this.getSwagger(), key, copyItem);

        childrenNode.keyEditable = true;
        node.value.value[key] = childrenNode.value;

        node.children.push(childrenNode);
    }

    public addChildNode4ObjectArray(node: any) {
        const copyItem = WorkflowUtil.deepClone(node.definition.items);

        const childrenNode = this.swaggerTreeConverterService
            .schema2TreeNode(
            this.getSwagger(),
            node.children.length,
            copyItem);

        node.value.value.push(childrenNode.value);
        node.children.push(childrenNode);
    }

    public deleteTreeNode(node: any) {
        if ('array' === node.parent.type) {
            // delete data
            node.parent.value.value.splice(node.label, 1);
            node.parent.children.splice(node.label, 1);

            // update node index
            node.parent.children.forEach((childNode, index) => childNode.label = index);
        } else if ('map' === node.parent.type) {
            delete node.parent.value.value[node.label];
            for (let index = 0; index < node.parent.children.length; index++) {
                const element = node.parent.children[index];
                if (element.label === node.label) {
                    node.parent.children.splice(index, 1);
                    break;
                }
            }
        }
    }

    public getCanInsert(node: any) {
        if (undefined === this.canInsert) {
            if (node.value.valueSource !== ValueSource[ValueSource.Definition]) {
                return false;
            } else {
                return this.isArrayObject(node) || this.isDynamicObject(node);
            }
        } else {
            return this.canInsert
        }
    }

    public getCanDelete(node: any) {
        const parent = node.parent;
        if (parent &&
            (this.isArrayObject(parent) || this.isDynamicObject(parent))) {
            return true;
        } else {
            return false;
        }
    }

    public getObjectValueSource(): ValueSource[] {
        const result = [];
        this.valueSource.forEach(source => {
            if (ValueSource.string != source) {
                result.push(source);
            }
        });
        result.push(ValueSource.Definition);
        return result;
    }

    private isArrayObject(node: any): boolean {
        return node.type === 'array';
    }

    private isDynamicObject(node: any): boolean {
        return node.type === 'map';
    }
}