Files
auto-solution/modeler/src/views/decision/utils/node.ts

146 lines
4.0 KiB
TypeScript
Raw Normal View History

2026-02-08 15:59:14 +08:00
/*
* This file is part of the kernelstudio package.
*
* (c) 2014-2026 zlin <admin@kernelstudio.com>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
import { Edge, Graph, Node } from '@antv/x6';
import type { EdgeNodeElement, NodeGraph, NodeTemplate, SettingTaskNodeElement, TaskNodeElement, TaskNodeRect } from '../types';
import { generateKey } from '@/utils/strings.ts';
export const createTaskNodeElementFromTemplate = (
template: NodeTemplate,
rect?: TaskNodeRect,
): SettingTaskNodeElement => {
let realRect = { width: 200, height: 100, x: 0, y: 0, ...rect || {} };
console.info('rect', rect);
return {
id: 0,
key: generateKey(template.type),
status: null,
template: template.id,
type: template.type,
name: template.name,
description: template.description,
position: {
x: realRect.x ?? 0,
y: realRect.y ?? 0,
},
width: realRect.width,
height: realRect.height,
settings: JSON.parse(JSON.stringify(template.parameter_defs ?? [])),
inputs: null,
outputs: null,
parameters: {},
variables: [
{
key: generateKey('var_'),
name: '范围',
value: '1000',
defaults: '1000',
unit: 'KM',
},
{
key: generateKey('var_'),
name: '武器名称',
value: '地对空导弹',
defaults: '地对空导弹',
unit: '个',
},
],
} as SettingTaskNodeElement;
};
export const createTaskNodeElement = (element: TaskNodeElement, width: number = 200, height: number = 100): any => {
return {
shape: 'task',
id: element.key,
position: {
x: element.position?.x || 0,
y: element.position?.y || 0,
},
size: {
width: element.width ?? width,
height: element.height ?? height,
},
attrs: {
label: {
text: element.name,
},
},
data: element,
};
};
export const resolveNodeTaskElements = (graph: Graph): TaskNodeElement[] => {
const taskElements: TaskNodeElement[] = [];
if (graph) {
const nodes = graph?.getNodes() as Node[];
if (nodes) {
nodes.forEach(node => {
const nodeData = node.getData() as TaskNodeElement;
const newElement = {
...nodeData,
key: node.id,
position: node.getPosition(),
width: node.getSize().width,
height: node.getSize().height,
};
taskElements.push(newElement);
});
}
}
return taskElements;
};
export const resolveNodeEdgeElements = (graph: Graph): EdgeNodeElement[] => {
const edgeElements: EdgeNodeElement[] = [];
if (graph) {
const graphEdges = graph?.getEdges() ?? [] as Edge[];
if (graphEdges) {
graphEdges.forEach(edge => {
const nodeData = edge.getData() as TaskNodeElement;
edgeElements.push({
id: nodeData?.id ?? 0,
key: edge.id,
type: 'edge',
status: nodeData?.status,
source: edge.getSource() ? edge.getSource() as unknown as string : null,
target: edge.getSource() ? edge.getTarget() as unknown as string : null,
attrs: edge.getAttrs() ?? {},
router: edge.getRouter() ?? {},
connector: edge.getConnector() ?? null,
});
});
}
}
return edgeElements;
};
export const resolveNodeGraph = (graph: Graph): NodeGraph => {
const nodes: TaskNodeElement[] = resolveNodeTaskElements(graph);
const edges: EdgeNodeElement[] = resolveNodeEdgeElements(graph);
return {
nodes,
edges,
};
};
export const hasElements = (graph: Graph): boolean => {
if (graph) {
const taskElements: TaskNodeElement[] = resolveNodeTaskElements(graph);
return taskElements.length > 0;
}
return false;
};
export const hasRootElementNode = (graph: Graph): boolean => {
if (graph) {
const taskElements: TaskNodeElement[] = resolveNodeTaskElements(graph);
return taskElements.filter(e => e.type === 'root').length === 1;
}
return false;
};