2023-11-03 20:08:11 +08:00
import React from 'react' ;
2023-05-16 09:45:45 +08:00
import { ArrowUpOutlined } from '@ant-design/icons' ;
2023-11-03 20:08:11 +08:00
2023-07-07 14:35:22 +08:00
import { css , cx , useCompile } from '@nocobase/client' ;
2023-11-03 20:08:11 +08:00
2023-11-07 22:17:07 +08:00
import {
2023-12-07 21:46:58 +08:00
NodeDefaultView ,
Branch ,
useFlowContext ,
useStyles ,
2023-11-07 22:17:07 +08:00
VariableOption ,
WorkflowVariableInput ,
defaultFieldNames ,
nodesOptions ,
scopeOptions ,
triggerOptions ,
2023-12-07 21:46:58 +08:00
Instruction ,
} from '@nocobase/plugin-workflow/client' ;
import { NAMESPACE , useLang } from '../locale' ;
2023-05-16 09:45:45 +08:00
function findOption ( options : VariableOption [ ] , paths : string [ ] ) {
2023-07-07 22:51:44 +08:00
let opts = options ;
let option = null ;
2023-05-16 09:45:45 +08:00
for ( let i = 0 ; i < paths . length ; i ++ ) {
const path = paths [ i ] ;
2023-07-07 22:51:44 +08:00
const current = opts . find ( ( item ) = > item . value === path ) ;
if ( ! current ) {
2023-09-12 14:29:05 +08:00
return null ;
2023-05-16 09:45:45 +08:00
}
2023-07-07 22:51:44 +08:00
option = current ;
2023-09-12 14:29:05 +08:00
if ( ! current . isLeaf && current . loadChildren ) {
current . loadChildren ( current ) ;
}
2023-07-07 22:51:44 +08:00
if ( current . children ) {
opts = current . children ;
2023-05-16 09:45:45 +08:00
}
}
2023-07-07 22:51:44 +08:00
return option ;
2023-05-16 09:45:45 +08:00
}
2023-12-07 21:46:58 +08:00
export default class extends Instruction {
title = ` {{t("Loop", { ns: " ${ NAMESPACE } " })}} ` ;
type = 'loop' ;
group = 'control' ;
description = ` {{t("By using a loop node, you can perform the same operation on multiple sets of data. The source of these sets can be either multiple records from a query node or multiple associated records of a single record. Loop node can also be used for iterating a certain number of times or for looping through each character in a string. However, excessive looping may cause performance issues, so use with caution.", { ns: " ${ NAMESPACE } " })}} ` ;
fieldset = {
2023-05-16 09:45:45 +08:00
target : {
type : 'string' ,
title : ` {{t("Loop target", { ns: " ${ NAMESPACE } " })}} ` ,
2023-05-18 19:42:30 +08:00
description : ` {{t("A single number will be treated as a loop count, a single string will be treated as an array of characters, and other non-array values will be converted to arrays. The loop node ends when the loop count is reached, or when the array loop is completed. You can also add condition nodes to the loop to terminate it.", { ns: " ${ NAMESPACE } " })}} ` ,
2023-05-16 09:45:45 +08:00
'x-decorator' : 'FormItem' ,
2023-11-03 20:08:11 +08:00
'x-component' : 'WorkflowVariableInput' ,
2023-05-16 09:45:45 +08:00
'x-component-props' : {
2023-07-05 22:01:41 +08:00
changeOnSelect : true ,
2023-05-16 09:45:45 +08:00
useTypedConstant : [ 'string' , 'number' , 'null' ] ,
2023-07-05 22:01:41 +08:00
className : css `
width : 100 % ;
. variable {
flex : 1 ;
}
. ant - input . null - value {
width : 100 % ;
}
` ,
2023-05-16 09:45:45 +08:00
} ,
required : true ,
} ,
2023-12-07 21:46:58 +08:00
} ;
components = {
WorkflowVariableInput ,
} ;
Component ( { data } ) {
// eslint-disable-next-line react-hooks/rules-of-hooks
2023-05-16 09:45:45 +08:00
const { nodes } = useFlowContext ( ) ;
2023-12-07 21:46:58 +08:00
// eslint-disable-next-line react-hooks/rules-of-hooks
2023-07-16 12:46:25 +08:00
const { styles } = useStyles ( ) ;
2023-05-16 09:45:45 +08:00
const entry = nodes . find ( ( node ) = > node . upstreamId === data . id && node . branchIndex != null ) ;
return (
< NodeDefaultView data = { data } >
2023-07-18 12:50:24 +08:00
< div className = { styles . nodeSubtreeClass } >
2023-05-16 09:45:45 +08:00
< div
className = { cx (
2023-07-16 12:46:25 +08:00
styles . branchBlockClass ,
2023-05-16 09:45:45 +08:00
css `
padding - left : 20em ;
` ,
) }
>
< Branch from = { data } entry = { entry } branchIndex = { entry ? . branchIndex ? ? 0 } / >
2023-07-18 12:50:24 +08:00
< div className = { styles . branchClass } >
2023-05-16 09:45:45 +08:00
< div className = "workflow-branch-lines" / >
2023-07-16 12:46:25 +08:00
< div className = { cx ( styles . addButtonClass , styles . loopLineClass ) } >
< ArrowUpOutlined / >
2023-05-16 09:45:45 +08:00
< / div >
< / div >
< / div >
< / div >
< / NodeDefaultView >
) ;
2023-12-07 21:46:58 +08:00
}
2023-05-17 17:29:18 +08:00
useScopeVariables ( node , options ) {
2023-12-07 21:46:58 +08:00
// eslint-disable-next-line react-hooks/rules-of-hooks
2023-05-16 09:45:45 +08:00
const compile = useCompile ( ) ;
2023-12-07 21:46:58 +08:00
// eslint-disable-next-line react-hooks/rules-of-hooks
const langLoopTarget = useLang ( 'Loop target' ) ;
// eslint-disable-next-line react-hooks/rules-of-hooks
const langLoopIndex = useLang ( 'Loop index' ) ;
// eslint-disable-next-line react-hooks/rules-of-hooks
const langLoopLength = useLang ( 'Loop length' ) ;
2023-05-16 09:45:45 +08:00
const { target } = node . config ;
if ( ! target ) {
return null ;
}
2023-07-18 12:50:24 +08:00
const { fieldNames = defaultFieldNames } = options ;
2023-05-16 09:45:45 +08:00
// const { workflow } = useFlowContext();
// const current = useNodeContext();
// const upstreams = useAvailableUpstreams(current);
// find target data model by path described in `config.target`
2023-10-25 23:14:54 +08:00
// 1. get options from $context/$jobsMapByNodeKey
2023-05-16 09:45:45 +08:00
// 2. route to sub-options and use as loop target options
2023-09-12 14:29:05 +08:00
let targetOption : VariableOption = {
key : 'item' ,
[ fieldNames . value ] : 'item' ,
2023-12-07 21:46:58 +08:00
[ fieldNames . label ] : langLoopTarget ,
2023-09-12 14:29:05 +08:00
} ;
2023-05-16 09:45:45 +08:00
if ( typeof target === 'string' && target . startsWith ( '{{' ) && target . endsWith ( '}}' ) ) {
const paths = target
. slice ( 2 , - 2 )
. split ( '.' )
. map ( ( path ) = > path . trim ( ) ) ;
2023-11-07 22:17:07 +08:00
const targetOptions = [ scopeOptions , nodesOptions , triggerOptions ] . map ( ( item : any ) = > {
const opts = item . useOptions ( { . . . options , current : node } ) . filter ( Boolean ) ;
2023-05-17 13:37:03 +08:00
return {
2023-09-12 14:29:05 +08:00
[ fieldNames . label ] : compile ( item . label ) ,
2023-07-18 12:50:24 +08:00
[ fieldNames . value ] : item . value ,
2023-05-17 13:37:03 +08:00
key : item.value ,
2023-07-18 12:50:24 +08:00
[ fieldNames . children ] : opts ,
2023-05-17 13:37:03 +08:00
disabled : opts && ! opts . length ,
} ;
} ) ;
2023-05-16 09:45:45 +08:00
2023-07-07 22:51:44 +08:00
const found = findOption ( targetOptions , paths ) ;
2023-09-12 14:29:05 +08:00
targetOption = Object . assign ( { } , found , targetOption ) ;
2023-05-16 09:45:45 +08:00
}
return [
targetOption ,
2023-12-07 21:46:58 +08:00
{ key : 'index' , [ fieldNames . value ] : 'index' , [ fieldNames . label ] : langLoopIndex } ,
{ key : 'length' , [ fieldNames . value ] : 'length' , [ fieldNames . label ] : langLoopLength } ,
2023-05-16 09:45:45 +08:00
] ;
2023-12-07 21:46:58 +08:00
}
}