onChange({ value: v, type, options })}/>;
// },
// default: new Date()
// }
};
export const VariableTypes = {
constant: {
title: '{{t("Constant")}}',
value: 'constant',
options: Object.values(ConstantTypes).map(item => ({
value: item.value,
label: item.title
})),
component({ options = { type: 'string' } }) {
return ConstantTypes[options.type]?.component ?? NullRender;
},
appendTypeValue({ options = { type: 'string' } }) {
return options?.type ? [options.type] : [];
},
onTypeChange(old, [type, optionsType], onChange) {
if (old?.options?.type === optionsType) {
return;
}
const { default: value } = ConstantTypes[optionsType];
onChange({
value,
type,
options: { ...old.options, type: optionsType }
});
},
parse(path) {
return { path };
}
},
$jobsMapByNodeId: {
title: '{{t("Node result")}}',
value: '$jobsMapByNodeId',
options() {
const node = useNodeContext();
const stack = [];
for (let current = node.upstream; current; current = current.upstream) {
const { getter } = instructions.get(current.type);
// Note: consider `getter` as the key of a value available node
if (getter) {
stack.push({
value: current.id,
label: current.title ?? `#${current.id}`
});
}
}
return stack;
},
component({ options }) {
const { nodes } = useFlowContext();
if (!options?.nodeId) {
return NullRender;
}
const node = nodes.find(n => n.id == options.nodeId);
if (!node) {
return NullRender;
}
const instruction = instructions.get(node.type);
return instruction?.getter ?? NullRender;
},
appendTypeValue({ options = {} }: { type: string, options: any }) {
return options.nodeId ? [Number.parseInt(options.nodeId, 10)] : [];
},
onTypeChange(old, [type, nodeId], onChange) {
onChange({
// ...old,
type,
options: { nodeId }
});
},
parse([nodeId, ...path]) {
return { nodeId, path: path.join('.') };
},
stringify({ options }) {
const stack = ['$jobsMapByNodeId'];
if (options.nodeId) {
stack.push(options.nodeId);
if (options.path) {
stack.push(options.path);
}
}
return `{{${stack.join('.')}}}`;
}
},
$context: {
title: '{{t("Trigger context")}}',
value: '$context',
component() {
const { workflow } = useFlowContext();
const trigger = triggers.get(workflow.type);
return trigger?.getter ?? NullRender;
},
parse([prefix, ...path]) {
return { path: path.join('.') };
},
stringify({ options }) {
const stack = ['$context'];
if (options?.path) {
stack.push(options.path);
}
return `{{${stack.join('.')}}}`;
}
},
// calculation: Calculation
};
export const VariableTypesContext = React.createContext(null);
export function useVariableTypes() {
return React.useContext(VariableTypesContext);
}
interface OperandProps {
value: {
type: string;
value?: any;
options?: any;
};
onChange(v: any): void;
children?: React.ReactNode;
}
export function Operand({
value: operand = { type: 'constant', value: '', options: { type: 'string' } },
onChange,
children
}: OperandProps) {
const compile = useCompile();
const Types = useVariableTypes();
const { type } = operand;
const { component, appendTypeValue } = Types[type] || {};
const VariableComponent = typeof component === 'function' ? component(operand) : NullRender;
return (
{
const options = typeof item.options === 'function' ? item.options() : item.options;
return {
label: compile(item.title),
value: item.value,
children: compile(options),
disabled: options && !options.length,
isLeaf: !options
};
})}
onChange={(next: Array) => {
const { onTypeChange } = Types[next[0]];
if (typeof onTypeChange === 'function') {
onTypeChange(operand, next, onChange);
} else {
if (next[0] !== type) {
onChange({ type: next[0], value: null });
}
}
}}
/>
{children ?? onChange({ ...op })} />}
);
}
export function Calculation({ calculator, operands = [], onChange }) {
const compile = useCompile();
return (
onChange({ calculator, operands: [v, operands[1]] }))} />
{operands[0]
? (
<>
onChange({ calculator, operands: [operands[0], v] }))} />
>
)
: null
}
);
}
export function VariableComponent({ value, onChange, renderSchemaComponent }) {
const VTypes = { ...VariableTypes,
constant: {
title: '{{t("Constant")}}',
value: 'constant',
options: undefined
}
};
const operand = typeof value === 'string'
? parseStringValue(value, VTypes)
: { type: 'constant', value };
return (
{
if (next.type !== operand.type && next.type === 'constant') {
onChange(null);
} else {
const { stringify } = VTypes[next.type];
onChange(stringify(next));
}
}}
>
{operand.type === 'constant' ? renderSchemaComponent() : null}
);
}
// NOTE: observer for watching useProps
export const CollectionFieldset = observer(({ value, onChange }: any) => {
const { t } = useTranslation();
const compile = useCompile();
const { getCollectionFields } = useCollectionManager();
const { values: data } = useForm();
const fields = getCollectionFields(data?.config?.collection)
.filter(field => (
!field.hidden
&& (field.uiSchema ? !field.uiSchema['x-read-pretty'] : false)
));
const VTypes = {
...VariableTypes,
constant: {
title: '{{t("Constant")}}',
value: 'constant',
options: undefined
}
};
return (
);
});