@nocobase/flow-engine
Version:
A standalone flow engine for NocoBase, managing workflows, models, and actions.
243 lines (241 loc) • 9.17 kB
JavaScript
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var CollectionFieldModel_exports = {};
__export(CollectionFieldModel_exports, {
CollectionFieldModel: () => CollectionFieldModel,
FieldPlaceholder: () => FieldPlaceholder
});
module.exports = __toCommonJS(CollectionFieldModel_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_utils = require("../utils");
var import_flowModel = require("./flowModel");
function FieldPlaceholder() {
return /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, null, /* @__PURE__ */ import_react.default.createElement(
import_antd.Card,
{
size: "small",
styles: {
body: {
color: "rgba(0,0,0,0.45)"
}
}
},
"\u8BE5\u5B57\u6BB5\u5DF2\u88AB\u9690\u85CF\uFF0C\u4F60\u65E0\u6CD5\u67E5\u770B\uFF08\u8BE5\u5185\u5BB9\u4EC5\u5728\u6FC0\u6D3B UI Editor \u65F6\u663E\u793A\uFF09\u3002"
));
}
__name(FieldPlaceholder, "FieldPlaceholder");
const defaultWhen = /* @__PURE__ */ __name(() => true, "defaultWhen");
const _CollectionFieldModel = class _CollectionFieldModel extends import_flowModel.FlowModel {
renderHiddenInConfig() {
return /* @__PURE__ */ import_react.default.createElement(FieldPlaceholder, null);
}
get title() {
return void 0;
}
onInit(options) {
this.context.defineProperty("collectionField", {
get: /* @__PURE__ */ __name(() => {
const params = this.getFieldSettingsInitParams();
const collectionField = this.context.dataSourceManager.getCollectionField(
`${params.dataSourceKey}.${params.collectionName}.${params.fieldPath}`
);
return collectionField;
}, "get"),
cache: false
});
this.context.defineProperty("fieldPath", {
get: /* @__PURE__ */ __name(() => {
return this.fieldPath;
}, "get")
});
this.context.blockModel.addAppends(this.fieldPath);
this.context.blockModel.addAppends(this.associationPathName);
}
getFieldSettingsInitParams() {
return this.getStepParams("fieldSettings", "init");
}
get fieldPath() {
return this.getFieldSettingsInitParams().fieldPath;
}
get associationPathName() {
return this.getFieldSettingsInitParams().associationPathName;
}
get collectionField() {
return this.context.collectionField;
}
async afterAddAsSubModel() {
if (this.context.resource) {
await this.context.resource.refresh();
}
}
static getBindingsByField(ctx, collectionField) {
const interfaceName = collectionField.interface;
if (!this.bindings.has(interfaceName)) {
return [];
}
const bindings = this.bindings.get(interfaceName);
return bindings.filter(
(binding) => ctx.engine.getModelClass(binding.modelName) && binding.when(ctx, collectionField)
);
}
static getDefaultBindingByField(ctx, collectionField, options = {}) {
if (options.fallbackToTargetTitleField) {
const binding = this.getDefaultBindingByField(ctx, collectionField, { useStrict: true });
if (!binding) {
if (collectionField.isAssociationField() && collectionField.targetCollectionTitleField) {
return this.getDefaultBindingByField(ctx, collectionField.targetCollectionTitleField);
}
}
return binding;
}
const interfaceName = collectionField.interface;
if (!interfaceName) {
return null;
}
if (!this.bindings.has(interfaceName)) {
return null;
}
const bindings = this.bindings.get(interfaceName);
const defaultBindings = bindings.filter(
(binding) => binding.isDefault && ctx.engine.getModelClass(binding.modelName) && binding.when(ctx, collectionField)
);
if (defaultBindings.length === 1) {
return defaultBindings[0];
}
if (defaultBindings.length > 0) {
let defaultBinding = null;
defaultBinding = defaultBindings.find((binding) => binding.when !== defaultWhen);
if (defaultBinding) {
return defaultBinding;
}
defaultBinding = defaultBindings.find((binding) => binding.when === defaultWhen);
if (defaultBinding) {
return defaultBinding;
}
}
if (options.useStrict) {
return null;
}
return bindings.find(
(binding) => ctx.engine.getModelClass(binding.modelName) && binding.when(ctx, collectionField)
);
}
static bindModelToInterface(modelName, interfaceName, options = {}) {
if (Array.isArray(interfaceName)) {
interfaceName.forEach((name) => this.bindModelToInterface(modelName, name, options));
return;
}
if (!this.currentBindings.has(interfaceName)) {
this.currentBindings.set(interfaceName, []);
}
const bindings = this.currentBindings.get(interfaceName);
bindings.push({
modelName,
isDefault: options.isDefault || false,
defaultProps: options.defaultProps || null,
when: options.when || defaultWhen
});
this.currentBindings.set(interfaceName, bindings);
}
static get currentBindings() {
if (!Object.prototype.hasOwnProperty.call(this, "_bindings") || !this._bindings) {
this._bindings = /* @__PURE__ */ new Map();
}
return this._bindings;
}
static getAllParentClasses() {
const parentClasses = [];
let currentClass = this;
while (currentClass && currentClass !== Object) {
currentClass = Object.getPrototypeOf(currentClass);
if (currentClass == null ? void 0 : currentClass.currentBindings) {
parentClasses.push(currentClass);
}
}
return parentClasses;
}
static get bindings() {
const allBindings = /* @__PURE__ */ new Map();
const allParentClasses = this.getAllParentClasses();
for (const parentClass of allParentClasses) {
for (const [interfaceName, binding] of parentClass.currentBindings) {
if (!allBindings.has(interfaceName)) {
allBindings.set(interfaceName, []);
}
allBindings.get(interfaceName).unshift(...binding);
}
}
for (const [interfaceName, binding] of this.currentBindings) {
if (!allBindings.has(interfaceName)) {
allBindings.set(interfaceName, []);
}
allBindings.get(interfaceName).unshift(...binding);
}
return allBindings;
}
};
__name(_CollectionFieldModel, "CollectionFieldModel");
__publicField(_CollectionFieldModel, "_bindings", /* @__PURE__ */ new Map());
let CollectionFieldModel = _CollectionFieldModel;
CollectionFieldModel.registerFlow({
key: "fieldSettings",
title: (0, import_utils.escapeT)("Field settings"),
steps: {
init: {
handler(ctx, params) {
const { dataSourceKey, collectionName, fieldPath } = params;
if (!dataSourceKey) {
throw new Error("dataSourceKey is a required parameter");
}
if (!collectionName) {
throw new Error("collectionName is a required parameter");
}
if (!fieldPath) {
throw new Error("fieldPath is a required parameter");
}
}
}
}
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CollectionFieldModel,
FieldPlaceholder
});