7f6ffbe22106732602e45e314cd64198343b9213
[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 {Component, EventEmitter, Input, Output, ViewChild} from "@angular/core";
14 import {TranslateService} from "@ngx-translate/core";
15 import {isArray, isObject} from "util";
16 import {ModelService} from "../../services/model.service";
17 import {NoticeService} from "../../services/notice.service";
18 import {NodeType} from "../../model/workflow/node-type.enum";
19 import {ValueSource} from "../../model/value-source.enum";
20 import {JsPlumbService} from "../../services/jsplumb.service";
21 import {WorkflowNode} from "../../model/workflow/workflow-node";
22
23 /**
24  * node or workflow-line name
25  */
26 @Component({
27     selector: 'wfm-editable-property',
28     templateUrl: 'editable-property.component.html',
29     styleUrls: ['./editable-property.component.css']
30 })
31 export class EditablePropertyComponent {
32     @Input() public value: string;
33     @Output() public valueChange = new EventEmitter<string>();
34     @ViewChild('editInput') private editInput: any;
35
36     public editValue = '';
37     public editable = false;
38
39     public preEdit(): void {
40         if (!this.editable) {
41             this.editable = true;
42             this.editValue = this.value;
43         }
44     }
45
46     public afterEdit(): void {
47         if (this.editable) {
48             this.editable = false;
49             if (this.value === this.editValue) {
50                 return;
51             }
52
53             if (this.editInput.wfInput.valid) {
54                 this.value = this.editValue;
55                 this.valueChange.emit(this.value);
56             } else {
57                 this.editValue = this.value;
58             }
59         }
60     }
61
62     /*private validate(): boolean {
63         const nodes = this.modelService.getNodes();
64         const existNode = nodes.find(node => node.id === this.editValue);
65         if (existNode) {
66             this.translate.get('WORKFLOW.MSG.NODE_ID_SAME', {value: existNode.id}).subscribe((res: string) => {
67                 this.notice.error(res, 10000);
68             });
69             return false;
70         }
71         return true;
72     }
73
74     private changeReferencedValue(): void {
75         const newNodeConnections = [];
76         const nodes = this.modelService.getNodes();
77         nodes.forEach((node: any) => {
78             this.changeConnectionReferencedValue(node, this.value, this.editValue);
79
80             if (node.type === NodeType[NodeType.restTask]) {
81                 const parameters = node.parameters || [];
82                 parameters.forEach(param => {
83                     this.changeRestTaskReferencedValue(param, this.value, this.editValue);
84                 });
85             }
86         });
87     }
88
89     private changeConnectionReferencedValue(node: WorkflowNode, oldValue: string, newValue: string): void {
90         node.connection.forEach(connection => {
91             if (connection.sourceRef === oldValue) {
92                 connection.sourceRef = newValue;
93             } else if (connection.targetRef === oldValue) {
94                 connection.targetRef = newValue;
95             }
96         });
97     }
98
99     // 当restTask类型的节点的属性中valueSource是Plan时,value的值为引用其他节点id
100     // value格式为[restTask_2].[responseBody].[name],可能有更多层,当时第一个[]里面的值为被引用节点的id
101     private changeRestTaskReferencedValue(param: any, oldValue: string, newValue: string): void {
102         if (param.valueSource === ValueSource[ValueSource.Plan]) {
103             const value = param.value || '';
104             const index = value.indexOf('].[');
105             if (index > -1) {
106                 const nodeId = value.substring(1, index);
107                 if (nodeId === oldValue) {
108                     param.value = '[' + newValue + value.substring(index);
109                 }
110             }
111         } else if (param.valueSource === ValueSource[ValueSource.Definition]) {
112             const value = param.value;
113             if (isArray(value)) {
114                 value.forEach(subValue => {
115                     this.changeRestTaskReferencedValue(subValue, oldValue, newValue);
116                 });
117             } else if (isObject(value)) {
118                 this.changeRestTaskReferencedValue(value, oldValue, newValue);
119             }
120         }
121     }*/
122 }