wujingjing
2024-12-11 3229f6a34f4982d1a551d86e68d313482e33f701
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
import { HandleType } from '@vue-flow/core';
import { v4 as uuid } from 'uuid';
import { VueFlowConstant } from './VueFlowConstant';
import { CompareOperation, ConditionOperator, NodeType, VarType, nodeTypeMap } from './vueFlowEnum';
import { get } from 'lodash';
 
export class VueFlowHelper {
    static genId() {
        return uuid().slice(0, 8);
    }
 
    static genGeometryId(){
        return uuid().slice(0, 12);
    }
 
    static getDefaultData = (type: NodeType) => {
        let data: any = {
            title: nodeTypeMap[type],
        };
        switch (type) {
            case NodeType.Start:
                data[VueFlowConstant.GROUP_PARAMS_KEY] = [
                    {
                        [VueFlowConstant.PARAMS_KEY]: [
                            {
                                key: 'var_list',
                                label: '',
                                type: 'var_list',
                                value: [],
                            },
                        ],
                    },
                ];
                break;
            case NodeType.Condition:
                data[VueFlowConstant.GROUP_PARAMS_KEY] = [
                    {
                        [VueFlowConstant.PARAMS_KEY]: [
                            {
                                key: 'condition',
                                label: '',
                                type: 'condition',
                                value: [ConditionHelper.getDefaultConditionGroup()],
                            },
                        ],
                    },
                ];
                break;
            default:
                break;
        }
        return data;
    };
 
    static getHandleId = (node: any, handleType: HandleType, order?: number) => {
        const orderSuffix = order == undefined ? '' : `__${order + ''}`;
        return `${node.id}__handle-${handleType}${orderSuffix}`;
    };
 
    static getFieldValue = (data, key, index = 0) => {
        let varList = [];
        const group = data?.[VueFlowConstant.GROUP_PARAMS_KEY];
        if (group && group.length > 0) {
            if (index !== null) {
                const val = group?.[index]?.[VueFlowConstant.PARAMS_KEY]?.find((item) => item.key === key)?.value;
                if (val) {
                    varList.push(val);
                }
            } else {
                for (const item of group) {
                    if (item[VueFlowConstant.PARAMS_KEY].key === key) {
                        varList.push(item[VueFlowConstant.PARAMS_KEY].value);
                    }
                }
            }
        }
        if (varList.length === 0) {
            return null;
        } else if (varList.length === 1) {
            return varList[0];
        } else {
            return varList;
        }
    };
}
 
export class StartNodeHelper {
    // static getDefaultData = () => {
    //     return {
    //         title: nodeTypeMap[NodeType.Start],
    //     };
    // };
 
    static getVarList = (data) => {
        const varList = data[VueFlowConstant.GROUP_PARAMS_KEY][0][VueFlowConstant.PARAMS_KEY].find(
            (item) => item.key === 'condition'
        ).value;
        return varList;
    };
}
 
export class ConditionHelper {
    static getConditionItem = (
        left?: {
            var: string;
            label: string;
        },
        right?: {
            type: VarType;
            value: string;
            label: string;
        },
        operation?: CompareOperation
    ) => {
        return {
            id: VueFlowHelper.genId(),
            left_var: left?.var ?? '',
            left_label: left?.label ?? '',
            comparison_operation: operation ?? '',
            right_value_type: right?.type ?? VarType.Input,
            right_value: right?.value ?? '',
            right_label: right?.label ?? '',
        };
    };
 
    static getDefaultConditionGroup = () => {
        return {
            id: VueFlowHelper.genId(),
            operator: ConditionOperator.And,
            conditions: [],
        };
    };
}