123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- /*
- * 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, watch } from 'vue'
- import { remove } from 'lodash'
- import { TaskType } from '@/views/projects/task/constants/task-type'
- import { formatParams } from '@/views/projects/task/components/node/format-data'
- import { useCellUpdate } from './dag-hooks'
- import type { Ref } from 'vue'
- import type { Graph } from '@antv/x6'
- import type {
- Coordinate,
- NodeData,
- WorkflowDefinition,
- EditWorkflowDefinition
- } from './types'
- interface Options {
- graph: Ref<Graph | undefined>
- definition: Ref<WorkflowDefinition | undefined>
- }
- /**
- * Edit task configuration when dbclick
- * @param {Options} options
- * @returns
- */
- export function useTaskEdit(options: Options) {
- const { graph, definition } = options
- const { addNode, removeNode, getSources, setNodeName, setNodeEdge } =
- useCellUpdate({
- graph
- })
- const processDefinition = ref(
- definition?.value || {
- processDefinition: {},
- processTaskRelationList: [],
- taskDefinitionList: []
- }
- ) as Ref<EditWorkflowDefinition>
- 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, '', 'YES', coordinate)
- processDefinition.value.taskDefinitionList.push({
- code,
- taskType: type,
- name: ''
- })
- openTaskModal({ code, taskType: type, name: '' })
- }
- /**
- * Copy a task
- */
- function copyTask(
- name: string,
- code: number,
- targetCode: number,
- type: TaskType,
- flag: string,
- coordinate: Coordinate
- ) {
- addNode(code + '', type, name, flag, coordinate)
- const definition = processDefinition.value.taskDefinitionList.find(
- (t) => t.code === targetCode
- )
- const newDefinition = {
- ...definition,
- code,
- name
- } as NodeData
- processDefinition.value.taskDefinitionList.push(newDefinition)
- }
- /**
- * Remove task
- * @param {number} code
- */
- function removeTasks(codes: number[]) {
- processDefinition.value.taskDefinitionList =
- processDefinition.value.taskDefinitionList.filter(
- (task) => !codes.includes(task.code)
- )
- }
- function openTaskModal(task: NodeData) {
- currTask.value = task
- taskModalVisible.value = true
- }
- /**
- * Edit task
- * @param {number} code
- */
- function editTask(code: number) {
- const definition = processDefinition.value.taskDefinitionList.find(
- (t) => t.code === code
- )
- if (definition) {
- currTask.value = definition
- }
- updatePreTasks(getSources(String(code)), code)
- taskModalVisible.value = true
- }
- /**
- * The confirm event in task config modal
- * @param formRef
- * @param from
- */
- function taskConfirm({ data }: any) {
- const taskDef = formatParams(data).taskDefinitionJsonObj as NodeData
- // override target config
- processDefinition.value.taskDefinitionList =
- processDefinition.value.taskDefinitionList.map((task) => {
- if (task.code === currTask.value?.code) {
- setNodeName(task.code + '', taskDef.name)
- setNodeEdge(String(task.code), data.preTasks)
- updatePreTasks(data.preTasks, task.code)
- return {
- ...taskDef,
- version: task.version,
- code: task.code,
- taskType: currTask.value.taskType
- }
- }
- return task
- })
- taskModalVisible.value = false
- }
- /**
- * The cancel event in task config modal
- */
- function taskCancel() {
- taskModalVisible.value = false
- if (!currTask.value.name) {
- removeNode(String(currTask.value.code))
- remove(
- processDefinition.value.taskDefinitionList,
- (task) => task.code === currTask.value.code
- )
- }
- }
- function updatePreTasks(preTasks: number[], code: number) {
- if (processDefinition.value?.processTaskRelationList?.length) {
- remove(
- processDefinition.value.processTaskRelationList,
- (process) => process.postTaskCode === code
- )
- }
- if (!preTasks?.length) return
- preTasks.forEach((task) => {
- processDefinition.value?.processTaskRelationList.push({
- postTaskCode: code,
- preTaskCode: task,
- name: '',
- preTaskVersion: 1,
- postTaskVersion: 1,
- conditionType: 'NONE',
- conditionParams: {}
- })
- })
- }
- onMounted(() => {
- if (graph.value) {
- graph.value.on('cell:dblclick', ({ cell }) => {
- const code = Number(cell.id)
- editTask(code)
- })
- }
- })
- watch(definition, () => {
- if (definition.value) processDefinition.value = definition.value
- })
- return {
- currTask,
- taskModalVisible,
- processDefinition,
- taskConfirm,
- taskCancel,
- appendTask,
- editTask,
- copyTask,
- removeTasks
- }
- }
|