2023-03-30 23:49:57 +08:00
|
|
|
import { useCollectionManager, useCompile } from '@nocobase/client';
|
|
|
|
import { useFlowContext } from './FlowContext';
|
|
|
|
import { NAMESPACE } from './locale';
|
2023-05-16 09:45:45 +08:00
|
|
|
import { instructions, useAvailableUpstreams, useNodeContext, useUpstreamScopes } from './nodes';
|
2023-03-30 23:49:57 +08:00
|
|
|
import { triggers } from './triggers';
|
2023-02-20 11:52:06 +08:00
|
|
|
|
2023-03-10 16:36:58 +08:00
|
|
|
export type VariableOption = {
|
2023-04-08 10:52:31 +08:00
|
|
|
key?: string;
|
2023-03-10 16:36:58 +08:00
|
|
|
value: string;
|
|
|
|
label: string;
|
2023-04-08 10:52:31 +08:00
|
|
|
children?: VariableOptions;
|
2023-03-10 16:36:58 +08:00
|
|
|
};
|
2023-02-20 11:52:06 +08:00
|
|
|
|
2023-04-08 10:52:31 +08:00
|
|
|
export type VariableOptions = VariableOption[] | null;
|
|
|
|
|
2023-05-16 09:45:45 +08:00
|
|
|
export const VariableTypes = [
|
|
|
|
{
|
|
|
|
title: `{{t("Scope variables", { ns: "${NAMESPACE}" })}}`,
|
|
|
|
value: '$scopes',
|
|
|
|
useOptions(current, types) {
|
|
|
|
const scopes = useUpstreamScopes(current);
|
|
|
|
const options: VariableOption[] = [];
|
|
|
|
scopes.forEach((node) => {
|
|
|
|
const instruction = instructions.get(node.type);
|
|
|
|
const subOptions = instruction.useScopeVariables?.(node, types);
|
|
|
|
if (subOptions) {
|
|
|
|
options.push({
|
|
|
|
key: node.id.toString(),
|
|
|
|
value: node.id.toString(),
|
|
|
|
label: node.title ?? `#${node.id}`,
|
|
|
|
children: subOptions,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return options;
|
|
|
|
},
|
|
|
|
},
|
2023-02-20 11:52:06 +08:00
|
|
|
{
|
|
|
|
title: `{{t("Node result", { ns: "${NAMESPACE}" })}}`,
|
|
|
|
value: '$jobsMapByNodeId',
|
2023-05-16 09:45:45 +08:00
|
|
|
useOptions(current, types) {
|
2023-02-20 11:52:06 +08:00
|
|
|
const upstreams = useAvailableUpstreams(current);
|
|
|
|
const options: VariableOption[] = [];
|
|
|
|
upstreams.forEach((node) => {
|
|
|
|
const instruction = instructions.get(node.type);
|
2023-05-16 09:45:45 +08:00
|
|
|
const subOptions = instruction.useVariables?.(node, types);
|
2023-02-20 11:52:06 +08:00
|
|
|
if (subOptions) {
|
|
|
|
options.push({
|
|
|
|
key: node.id.toString(),
|
|
|
|
value: node.id.toString(),
|
|
|
|
label: node.title ?? `#${node.id}`,
|
|
|
|
children: subOptions,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return options;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: `{{t("Trigger variables", { ns: "${NAMESPACE}" })}}`,
|
|
|
|
value: '$context',
|
2023-05-16 09:45:45 +08:00
|
|
|
useOptions(current, types) {
|
2023-02-20 11:52:06 +08:00
|
|
|
const { workflow } = useFlowContext();
|
|
|
|
const trigger = triggers.get(workflow.type);
|
|
|
|
return trigger?.getOptions?.(workflow.config, types) ?? null;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: `{{t("System variables", { ns: "${NAMESPACE}" })}}`,
|
|
|
|
value: '$system',
|
2023-05-16 09:45:45 +08:00
|
|
|
useOptions(current, types) {
|
2023-04-08 10:52:31 +08:00
|
|
|
return [
|
2023-04-25 13:12:14 +08:00
|
|
|
...(!types || types.includes('date')
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
key: 'now',
|
|
|
|
value: 'now',
|
|
|
|
label: `{{t("System time")}}`,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: []),
|
2023-04-08 10:52:31 +08:00
|
|
|
];
|
2023-04-25 13:12:14 +08:00
|
|
|
},
|
2023-03-30 23:49:57 +08:00
|
|
|
},
|
2023-02-20 11:52:06 +08:00
|
|
|
];
|
|
|
|
|
2023-04-08 10:52:31 +08:00
|
|
|
export const BaseTypeSets = {
|
|
|
|
boolean: new Set(['checkbox']),
|
|
|
|
number: new Set(['number', 'percent']),
|
2023-04-25 13:12:14 +08:00
|
|
|
string: new Set([
|
|
|
|
'input',
|
|
|
|
'password',
|
|
|
|
'email',
|
|
|
|
'phone',
|
|
|
|
'select',
|
|
|
|
'radioGroup',
|
|
|
|
'text',
|
|
|
|
'markdown',
|
|
|
|
'richText',
|
|
|
|
'expression',
|
|
|
|
'time',
|
|
|
|
]),
|
2023-04-08 10:52:31 +08:00
|
|
|
date: new Set(['date', 'createdAt', 'updatedAt']),
|
2023-03-30 23:49:57 +08:00
|
|
|
};
|
2023-02-20 11:52:06 +08:00
|
|
|
|
2023-04-08 10:52:31 +08:00
|
|
|
// { type: 'reference', options: { collection: 'users', multiple: false } }
|
|
|
|
// { type: 'reference', options: { collection: 'attachments', multiple: false } }
|
|
|
|
// { type: 'reference', options: { collection: 'myExpressions', entity: false } }
|
|
|
|
|
2023-04-25 13:12:14 +08:00
|
|
|
function matchFieldType(field, type): boolean {
|
2023-04-08 10:52:31 +08:00
|
|
|
const inputType = typeof type;
|
|
|
|
if (inputType === 'string') {
|
2023-05-08 13:50:43 +08:00
|
|
|
return BaseTypeSets[type]?.has(field.interface);
|
2023-02-20 11:52:06 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 10:52:31 +08:00
|
|
|
if (inputType === 'object' && type.type === 'reference') {
|
|
|
|
if (isAssociationField(field)) {
|
2023-04-25 13:12:14 +08:00
|
|
|
return (
|
|
|
|
type.options?.entity && (field.collectionName === type.options?.collection || type.options?.collection === '*')
|
|
|
|
);
|
2023-04-08 10:52:31 +08:00
|
|
|
} else if (field.isForeignKey) {
|
|
|
|
return (
|
|
|
|
(field.collectionName === type.options?.collection && field.name === 'id') ||
|
2023-04-25 13:12:14 +08:00
|
|
|
field.target === type.options?.collection
|
2023-04-08 10:52:31 +08:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputType === 'function') {
|
|
|
|
return type(field);
|
2023-02-20 11:52:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-08 10:52:31 +08:00
|
|
|
function isAssociationField(field): boolean {
|
|
|
|
return ['belongsTo', 'hasOne', 'hasMany', 'belongsToMany'].includes(field.type);
|
2023-02-20 11:52:06 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 10:52:31 +08:00
|
|
|
export function filterTypedFields(fields, types, depth = 1) {
|
|
|
|
if (!types) {
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
return fields.filter((field) => {
|
2023-04-25 13:12:14 +08:00
|
|
|
if (
|
|
|
|
isAssociationField(field) &&
|
|
|
|
depth &&
|
|
|
|
filterTypedFields(useNormalizedFields(field.target), types, depth - 1).length
|
|
|
|
) {
|
2023-04-08 10:52:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return types.some((type) => matchFieldType(field, type));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useWorkflowVariableOptions(types?) {
|
2023-02-20 11:52:06 +08:00
|
|
|
const compile = useCompile();
|
2023-05-16 09:45:45 +08:00
|
|
|
const current = useNodeContext();
|
2023-02-20 11:52:06 +08:00
|
|
|
const options = VariableTypes.map((item: any) => {
|
2023-05-16 09:45:45 +08:00
|
|
|
const opts = typeof item.useOptions === 'function' ? item.useOptions(current, types).filter(Boolean) : null;
|
2023-02-20 11:52:06 +08:00
|
|
|
return {
|
|
|
|
label: compile(item.title),
|
|
|
|
value: item.value,
|
|
|
|
key: item.value,
|
2023-04-08 10:52:31 +08:00
|
|
|
children: compile(opts),
|
|
|
|
disabled: opts && !opts.length,
|
2023-02-20 11:52:06 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2023-04-21 10:52:13 +08:00
|
|
|
function useNormalizedFields(collectionName) {
|
2023-04-08 10:52:31 +08:00
|
|
|
const compile = useCompile();
|
|
|
|
const { getCollection } = useCollectionManager();
|
2023-04-08 15:20:33 +08:00
|
|
|
const collection = getCollection(collectionName);
|
2023-04-08 10:52:31 +08:00
|
|
|
if (!collection) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-04-08 15:20:33 +08:00
|
|
|
const { fields } = collection;
|
2023-04-08 10:52:31 +08:00
|
|
|
const foreignKeyFields: any[] = [];
|
|
|
|
const otherFields: any[] = [];
|
2023-04-25 13:12:14 +08:00
|
|
|
fields.forEach((field) => {
|
2023-04-08 10:52:31 +08:00
|
|
|
if (field.isForeignKey) {
|
|
|
|
foreignKeyFields.push(field);
|
|
|
|
} else {
|
|
|
|
otherFields.push(field);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (let i = otherFields.length - 1; i >= 0; i--) {
|
|
|
|
const field = otherFields[i];
|
|
|
|
if (field.type === 'belongsTo') {
|
2023-04-25 13:12:14 +08:00
|
|
|
const foreignKeyField = foreignKeyFields.find((f) => f.name === field.foreignKey);
|
2023-04-08 10:52:31 +08:00
|
|
|
if (foreignKeyField) {
|
|
|
|
otherFields.splice(i, 0, {
|
2023-04-21 10:52:13 +08:00
|
|
|
...field,
|
2023-04-08 10:52:31 +08:00
|
|
|
...foreignKeyField,
|
|
|
|
uiSchema: {
|
|
|
|
...field.uiSchema,
|
|
|
|
title: field.uiSchema?.title ? `${compile(field.uiSchema?.title)} ID` : foreignKeyField.name,
|
2023-04-25 13:12:14 +08:00
|
|
|
},
|
2023-04-08 10:52:31 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
otherFields.splice(i, 0, {
|
|
|
|
...field,
|
|
|
|
name: field.foreignKey,
|
|
|
|
type: 'bigInt',
|
|
|
|
isForeignKey: true,
|
|
|
|
interface: field.interface,
|
|
|
|
uiSchema: {
|
|
|
|
...field.uiSchema,
|
|
|
|
title: field.uiSchema?.title ? `${compile(field.uiSchema?.title)} ID` : field.name,
|
2023-04-25 13:12:14 +08:00
|
|
|
},
|
2023-04-08 10:52:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (field.type === 'context' && field.collectionName === 'users') {
|
2023-04-25 13:12:14 +08:00
|
|
|
const belongsToField =
|
|
|
|
otherFields.find((f) => f.type === 'belongsTo' && f.target === 'users' && f.foreignKey === field.name) ?? {};
|
2023-04-08 10:52:31 +08:00
|
|
|
otherFields.splice(i, 0, {
|
|
|
|
...field,
|
|
|
|
type: field.dataType,
|
|
|
|
interface: belongsToField.interface,
|
|
|
|
uiSchema: {
|
|
|
|
...belongsToField.uiSchema,
|
2023-04-25 13:12:14 +08:00
|
|
|
title: belongsToField.uiSchema?.title ? `${compile(belongsToField.uiSchema?.title)} ID` : field.name,
|
|
|
|
},
|
2023-04-08 10:52:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 13:12:14 +08:00
|
|
|
return otherFields.filter((field) => field.interface && !field.hidden);
|
2023-04-02 13:24:25 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 10:52:31 +08:00
|
|
|
export function useCollectionFieldOptions(options): VariableOption[] {
|
|
|
|
const { fields, collection, types, depth = 1 } = options;
|
2023-04-02 13:24:25 +08:00
|
|
|
const compile = useCompile();
|
2023-05-16 09:45:45 +08:00
|
|
|
const normalizedFields = useNormalizedFields(collection);
|
|
|
|
const computedFields = fields ?? normalizedFields;
|
|
|
|
const result: VariableOption[] = filterTypedFields(computedFields, types, depth)
|
2023-04-25 13:12:14 +08:00
|
|
|
.filter((field) => !isAssociationField(field) || depth)
|
|
|
|
.map((field) => {
|
2023-04-02 13:24:25 +08:00
|
|
|
const label = compile(field.uiSchema?.title || field.name);
|
2023-04-08 10:52:31 +08:00
|
|
|
return {
|
2023-04-02 13:24:25 +08:00
|
|
|
label,
|
|
|
|
key: field.name,
|
|
|
|
value: field.name,
|
2023-04-25 13:12:14 +08:00
|
|
|
children:
|
|
|
|
isAssociationField(field) && depth
|
|
|
|
? useCollectionFieldOptions({ collection: field.target, types, depth: depth - 1 })
|
|
|
|
: null,
|
2023-04-08 10:52:31 +08:00
|
|
|
};
|
2023-04-02 13:24:25 +08:00
|
|
|
});
|
2023-03-10 16:36:58 +08:00
|
|
|
|
|
|
|
return result;
|
2023-02-20 11:52:06 +08:00
|
|
|
}
|