mirror of
https://github.com/baserow/baserow.git
synced 2026-12-05 08:48:35 +00:00
* Add recipies for For and While loops * Add tests * Add changelog * Fix the clean-up when rolling back
1007 lines
28 KiB
JavaScript
1007 lines
28 KiB
JavaScript
import { useNuxtApp } from '#app'
|
|
import { uuid } from '@baserow/modules/core/utils/string'
|
|
import AutomationWorkflowNodeService from '@baserow/modules/automation/services/automationWorkflowNode'
|
|
import { NodeEditorSidePanelType } from '@baserow/modules/automation/editorSidePanelTypes'
|
|
import { clone } from '@baserow/modules/core/utils/object'
|
|
import {
|
|
createNewUndoRedoActionGroupId,
|
|
getUndoRedoActionRequestConfig,
|
|
} from '@baserow/modules/database/utils/action'
|
|
import {
|
|
markRealtimeMetadata,
|
|
realtimeMetadata,
|
|
} from '@baserow/modules/core/utils/realtime'
|
|
|
|
import NodeGraphHandler from '@baserow/modules/automation/utils/nodeGraphHandler'
|
|
|
|
const state = () => ({
|
|
selectedNodeId: null,
|
|
draggingNodeId: null,
|
|
})
|
|
|
|
const updateContext = {
|
|
updateTimeout: null,
|
|
promiseResolve: null,
|
|
lastUpdatedValues: null,
|
|
valuesToUpdate: {},
|
|
}
|
|
|
|
const updateCachedValues = (workflow) => {
|
|
if (!workflow || !workflow.nodes) return
|
|
|
|
workflow.nodeMap = Object.fromEntries(
|
|
workflow.nodes.map((node) => [`${node.id}`, node])
|
|
)
|
|
}
|
|
|
|
export function populateNode(node) {
|
|
return { ...node, _: { loading: false, ...realtimeMetadata() } }
|
|
}
|
|
|
|
export function getWorkflowImmediateDispatch($registry, workflow) {
|
|
const trigger = workflow.nodes?.find((node) => {
|
|
return $registry.get('node', node.type).isTrigger
|
|
})
|
|
|
|
if (!trigger) {
|
|
return false
|
|
}
|
|
|
|
return $registry
|
|
.get('node', trigger.type)
|
|
.serviceType.canBeImmediatelyDispatched(trigger.service)
|
|
}
|
|
|
|
export function getNodeTypeImmediateDispatch($registry, type, service = {}) {
|
|
const nodeType = $registry.get('node', type)
|
|
|
|
if (!nodeType.isTrigger) {
|
|
return false
|
|
}
|
|
|
|
return nodeType.serviceType.canBeImmediatelyDispatched(service)
|
|
}
|
|
|
|
const mutations = {
|
|
SET_ITEMS(state, { workflow, nodes }) {
|
|
workflow.nodes = nodes.map((node) => populateNode(node))
|
|
workflow.selectedNodeId = null
|
|
updateCachedValues(workflow)
|
|
},
|
|
ADD_ITEM(state, { workflow, node }) {
|
|
workflow.nodes.push(populateNode(node))
|
|
updateCachedValues(workflow)
|
|
},
|
|
UPDATE_ITEM(
|
|
state,
|
|
{
|
|
workflow,
|
|
node: nodeToUpdate,
|
|
values,
|
|
override = false,
|
|
viaRealtime = false,
|
|
}
|
|
) {
|
|
if (override) {
|
|
const index = workflow.nodes.findIndex(
|
|
(item) => item.id === nodeToUpdate.id
|
|
)
|
|
const previous = workflow.nodes[index]
|
|
const newNode = populateNode(values)
|
|
// Carry the realtime version over the object replacement so a subsequent
|
|
// increment is still detected as a change by watchers.
|
|
newNode._.realtimeVersion = previous?._?.realtimeVersion || 0
|
|
markRealtimeMetadata(newNode, viaRealtime)
|
|
workflow.nodes[index] = newNode
|
|
updateCachedValues(workflow)
|
|
} else {
|
|
const node = workflow.nodeMap[nodeToUpdate.id]
|
|
Object.assign(node, values)
|
|
markRealtimeMetadata(node, viaRealtime)
|
|
}
|
|
},
|
|
DELETE_ITEM(state, { workflow, nodeId }) {
|
|
const nodeIdStr = nodeId.toString()
|
|
workflow.nodes = workflow.nodes.filter(
|
|
(item) => item.id.toString() !== nodeIdStr
|
|
)
|
|
updateCachedValues(workflow)
|
|
},
|
|
SELECT_ITEM(state, { workflow, node }) {
|
|
if (!workflow) {
|
|
return
|
|
}
|
|
workflow.selectedNodeId = node?.id || null
|
|
},
|
|
SET_LOADING(state, { node, value }) {
|
|
node._.loading = value
|
|
},
|
|
SET_DRAGGING_NODE_ID(state, nodeId) {
|
|
state.draggingNodeId = nodeId
|
|
},
|
|
}
|
|
|
|
const actions = {
|
|
forceCreate({ commit, getters, dispatch }, { workflow, node }) {
|
|
if (!workflow) return
|
|
|
|
const nodeIdStr = node.id?.toString()
|
|
if (workflow.nodes?.some((item) => item.id.toString() === nodeIdStr)) {
|
|
return
|
|
}
|
|
|
|
// Add the new node into the workflow
|
|
commit('ADD_ITEM', { workflow, node })
|
|
},
|
|
async fetch({ commit }, { workflow }) {
|
|
if (!workflow) return []
|
|
|
|
const { data: nodes } = await AutomationWorkflowNodeService(
|
|
this.$client
|
|
).get(workflow.id)
|
|
|
|
if (!workflow.nodes) {
|
|
workflow.nodes = []
|
|
}
|
|
|
|
commit('SET_ITEMS', { workflow, nodes })
|
|
return nodes
|
|
},
|
|
async graphInsert(
|
|
{ commit, dispatch, getters },
|
|
{ workflow, node, referenceNode, position, output }
|
|
) {
|
|
const graphHandler = new NodeGraphHandler(workflow)
|
|
graphHandler.insert(node, referenceNode, position, output)
|
|
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: { graph: graphHandler.graph },
|
|
},
|
|
{ root: true }
|
|
)
|
|
},
|
|
async graphRemove({ commit, dispatch, getters }, { workflow, node }) {
|
|
const graphHandler = new NodeGraphHandler(workflow)
|
|
graphHandler.remove(node)
|
|
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: { graph: graphHandler.graph },
|
|
},
|
|
{ root: true }
|
|
)
|
|
},
|
|
async graphMove(
|
|
{ commit, dispatch, getters },
|
|
{ workflow, nodeToMove, referenceNode, position, output }
|
|
) {
|
|
const graphHandler = new NodeGraphHandler(workflow)
|
|
graphHandler.move(nodeToMove, referenceNode, position, output)
|
|
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: { graph: graphHandler.graph },
|
|
},
|
|
{ root: true }
|
|
)
|
|
},
|
|
async graphReplace(
|
|
{ commit, dispatch, getters },
|
|
{ workflow, nodeToReplace, newNode }
|
|
) {
|
|
const graphHandler = new NodeGraphHandler(workflow)
|
|
graphHandler.replace(nodeToReplace, newNode)
|
|
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: { graph: graphHandler.graph },
|
|
},
|
|
{ root: true }
|
|
)
|
|
},
|
|
/**
|
|
* `undoRedoActionGroupId` groups this request with others under one undo
|
|
* step on the backend; `select: false` leaves the new node unselected, for
|
|
* callers that create several nodes in a row and select one at the end.
|
|
*/
|
|
async create(
|
|
{ commit, dispatch, getters },
|
|
{
|
|
workflow,
|
|
type,
|
|
referenceNode,
|
|
position,
|
|
output,
|
|
undoRedoActionGroupId = null,
|
|
select = true,
|
|
}
|
|
) {
|
|
// Using the `previousNodeId` and `previousNodeOutput` to determine
|
|
// what the `beforeId` should be. We will have `beforeId` if we're
|
|
// creating a node after `previousNodeId`, and `previousNodeId` has
|
|
// a node that follows it.
|
|
const nodeType = this.$registry.get('node', type)
|
|
const oldImmediateDispatch = workflow.immediate_dispatch
|
|
|
|
// Apply optimistic create
|
|
const tempNode = nodeType.getDefaultValues({
|
|
id: uuid(),
|
|
type,
|
|
workflow: workflow.id,
|
|
})
|
|
|
|
commit('ADD_ITEM', { workflow, node: tempNode })
|
|
if (nodeType.isTrigger) {
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: {
|
|
immediate_dispatch: getNodeTypeImmediateDispatch(
|
|
this.$registry,
|
|
type
|
|
),
|
|
},
|
|
},
|
|
{ root: true }
|
|
)
|
|
}
|
|
|
|
const initialGraph = clone(workflow.graph)
|
|
|
|
dispatch('graphInsert', {
|
|
workflow,
|
|
node: tempNode,
|
|
referenceNode,
|
|
position,
|
|
output,
|
|
})
|
|
|
|
try {
|
|
const { data: node } = await AutomationWorkflowNodeService(
|
|
this.$client
|
|
).create(
|
|
workflow.id,
|
|
type,
|
|
referenceNode,
|
|
position,
|
|
output,
|
|
getUndoRedoActionRequestConfig({ undoRedoActionGroupId })
|
|
)
|
|
|
|
commit('ADD_ITEM', { workflow, node })
|
|
|
|
await dispatch('graphReplace', {
|
|
workflow,
|
|
nodeToReplace: tempNode,
|
|
newNode: node,
|
|
})
|
|
|
|
// Remove temp node and add real one
|
|
commit('DELETE_ITEM', { workflow, nodeId: tempNode.id })
|
|
if (nodeType.isTrigger) {
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: {
|
|
immediate_dispatch: getWorkflowImmediateDispatch(
|
|
this.$registry,
|
|
workflow
|
|
),
|
|
},
|
|
},
|
|
{ root: true }
|
|
)
|
|
}
|
|
|
|
await dispatch('application/refreshPermissions', workflow.automation_id, {
|
|
root: true,
|
|
})
|
|
|
|
if (select) {
|
|
setTimeout(() => {
|
|
const populatedNode = getters.findById(workflow, node.id)
|
|
dispatch('select', { workflow, node: populatedNode })
|
|
})
|
|
}
|
|
|
|
return node
|
|
} catch (error) {
|
|
// If API fails, remove the temporary node
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: { graph: initialGraph },
|
|
},
|
|
{ root: true }
|
|
)
|
|
commit('DELETE_ITEM', { workflow, nodeId: tempNode.id })
|
|
if (nodeType.isTrigger) {
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: { immediate_dispatch: oldImmediateDispatch },
|
|
},
|
|
{ root: true }
|
|
)
|
|
}
|
|
|
|
throw error
|
|
}
|
|
},
|
|
/**
|
|
* Inserts the nodes of a recipe (see `nodeRecipeTypes.js`) one after the
|
|
* other. Every request carries the same undo action group id, so a single
|
|
* undo removes the whole recipe. If a step fails, the nodes created so far
|
|
* are deleted again under that same group id, so an undo of the failed
|
|
* attempt is a no-op rather than resurrecting part of it, and the error is
|
|
* rethrown for the caller to report. A node that cannot be deleted stays in
|
|
* the workflow together with the ones created before it, and a warning
|
|
* toast says so. Nothing is selected while the recipe runs; its first node
|
|
* is selected once it is complete.
|
|
* @returns {Array} - The created nodes, in creation order.
|
|
*/
|
|
async createFromRecipe(
|
|
{ dispatch, getters },
|
|
{ workflow, recipe, referenceNode, position, output }
|
|
) {
|
|
const undoRedoActionGroupId = createNewUndoRedoActionGroupId()
|
|
const created = []
|
|
|
|
const addNode = async ({
|
|
type,
|
|
referenceNode,
|
|
position,
|
|
output,
|
|
label,
|
|
service,
|
|
}) => {
|
|
const { id } = await dispatch('create', {
|
|
workflow,
|
|
type,
|
|
referenceNode,
|
|
position,
|
|
output,
|
|
undoRedoActionGroupId,
|
|
select: false,
|
|
})
|
|
// `update` needs the node as stored, not the bare server response.
|
|
let node = getters.findById(workflow, id)
|
|
created.push(node)
|
|
|
|
const values = {}
|
|
if (label !== undefined) {
|
|
values.label = label
|
|
}
|
|
if (service !== undefined) {
|
|
values.service = service
|
|
}
|
|
if (Object.keys(values).length > 0) {
|
|
await dispatch('update', {
|
|
workflow,
|
|
node,
|
|
values,
|
|
undoRedoActionGroupId,
|
|
})
|
|
node = getters.findById(workflow, id)
|
|
}
|
|
return node
|
|
}
|
|
|
|
try {
|
|
await recipe.create({
|
|
workflow,
|
|
referenceNode,
|
|
position,
|
|
output,
|
|
addNode,
|
|
})
|
|
} catch (error) {
|
|
// Remove the nodes created so far, last first. `delete` puts back a node
|
|
// it could not remove, so the first failed removal ends the rollback:
|
|
// what stays behind is then the start of the recipe rather than a
|
|
// scattered subset, and if the connection dropped the remaining
|
|
// requests would fail the same way.
|
|
const remaining = [...created]
|
|
while (remaining.length > 0) {
|
|
try {
|
|
await dispatch('delete', {
|
|
workflow,
|
|
nodeId: remaining[remaining.length - 1].id,
|
|
undoRedoActionGroupId,
|
|
})
|
|
} catch (_) {
|
|
break
|
|
}
|
|
remaining.pop()
|
|
}
|
|
if (remaining.length > 0) {
|
|
// The caller reports the original error; this says what it left in
|
|
// the workflow. Those nodes keep the attempt's undo group, so one undo
|
|
// still removes them.
|
|
dispatch(
|
|
'toast/warning',
|
|
{
|
|
title: this.$i18n.t('nodeRecipe.nodesLeftTitle'),
|
|
message: this.$i18n.t('nodeRecipe.nodesLeftMessage', {
|
|
recipe: recipe.name,
|
|
}),
|
|
},
|
|
{ root: true }
|
|
)
|
|
}
|
|
throw error
|
|
}
|
|
|
|
if (created.length > 0) {
|
|
setTimeout(() => {
|
|
const populatedNode = getters.findById(workflow, created[0].id)
|
|
dispatch('select', { workflow, node: populatedNode })
|
|
})
|
|
}
|
|
|
|
return created
|
|
},
|
|
/**
|
|
* Reloads one node from the server and applies it as a realtime update.
|
|
* Used when a websocket event was too large to carry the node's sample data
|
|
* and arrived with `requires_refresh` instead; the list endpoint is the only
|
|
* read endpoint for nodes, so the workflow's nodes are fetched and the one we
|
|
* need is picked out, leaving the selection and the other nodes untouched.
|
|
*/
|
|
async refetch({ dispatch, getters }, { workflow, nodeId }) {
|
|
const { data: nodes } = await AutomationWorkflowNodeService(
|
|
this.$client
|
|
).get(workflow.id)
|
|
const values = nodes.find((node) => node.id === nodeId)
|
|
const existing = getters.findById(workflow, nodeId)
|
|
if (!values || !existing) return null
|
|
await dispatch('forceUpdate', {
|
|
workflow,
|
|
node: existing,
|
|
values,
|
|
override: true,
|
|
viaRealtime: true,
|
|
})
|
|
return getters.findById(workflow, nodeId)
|
|
},
|
|
forceUpdate({ commit }, { workflow, node, values, override, viaRealtime }) {
|
|
commit('UPDATE_ITEM', {
|
|
workflow,
|
|
node,
|
|
values,
|
|
override,
|
|
viaRealtime,
|
|
})
|
|
},
|
|
/**
|
|
* Sends an update straight away. `updateDebounced` waits half a second after
|
|
* the last change so keystrokes batch into one request, which for a one-shot
|
|
* command such as regenerating the inbound email address is only a delay
|
|
* before the spinner and the request. There is nothing to show optimistically
|
|
* either: the server produces the result, so the node is updated from its
|
|
* response alone and the loading flag covers the whole round trip. Pending
|
|
* debounced changes are left untouched so the two paths stay independent.
|
|
*/
|
|
async update(
|
|
{ dispatch, commit },
|
|
{ workflow, node, values, undoRedoActionGroupId = null }
|
|
) {
|
|
commit('SET_LOADING', { node, value: true })
|
|
try {
|
|
const { data } = await AutomationWorkflowNodeService(this.$client).update(
|
|
node.id,
|
|
values,
|
|
getUndoRedoActionRequestConfig({ undoRedoActionGroupId })
|
|
)
|
|
// As in `updateDebounced`, the id is never written back.
|
|
delete data.id
|
|
await dispatch('forceUpdate', { workflow, node, values: data })
|
|
} finally {
|
|
commit('SET_LOADING', { node, value: false })
|
|
}
|
|
},
|
|
async updateDebounced(
|
|
{ dispatch, commit, getters },
|
|
{ workflow, node, values }
|
|
) {
|
|
// These values should not be updated via a regular update request
|
|
const excludeValues = ['id']
|
|
|
|
const oldValues = {}
|
|
Object.keys(values).forEach((name) => {
|
|
if (
|
|
Object.prototype.hasOwnProperty.call(node, name) &&
|
|
!excludeValues.includes(name)
|
|
) {
|
|
oldValues[name] = node[name]
|
|
// Accumulate the changed values to send all the ongoing changes with the
|
|
// final request. Use clone() to handle Vue 3 reactive objects safely.
|
|
updateContext.valuesToUpdate[name] = clone(values[name])
|
|
}
|
|
})
|
|
|
|
await dispatch('forceUpdate', {
|
|
workflow,
|
|
node,
|
|
values: updateContext.valuesToUpdate,
|
|
})
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const fire = async () => {
|
|
commit('SET_LOADING', { node, value: true })
|
|
const toUpdate = updateContext.valuesToUpdate
|
|
updateContext.valuesToUpdate = {}
|
|
try {
|
|
const { data } = await AutomationWorkflowNodeService(
|
|
this.$client
|
|
).update(node.id, toUpdate)
|
|
updateContext.lastUpdatedValues = null
|
|
|
|
excludeValues.forEach((name) => {
|
|
delete data[name]
|
|
})
|
|
|
|
await dispatch('forceUpdate', {
|
|
workflow,
|
|
node,
|
|
values: data,
|
|
})
|
|
|
|
resolve()
|
|
} catch (error) {
|
|
await dispatch('forceUpdate', {
|
|
workflow,
|
|
node,
|
|
values: updateContext.lastUpdatedValues,
|
|
})
|
|
updateContext.lastUpdatedValues = null
|
|
reject(error)
|
|
}
|
|
updateContext.lastUpdatedValues = null
|
|
commit('SET_LOADING', { node, value: false })
|
|
}
|
|
|
|
if (updateContext.promiseResolve) {
|
|
updateContext.promiseResolve()
|
|
updateContext.promiseResolve = null
|
|
}
|
|
|
|
clearTimeout(updateContext.updateTimeout)
|
|
|
|
if (!updateContext.lastUpdatedValues) {
|
|
updateContext.lastUpdatedValues = oldValues
|
|
}
|
|
|
|
updateContext.updateTimeout = setTimeout(fire, 500)
|
|
updateContext.promiseResolve = resolve
|
|
})
|
|
},
|
|
forceDelete({ commit, dispatch, getters }, { workflow, nodeId }) {
|
|
const node = getters.findById(workflow, nodeId)
|
|
if (!node) return
|
|
|
|
const nextNodes = getters.getNextNodes(workflow, node)
|
|
const nextNode = nextNodes.length > 0 ? nextNodes[0] : null
|
|
|
|
if (getters.getSelected(workflow)?.id === nodeId) {
|
|
dispatch('select', { workflow, node: null })
|
|
if (nextNode) {
|
|
dispatch('select', { workflow, node: nextNode })
|
|
}
|
|
}
|
|
|
|
commit('DELETE_ITEM', { workflow, nodeId })
|
|
},
|
|
async delete(
|
|
{ commit, dispatch, getters },
|
|
{ workflow, nodeId, undoRedoActionGroupId = null }
|
|
) {
|
|
const node = getters.findById(workflow, nodeId)
|
|
const originalNode = clone(node)
|
|
|
|
const initialGraph = clone(workflow.graph)
|
|
await dispatch('graphRemove', {
|
|
workflow,
|
|
node,
|
|
})
|
|
|
|
commit('DELETE_ITEM', { workflow, nodeId })
|
|
try {
|
|
await AutomationWorkflowNodeService(this.$client).delete(
|
|
nodeId,
|
|
getUndoRedoActionRequestConfig({ undoRedoActionGroupId })
|
|
)
|
|
} catch (error) {
|
|
// We restore the removed node
|
|
commit('ADD_ITEM', { workflow, node: originalNode })
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: { graph: initialGraph },
|
|
},
|
|
{ root: true }
|
|
)
|
|
throw error
|
|
}
|
|
},
|
|
async replace(
|
|
{ commit, dispatch, getters, rootGetters },
|
|
{ workflow, nodeId, newType }
|
|
) {
|
|
const nodeToReplace = getters.findById(workflow, nodeId)
|
|
const oldImmediateDispatch = workflow.immediate_dispatch
|
|
const nodeType = this.$registry.get('node', nodeToReplace.type)
|
|
|
|
if (nodeType.isTrigger) {
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: {
|
|
immediate_dispatch: getNodeTypeImmediateDispatch(
|
|
this.$registry,
|
|
newType
|
|
),
|
|
},
|
|
},
|
|
{ root: true }
|
|
)
|
|
}
|
|
|
|
let newNode
|
|
try {
|
|
const { data } = await AutomationWorkflowNodeService(
|
|
this.$client
|
|
).replace(nodeId, {
|
|
new_type: newType,
|
|
})
|
|
newNode = data
|
|
} catch (error) {
|
|
if (nodeType.isTrigger) {
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: { immediate_dispatch: oldImmediateDispatch },
|
|
},
|
|
{ root: true }
|
|
)
|
|
}
|
|
throw error
|
|
}
|
|
|
|
commit('ADD_ITEM', { workflow, node: newNode })
|
|
|
|
await dispatch('graphReplace', {
|
|
workflow,
|
|
nodeToReplace,
|
|
newNode,
|
|
})
|
|
|
|
commit('DELETE_ITEM', { workflow, nodeId })
|
|
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: { simulate_until_node_id: null },
|
|
},
|
|
{ root: true }
|
|
)
|
|
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: {
|
|
immediate_dispatch: getWorkflowImmediateDispatch(
|
|
this.$registry,
|
|
workflow
|
|
),
|
|
},
|
|
},
|
|
{ root: true }
|
|
)
|
|
|
|
setTimeout(() => {
|
|
dispatch('select', { workflow, node: newNode })
|
|
})
|
|
},
|
|
async move({ commit, dispatch, getters }, { workflow, moveData }) {
|
|
const { movedNodeId, referenceNodeId, position, output } = moveData
|
|
const movedNode = getters.findById(workflow, movedNodeId)
|
|
const referenceNode = getters.findById(workflow, referenceNodeId)
|
|
|
|
const [previousReferenceNode, previousPosition, previousOutput] =
|
|
new NodeGraphHandler(workflow).getNodePosition(movedNode)
|
|
|
|
dispatch('graphMove', {
|
|
workflow,
|
|
nodeToMove: movedNode,
|
|
referenceNode,
|
|
position,
|
|
output,
|
|
})
|
|
|
|
try {
|
|
// Perform the backend update.
|
|
await AutomationWorkflowNodeService(this.$client).move(movedNodeId, {
|
|
reference_node_id: referenceNodeId,
|
|
position,
|
|
output,
|
|
})
|
|
|
|
// A move can change a node's level or path, which may invalidate
|
|
// cross-node references. Let each node type reconcile the workflow, as
|
|
// the backend does after its own move.
|
|
dispatch('afterMove', { workflow })
|
|
} catch (error) {
|
|
// We revert the operation
|
|
dispatch('graphMove', {
|
|
workflow,
|
|
nodeToMove: movedNode,
|
|
referenceNode: previousReferenceNode,
|
|
position: previousPosition,
|
|
output: previousOutput,
|
|
})
|
|
|
|
throw error
|
|
}
|
|
},
|
|
/**
|
|
* Mirrors the backend, which calls `after_move` on every node type after a
|
|
* move: lets each node reconcile state the move may have invalidated (e.g. a
|
|
* "Go to node" whose destination now sits on a different branch or level).
|
|
*/
|
|
afterMove({ dispatch, getters }, { workflow }) {
|
|
for (const node of getters.getNodes(workflow)) {
|
|
const values = this.$registry
|
|
.get('node', node.type)
|
|
.afterMove({ workflow, node })
|
|
if (values) {
|
|
dispatch('forceUpdate', { workflow, node, values })
|
|
}
|
|
}
|
|
},
|
|
async duplicate({ commit, dispatch, getters }, { workflow, nodeId }) {
|
|
const nodeToDuplicate = getters.findById(workflow, nodeId)
|
|
if (!nodeToDuplicate) {
|
|
return
|
|
}
|
|
|
|
// Get the node type to properly initialize the node
|
|
const nodeType = this.$registry.get('node', nodeToDuplicate.type)
|
|
|
|
// Use getDefaultValues like in create, but override with duplicated node's data
|
|
const tempNode = nodeType.getDefaultValues({
|
|
...nodeToDuplicate, // Copy all properties from the original
|
|
id: uuid(), // But give it a new ID
|
|
workflow: workflow.id,
|
|
})
|
|
|
|
commit('ADD_ITEM', { workflow, node: tempNode })
|
|
|
|
const initialGraph = clone(workflow.graph)
|
|
|
|
// Insert the duplicated node after the original node using 'south' position
|
|
await dispatch('graphInsert', {
|
|
workflow,
|
|
node: tempNode,
|
|
referenceNode: nodeToDuplicate,
|
|
position: 'south',
|
|
output: '', // Default output for creating after a node
|
|
})
|
|
|
|
try {
|
|
const { data: node } = await AutomationWorkflowNodeService(
|
|
this.$client
|
|
).duplicate(nodeId)
|
|
|
|
commit('ADD_ITEM', { workflow, node })
|
|
|
|
await dispatch('graphReplace', {
|
|
workflow,
|
|
nodeToReplace: tempNode,
|
|
newNode: node,
|
|
})
|
|
|
|
// Remove temp node and add real one
|
|
commit('DELETE_ITEM', { workflow, nodeId: tempNode.id })
|
|
|
|
setTimeout(() => {
|
|
const populatedNode = getters.findById(workflow, node.id)
|
|
dispatch('select', { workflow, node: populatedNode })
|
|
})
|
|
|
|
return node
|
|
} catch (error) {
|
|
// If API fails, restore the initial graph
|
|
await dispatch(
|
|
'automationWorkflow/forceUpdate',
|
|
{
|
|
workflow,
|
|
values: { graph: initialGraph },
|
|
},
|
|
{ root: true }
|
|
)
|
|
commit('DELETE_ITEM', { workflow, nodeId: tempNode.id })
|
|
throw error
|
|
}
|
|
},
|
|
select({ commit, dispatch }, { workflow, node }) {
|
|
if (!workflow) {
|
|
return
|
|
}
|
|
commit('SELECT_ITEM', { workflow, node })
|
|
dispatch(
|
|
'automationWorkflow/setActiveSidePanel',
|
|
node ? NodeEditorSidePanelType.getType() : null,
|
|
{ root: true }
|
|
)
|
|
},
|
|
setDraggingNodeId({ commit }, nodeId) {
|
|
commit('SET_DRAGGING_NODE_ID', nodeId)
|
|
},
|
|
async simulateDispatch({ commit, dispatch }, { nodeId }) {
|
|
await AutomationWorkflowNodeService(this.$client).simulateDispatch(nodeId)
|
|
},
|
|
/**
|
|
* Updates all the next nodes of a given node with the provided values.
|
|
* This used when a node is replaced, or moved, as the next nodes need to
|
|
* be updated to reflect the new previous node id and output.
|
|
*/
|
|
_updateNextNodesValues(
|
|
{ commit, getters },
|
|
{ workflow, nodeId, valuesToUpdate, outputUid = null, parentNodeId = null }
|
|
) {
|
|
const node = getters.findById(workflow, nodeId)
|
|
const nextNodes = getters.getNextNodes(workflow, node, outputUid)
|
|
nextNodes.forEach((nextNode) => {
|
|
commit('UPDATE_ITEM', {
|
|
workflow,
|
|
node: nextNode,
|
|
values: valuesToUpdate,
|
|
})
|
|
})
|
|
},
|
|
/**
|
|
* Updates all the next nodes of a given node with the provided values.
|
|
* This used when a node is replaced, or moved, as the next nodes need to
|
|
* be updated to reflect the new previous node id and output.
|
|
*/
|
|
updateNextNodesValues(
|
|
{ commit, getters },
|
|
{
|
|
workflow,
|
|
nodeId = null,
|
|
parentNodeId = null,
|
|
valuesToUpdate,
|
|
outputUid = null,
|
|
}
|
|
) {
|
|
let nextNodes
|
|
if (nodeId) {
|
|
const node = getters.findById(workflow, nodeId)
|
|
nextNodes = getters.getNextNodes(workflow, node, outputUid)
|
|
} else {
|
|
const parentNode = getters.findById(workflow, parentNodeId)
|
|
nextNodes = getters.getChildren(workflow, parentNode)
|
|
}
|
|
nextNodes.forEach((nextNode) => {
|
|
commit('UPDATE_ITEM', {
|
|
workflow,
|
|
node: nextNode,
|
|
values: valuesToUpdate,
|
|
})
|
|
})
|
|
},
|
|
}
|
|
|
|
const getters = {
|
|
getNodes: (state) => (workflow) => {
|
|
if (!workflow) return []
|
|
return workflow.nodes
|
|
},
|
|
/**
|
|
* The nodes in the order they appear in the editor, top to bottom. `getNodes`
|
|
* returns them in creation order (the backend orders by id), which diverges
|
|
* from the graph as soon as a node is inserted between two others or moved.
|
|
*/
|
|
getNodesInOrder: (state) => (workflow) => {
|
|
if (!workflow) return []
|
|
return new NodeGraphHandler(workflow).getOrderedNodes()
|
|
},
|
|
findById: (state) => (workflow, nodeId) => {
|
|
if (!workflow || !workflow.nodes || !nodeId) return null
|
|
const nodeIdStr = nodeId.toString()
|
|
if (workflow.nodeMap && workflow.nodeMap[nodeIdStr]) {
|
|
return workflow.nodeMap[nodeIdStr]
|
|
}
|
|
return null
|
|
},
|
|
findByServiceId: (state, getters) => (workflow, serviceId) => {
|
|
if (!workflow || !serviceId) return null
|
|
return (
|
|
getters
|
|
.getNodes(workflow)
|
|
.find((node) => node.service?.id === serviceId) || null
|
|
)
|
|
},
|
|
getSelected: (state) => (workflow) => {
|
|
if (!workflow) return null
|
|
return workflow.nodeMap?.[workflow.selectedNodeId] || null
|
|
},
|
|
getLoading: (state) => (node) => {
|
|
return node._.loading
|
|
},
|
|
getDraggingNodeId(state) {
|
|
return state.draggingNodeId
|
|
},
|
|
/**
|
|
* Returns the immediate children of the given targetNode. For now we support only
|
|
* one child but may be later we can support more.
|
|
*/
|
|
getChildren: (state, getters) => (workflow, targetNode) => {
|
|
return new NodeGraphHandler(workflow).getChildren(targetNode)
|
|
},
|
|
getNextNodes:
|
|
(state, getters) =>
|
|
(workflow, targetNode, outputUid = null) => {
|
|
return new NodeGraphHandler(workflow).getNextNodes(targetNode, outputUid)
|
|
},
|
|
getAncestors: (state, getters) => (workflow, targetNode) => {
|
|
const positions = new NodeGraphHandler(workflow).getPreviousPositions(
|
|
targetNode
|
|
)
|
|
|
|
const parentNodes = positions
|
|
.filter(([, position]) => position === 'child')
|
|
.map(([prevNode]) => prevNode)
|
|
|
|
return parentNodes
|
|
},
|
|
getPreviousNodes:
|
|
(state, getters) =>
|
|
(
|
|
workflow,
|
|
targetNode,
|
|
{ targetFirst = false, includeSelf = false, predicate = () => true } = {}
|
|
) => {
|
|
const positions = new NodeGraphHandler(workflow).getPreviousPositions(
|
|
targetNode
|
|
)
|
|
|
|
const previousNodes = positions
|
|
.filter((position) => predicate(...position))
|
|
.map(([prevNode]) => prevNode)
|
|
.filter((node) => node)
|
|
|
|
const previous = includeSelf
|
|
? [...previousNodes, targetNode]
|
|
: previousNodes
|
|
return targetFirst ? previous.reverse() : previous
|
|
},
|
|
}
|
|
|
|
export default {
|
|
namespaced: true,
|
|
state,
|
|
getters,
|
|
actions,
|
|
mutations,
|
|
}
|