wangyizhi
3 years ago
committed by
GitHub
21 changed files with 502 additions and 151 deletions
@ -0,0 +1,118 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
import type { Node, Edge } from '@antv/x6' |
||||
import { Connect, Location, TaskDefinition } from './types' |
||||
import { get } from 'lodash' |
||||
|
||||
/** |
||||
* Handling business entity and x6 entity conversion |
||||
* @param {Options} options |
||||
*/ |
||||
export function useBusinessMapper() { |
||||
/** |
||||
* Get connects, connects and processTaskRelationList are the same |
||||
* @param {Node[]} nodes |
||||
* @param {Edge[]} edges |
||||
* @param {TaskDefinition[]} taskDefinitions |
||||
* @returns {Connect[]} |
||||
*/ |
||||
function getConnects( |
||||
nodes: Node[], |
||||
edges: Edge[], |
||||
taskDefinitions: TaskDefinition[] |
||||
): Connect[] { |
||||
interface TailNodes { |
||||
[code: string]: boolean |
||||
} |
||||
// Nodes in DAG whose in-degree is not 0
|
||||
const tailNodes: TailNodes = {} |
||||
// If there is an edge target to a node, the node is tailNode
|
||||
edges.forEach((edge) => { |
||||
const targetId = edge.getTargetCellId() |
||||
tailNodes[targetId] = true |
||||
}) |
||||
const isHeadNode = (code: string) => !tailNodes[code] |
||||
|
||||
interface TasksMap { |
||||
[code: string]: TaskDefinition |
||||
} |
||||
const tasksMap: TasksMap = {} |
||||
nodes.forEach((node) => { |
||||
const code = node.id |
||||
const task = taskDefinitions.find((t) => t.code === Number(code)) |
||||
if (task) { |
||||
tasksMap[code] = task |
||||
} |
||||
}) |
||||
|
||||
const headConnects: Connect[] = nodes |
||||
.filter((node) => isHeadNode(node.id)) |
||||
.map((node) => { |
||||
const task = tasksMap[node.id] |
||||
return { |
||||
name: '', |
||||
preTaskCode: 0, |
||||
preTaskVersion: 0, |
||||
postTaskCode: task.code, |
||||
postTaskVersion: task.version || 0, |
||||
// conditionType and conditionParams are reserved
|
||||
conditionType: 'NONE', |
||||
conditionParams: {} |
||||
} |
||||
}) |
||||
|
||||
const tailConnects: Connect[] = edges.map((edge) => { |
||||
const labels = edge.getLabels() |
||||
const labelName = get(labels, ['0', 'attrs', 'label', 'text'], '') |
||||
const sourceId = edge.getSourceCellId() |
||||
const prevTask = tasksMap[sourceId] |
||||
const targetId = edge.getTargetCellId() |
||||
const task = tasksMap[targetId] |
||||
|
||||
return { |
||||
name: labelName, |
||||
preTaskCode: prevTask.code, |
||||
preTaskVersion: prevTask.version || 0, |
||||
postTaskCode: task.code, |
||||
postTaskVersion: task.version || 0, |
||||
// conditionType and conditionParams are reserved
|
||||
conditionType: 'NONE', |
||||
conditionParams: {} |
||||
} |
||||
}) |
||||
|
||||
return headConnects.concat(tailConnects) |
||||
} |
||||
|
||||
function getLocations(nodes: Node[]): Location[] { |
||||
return nodes.map((node) => { |
||||
const code = +node.id |
||||
const { x, y } = node.getPosition() |
||||
return { |
||||
taskCode: code, |
||||
x, |
||||
y |
||||
} |
||||
}) |
||||
} |
||||
|
||||
return { |
||||
getLocations, |
||||
getConnects |
||||
} |
||||
} |
@ -1,54 +0,0 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
import type { Ref } from 'vue' |
||||
import type { Graph } from '@antv/x6' |
||||
import { TaskType } from '../../../task/constants/task-type' |
||||
|
||||
interface Options { |
||||
graph: Ref<Graph | undefined> |
||||
} |
||||
|
||||
/** |
||||
* Expose some cell-related query methods and refs |
||||
* @param {Options} options |
||||
*/ |
||||
export function useCellQuery(options: Options) { |
||||
const { graph } = options |
||||
|
||||
/** |
||||
* Get all nodes |
||||
*/ |
||||
function getNodes() { |
||||
const nodes = graph.value?.getNodes() |
||||
if (!nodes) return [] |
||||
return nodes.map((node) => { |
||||
const position = node.getPosition() |
||||
const data = node.getData() |
||||
return { |
||||
code: node.id, |
||||
position: position, |
||||
name: data.taskName as string, |
||||
type: data.taskType as TaskType |
||||
} |
||||
}) |
||||
} |
||||
|
||||
return { |
||||
getNodes |
||||
} |
||||
} |
@ -0,0 +1,119 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
import { ref, onMounted } from 'vue' |
||||
import type { Ref } from 'vue' |
||||
import type { Graph } from '@antv/x6' |
||||
import type { Coordinate, NodeData } from './types' |
||||
import { TaskType } from '@/views/projects/task/constants/task-type' |
||||
import { useCellUpdate } from './dag-hooks' |
||||
|
||||
interface Options { |
||||
graph: Ref<Graph | undefined> |
||||
} |
||||
|
||||
/** |
||||
* Edit task configuration when dbclick |
||||
* @param {Options} options |
||||
* @returns |
||||
*/ |
||||
export function useTaskEdit(options: Options) { |
||||
const { graph } = options |
||||
|
||||
const { addNode, setNodeName } = useCellUpdate({ graph }) |
||||
|
||||
const taskDefinitions = ref<NodeData[]>([]) |
||||
const currTask = ref<NodeData>({ |
||||
taskType: 'SHELL', |
||||
code: 0, |
||||
name: '' |
||||
}) |
||||
const taskModalVisible = ref(false) |
||||
|
||||
/** |
||||
* Append a new task |
||||
*/ |
||||
function appendTask(code: number, type: TaskType, coordinate: Coordinate) { |
||||
addNode(code + '', type, '', coordinate) |
||||
taskDefinitions.value.push({ |
||||
code, |
||||
taskType: type, |
||||
name: '' |
||||
}) |
||||
openTaskModal({ code, taskType: type, name: '' }) |
||||
} |
||||
|
||||
function openTaskModal(task: NodeData) { |
||||
currTask.value = task |
||||
taskModalVisible.value = true |
||||
} |
||||
|
||||
/** |
||||
* The confirm event in task config modal |
||||
* @param formRef |
||||
* @param from |
||||
*/ |
||||
function taskConfirm({ formRef, form }: any) { |
||||
formRef.validate((errors: any) => { |
||||
if (!errors) { |
||||
// override target config
|
||||
taskDefinitions.value = taskDefinitions.value.map((task) => { |
||||
if (task.code === currTask.value?.code) { |
||||
setNodeName(task.code + '', form.name) |
||||
console.log(form) |
||||
console.log(JSON.stringify(form)) |
||||
return { |
||||
code: task.code, |
||||
...form |
||||
} |
||||
} |
||||
return task |
||||
}) |
||||
taskModalVisible.value = false |
||||
} |
||||
}) |
||||
} |
||||
|
||||
/** |
||||
* The cancel event in task config modal |
||||
*/ |
||||
function taskCancel() { |
||||
taskModalVisible.value = false |
||||
} |
||||
|
||||
onMounted(() => { |
||||
if (graph.value) { |
||||
graph.value.on('cell:dblclick', ({ cell }) => { |
||||
const code = Number(cell.id) |
||||
const definition = taskDefinitions.value.find((t) => t.code === code) |
||||
if (definition) { |
||||
currTask.value = definition |
||||
} |
||||
taskModalVisible.value = true |
||||
}) |
||||
} |
||||
}) |
||||
|
||||
return { |
||||
currTask, |
||||
taskModalVisible, |
||||
taskConfirm, |
||||
taskCancel, |
||||
appendTask, |
||||
taskDefinitions |
||||
} |
||||
} |
Loading…
Reference in new issue