@farris/renderer
Version:
Render schema to web page with farris ui.
1,462 lines (1,461 loc) • 352 kB
JavaScript
(function(global, factory) {
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("lodash-es"), require("@farris/devkit-vue"), require("@farris/command-services-vue"), require("vue"), require("@farris/ba-command-services-vue"), require("@farris/ui-vue"), require("@farris/ui-binding-vue"), require("@farris/bef-vue"), require("@farris/charts-vue")) : typeof define === "function" && define.amd ? define(["exports", "lodash-es", "@farris/devkit-vue", "@farris/command-services-vue", "vue", "@farris/ba-command-services-vue", "@farris/ui-vue", "@farris/ui-binding-vue", "@farris/bef-vue", "@farris/charts-vue"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.FarrisRendererVue = {}, global.lodashEs, global.devkitVue, global.commandServicesVue, global.Vue, global.baCommandServicesVue, global.uiVue, global.uiBindingVue, global.befVue, global.chartsVue));
})(this, function(exports2, lodashEs, devkitVue, commandServicesVue, vue, baCommandServicesVue, uiVue, uiBindingVue, befVue, chartsVue) {
"use strict";var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
class CallbackHandler {
constructor() {
/**
* 回调类型
*/
__publicField(this, "callbackType", null);
}
}
class CrosstabCallbackHandler extends CallbackHandler {
/**
* 构造函数
* @param module 模块实例
* @param formMetadataService 元数据服务
*/
constructor(module2, formMetadataService) {
super();
/**
* 回调类型
*/
__publicField(this, "callbackType", "queryData");
this.module = module2;
this.formMetadataService = formMetadataService;
}
/**
* 处理回调
* @param type 回调类型
* @param args 回调参数
* @returns
*/
handle(type, args) {
const payload = args[0];
const viewSchema = args[1];
const { id } = viewSchema;
const { queryData } = viewSchema["queryProperties"];
if (!queryData) {
return payload.data;
}
const relatedComponent = this.formMetadataService.getRelatedComponent(id);
const viewModel = this.module.getViewModel(relatedComponent.id);
payload.schema = JSON.stringify(viewSchema);
return viewModel[queryData]({ payload, schema: viewSchema });
}
}
class BeforeCloseModalCallbackHandler extends CallbackHandler {
/**
* 构造函数
*/
constructor(module2, formMetadataService) {
super();
/**
* 回调类型
*/
__publicField(this, "callbackType", "beforeClose");
this.module = module2;
this.formMetadataService = formMetadataService;
}
/**
* 回调处理
*/
handle(type, args) {
const payload = args[0];
const schema = args[1];
const { beforeClose, id } = schema;
if (!beforeClose) {
return true;
}
const relatedComponent = this.formMetadataService.getRelatedComponent(id);
const viewModel = this.module.getViewModel(relatedComponent.id);
return viewModel[beforeClose]({ payload, schema });
}
}
class DataSourceResolver {
static resolve(entityMetadata, dataSource) {
const entityInfo = this.resolveEntity(entityMetadata, dataSource);
return entityInfo;
}
static resolveEntity(entity, entityLabel, isRoot = true) {
var _a, _b;
const currentPath = isRoot ? [] : [entity.label];
if (entity.label === entityLabel) {
return {
bindingPaths: currentPath,
primaryKey: ((_a = entity == null ? void 0 : entity.type) == null ? void 0 : _a.primary) || "id"
//BAP: BA统一查询平台默认主键是id
};
}
if (!entity.type || !entity.type.entities) {
return null;
}
for (const childEntity of (_b = entity == null ? void 0 : entity.type) == null ? void 0 : _b.entities) {
const result = this.resolveEntity(childEntity, entityLabel, false);
if (result) {
return {
bindingPaths: [...currentPath, ...result.bindingPaths],
primaryKey: result.primaryKey
};
}
}
return null;
}
}
class FieldResolver {
static resolve(entityMetadata, fieldId) {
const fieldInfo = this.resolveField(entityMetadata, fieldId);
return fieldInfo;
}
/**
* BAP: BA统一查询平台字段解析
*/
static baResolve(entityMetadata, fieldId) {
const fieldInfo = this.baResolveField(entityMetadata, fieldId);
return fieldInfo;
}
static resolveField(entitySchema, fieldId) {
const { fields } = entitySchema.type;
for (const field of fields) {
if (field.id === fieldId) {
return {
id: field.id,
bindingPath: field.bindingPath,
required: field.required,
readonly: field.readonly,
multiLanguage: field.multiLanguage,
label: field.label,
dataSource: entitySchema.label
};
}
if (field.type && field.type.fields) {
const result = this.resolveField({ type: field.type, label: entitySchema.label }, fieldId);
if (result) {
return result;
}
}
}
const { entities } = entitySchema.type;
if (!entities || entities.length < 1) {
return null;
}
for (const entity of entities) {
const result = this.resolveField(entity, fieldId);
if (result) {
return result;
}
}
return null;
}
/**
* BAP: BA统一查询平台字段解析
*/
static baResolveField(entitySchema, fieldId) {
const { columns } = entitySchema;
for (const col of columns) {
if (col.columnName === fieldId) {
return {
id: col.columnName,
bindingPath: col.columnName,
required: true,
readonly: false,
multiLanguage: false,
label: col.columnName,
dataSource: entitySchema.label
};
}
}
}
}
const FORM_METADATA_ID_TOKEN = new devkitVue.InjectionToken("@farris/form_metadata_id");
const FORM_METADATA_TOKEN = new devkitVue.InjectionToken("@farris/form_metadata");
const EVENT_HANDLERS_TOKEN = new devkitVue.InjectionToken("@farris/event_handlers_token");
const MODULE_CONFIG_ID_TOKEN = new devkitVue.InjectionToken("@farris/module_config_id_token");
const RENDER_TOKEN = new devkitVue.InjectionToken("@farris/render_token");
const CONFIG_DEPENDENCY_RESOLVER_TOKEN = new devkitVue.InjectionToken("@farris/config_dependency_resolver_token");
const COMPONENT_CONFIG_RESOLVER_TOKEN = new devkitVue.InjectionToken("@farris/component_config_resolver_token");
const COMPONENT_CONFIG_DEPENDENCY_RESOLVER_TOKEN = new devkitVue.InjectionToken("@farris/component_config_dependency_resolver_token");
const CHANGE_EFFECT_RESOLVER_TOKEN = new devkitVue.InjectionToken("@farris/change_effect_resolver_token");
const CALLBACK_HANDLER_TOKEN = new devkitVue.InjectionToken("@farris/callback_handler_token");
var MetadataType = /* @__PURE__ */ ((MetadataType2) => {
MetadataType2["Component"] = "component";
MetadataType2["DataGrid"] = "data-grid";
MetadataType2["TreeGrid"] = "tree-grid";
return MetadataType2;
})(MetadataType || {});
const ENTITY_STORE_SUFFIX = "-entitystore";
const UI_STORE_SUFFIX = "-uistore";
const REPOSITORY_SUFFIX = "-repository";
class ModelValueResolver {
/**
* 构造函数
* @param formMetadataService 表单元数据服务
* @param module 模块实例
* @param injector 注入器
*/
constructor(formMetadataService, module2, injector) {
/**
* 模型值
* @summary
* key = 组件ID;
* value = 绑定值(字段值或表数据)
*/
__publicField(this, "modelValue", {});
this.formMetadataService = formMetadataService;
this.module = module2;
this.injector = injector;
}
/**
* 解析模型
*/
resolve() {
this.modelValue = {};
const components = this.formMetadataService.getComponents();
if (!components || components.length < 1) {
return this.modelValue;
}
components.forEach((component) => {
this.resolveModelValue(component);
});
return this.modelValue;
}
/**
* 解析组件
* @param viewSchema 组件配置
* @returns 递归解析组件及下级组件的绑定值
*/
resolveModelValue(viewSchema) {
const { binding, dataSource } = viewSchema;
const { id } = viewSchema;
if (binding) {
const value = this.resolveBindingValue(viewSchema);
this.modelValue[id] = value;
} else if (dataSource && viewSchema.type !== "lookup") {
const value = this.resolveDataSourceValue(viewSchema);
this.modelValue[id] = value;
}
if (!viewSchema.contents) {
return;
}
if (typeof viewSchema.contents === "string") {
return;
}
viewSchema.contents.forEach((schema) => this.resolveModelValue(schema));
}
/**
* 解析字段绑定
* @param viewSchema
* @returns 绑定的字段值
*/
resolveBindingValue(viewSchema) {
var _a, _b;
const { binding } = viewSchema;
const { field } = binding;
const devkitType = this.module.getDevkitType();
const resolvedField = devkitType === "BA" ? FieldResolver.baResolve(this.getEntityMetadata(binding.dataSource), binding.path) : FieldResolver.resolve(this.entityMetadata, field);
if (!resolvedField) {
console.warn(`Invalid field: ${field}`);
return null;
}
const { dataSource, bindingPath } = resolvedField;
const resolvedEntity = devkitType === "BA" ? DataSourceResolver.resolve(this.getEntityMetadata(dataSource), dataSource) : DataSourceResolver.resolve(this.entityMetadata, dataSource);
if (!resolvedEntity) {
console.warn(`Invalid entity dataSource: ${dataSource}`);
return null;
}
const { bindingPaths } = resolvedEntity;
const fieldBindingPaths = bindingPath.split(".");
const fieldBindingPath = bindingPaths.concat(...fieldBindingPaths).join("/");
let value = null;
try {
if (devkitType === "BA") {
value = (_a = this.module.getEntityStore(`${dataSource}${ENTITY_STORE_SUFFIX}`)) == null ? void 0 : _a.getValueByPath("/" + fieldBindingPath);
} else {
value = (_b = this.entityStore) == null ? void 0 : _b.getValueByPath("/" + fieldBindingPath);
}
} catch (e) {
console.error(e);
}
return value === void 0 ? null : value;
}
/**
* 解析集合数据绑定
* @param viewSchema 组件配置
* @returns 绑定的集合数据
*/
resolveDataSourceValue(viewSchema) {
var _a, _b;
const { dataSource } = viewSchema;
const resolvedEntity = this.module.getDevkitType() === "BA" ? DataSourceResolver.resolve(this.getEntityMetadata(dataSource), dataSource) : DataSourceResolver.resolve(this.entityMetadata, dataSource);
if (!resolvedEntity) {
console.warn(`Invalid entity dataSource: ${dataSource}`);
return null;
}
const { bindingPaths } = resolvedEntity;
let entityList;
if (this.module.getDevkitType() === "BA") {
entityList = (_a = this.module.getEntityStore(`${dataSource}${ENTITY_STORE_SUFFIX}`)) == null ? void 0 : _a.getEntityListByPath("/" + bindingPaths.join("/"));
} else {
entityList = (_b = this.entityStore) == null ? void 0 : _b.getEntityListByPath("/" + bindingPaths.join("/"));
}
const value = (entityList == null ? void 0 : entityList.toJSON()) || [];
return value;
}
/**
* 实体仓库
*/
get entityStore() {
const configId = this.injector.get(MODULE_CONFIG_ID_TOKEN, void 0);
return this.module.getEntityStore(`${configId}${ENTITY_STORE_SUFFIX}`);
}
/**
* 实体元数据
*/
get entityMetadata() {
return this.formMetadataService.getEntity();
}
/**
* 实体元数据
*/
getEntityMetadata(id) {
return this.formMetadataService.getEntityById(id);
}
}
class EntityPathResolver {
/**
* 解析实体路径,返回实体路径,属性路径
* @param path 字符串,形如/ch1s/username
*/
static resolve(entityMetadata, path) {
var _a;
const paths = typeof path === "string" ? path.split("/").filter((p) => p) : path;
const entityPaths = [];
const propertyPaths = [];
if (!paths) {
throw new Error(`Invalid argument: path=${JSON.stringify(path)}`);
}
let currentNode = entityMetadata;
for (let index = 0; index < paths.length; index++) {
const path2 = paths[index];
const entity = this.getEntityProperty(currentNode, path2);
if (entity) {
currentNode = entity;
entityPaths.push(currentNode.label);
} else {
const field = this.getFieldProperty(currentNode, path2);
if (!field) {
break;
}
propertyPaths.push(field.label);
if (!((_a = field.type) == null ? void 0 : _a.fields)) {
break;
}
currentNode = field;
}
}
return {
entityPaths,
propertyPaths
};
}
static getEntityProperty(entity, propertyName) {
var _a, _b;
return (_b = (_a = entity.type) == null ? void 0 : _a.entities) == null ? void 0 : _b.find((p) => p.label === propertyName);
}
static getFieldProperty(entity, propertyName) {
var _a, _b;
return (_b = (_a = entity.type) == null ? void 0 : _a.fields) == null ? void 0 : _b.find((p) => p.label === propertyName);
}
}
class EntityResolver {
resolve(entityMetadata) {
var _a, _b;
const result = [];
function traverse(entity, path) {
var _a2;
const currentPath = [...path, entity.label];
const primary2 = ((_a2 = entityMetadata == null ? void 0 : entityMetadata.type) == null ? void 0 : _a2.primary) || null;
if (primary2) {
result.push({
bindingPaths: currentPath,
primaryKey: entity.type.primary
});
}
entity.type.entities.forEach((childEntity) => {
traverse(childEntity, currentPath);
});
}
const primary = ((_a = entityMetadata == null ? void 0 : entityMetadata.type) == null ? void 0 : _a.primary) || null;
if (!primary) {
return result;
}
result.push({
bindingPaths: [],
primaryKey: entityMetadata.type.primary
});
const childEntities = ((_b = entityMetadata == null ? void 0 : entityMetadata.type) == null ? void 0 : _b.entities) || [];
childEntities.forEach((entity) => {
traverse(entity, []);
});
return result;
}
}
class FormMetadataService {
constructor(metadata, injector) {
__publicField(this, "metadataMap", /* @__PURE__ */ new Map());
var _a, _b;
this.metadata = metadata;
this.injector = injector;
const components = ((_b = (_a = this.metadata) == null ? void 0 : _a.module) == null ? void 0 : _b.components) || [];
this.traverseComponent(components);
}
refreshMetadata(metadata) {
var _a;
const components = ((_a = metadata == null ? void 0 : metadata.module) == null ? void 0 : _a.components) || [];
this.traverseComponent(components);
}
getModuleId() {
return this.metadata.module.id;
}
getModuleCode() {
return this.metadata.module.code;
}
getComponents() {
var _a, _b;
return ((_b = (_a = this.metadata) == null ? void 0 : _a.module) == null ? void 0 : _b.components) || [];
}
getMetadataByType(type) {
return Array.from(this.metadataMap.values()).filter((item) => item.type === type);
}
getMetadataById(id) {
const content = this.metadataMap.get(id);
return content ? content.schema : null;
}
getExpressions() {
return this.metadata.module.expressions || [];
}
getEntity() {
var _a, _b, _c;
return ((_c = (_b = (_a = this.metadata) == null ? void 0 : _a.module) == null ? void 0 : _b.entity[0]) == null ? void 0 : _c.entities[0]) || {};
}
/**
* BAP: BA统一查询平台新增通过id获取实体元数据方法
* @param id
* @returns
*/
getEntityById(id) {
var _a, _b, _c;
return ((_c = (_b = (_a = this.metadata) == null ? void 0 : _a.module) == null ? void 0 : _b.entity) == null ? void 0 : _c.find((el) => el.code == id)) || {};
}
getFrameComponent() {
var _a;
const { components } = ((_a = this.metadata) == null ? void 0 : _a.module) || {};
if (!components || components.length < 1) {
return null;
}
const frameComponent = components.find((component) => component.componentType && component.componentType.toLowerCase() === "frame");
if (!frameComponent) {
return null;
}
return frameComponent;
}
getViewModels() {
var _a;
const { viewmodels } = ((_a = this.metadata) == null ? void 0 : _a.module) || {};
return viewmodels;
}
getViewModelById(id) {
var _a;
const { viewmodels } = ((_a = this.metadata) == null ? void 0 : _a.module) || {};
return viewmodels && viewmodels.find((viewModel) => viewModel.id === id) || null;
}
convertViewModelIdToComponentId(viewModelId) {
const component = this.getComponentByViewModelId(viewModelId);
return component ? component.id : null;
}
getComponentByViewModelId(viewModelId) {
const components = this.getComponents();
const component = components.find((component2) => component2.viewModel === viewModelId);
return component ? component : null;
}
getRelatedComponent(metadataId) {
let currentMetadata = this.metadataMap.get(metadataId);
while (currentMetadata.type !== "component" && currentMetadata.parentId) {
const { parentId } = currentMetadata;
currentMetadata = this.metadataMap.get(parentId);
}
return currentMetadata.schema;
}
traverseComponent(components, parentId) {
components.forEach((component) => {
if (component == null ? void 0 : component.id) {
this.metadataMap.set(
component.id,
Object.assign(
this.metadataMap.get(component.id) ? this.metadataMap.get(component.id) : {},
{
schema: component,
type: component.type,
parentId
}
)
);
}
if (component.contents) {
this.traverseComponent(component.contents, component.id);
}
});
}
}
class EntitySchemaQuery {
/**
* 实体Schema查询服务
*/
constructor(formModule) {
/**
* 表单Schema
*/
__publicField(this, "rootEntitySchema");
/**
* 实体标签和实体描述Map
*/
__publicField(this, "labelAndEntitySchemaMap");
/**
* 字段ID和字段描述Map
*/
__publicField(this, "idAndFieldSchemaMap");
this.labelAndEntitySchemaMap = /* @__PURE__ */ new Map();
this.idAndFieldSchemaMap = /* @__PURE__ */ new Map();
if (formModule && formModule.entity && formModule.entity.length && formModule.entity[0].entities && formModule.entity[0].entities.length) {
this.rootEntitySchema = formModule.entity[0].entities[0];
this.collectEntitySchema(this.rootEntitySchema, null);
} else {
if (formModule && formModule.entity && formModule.entity.length) {
this.rootEntitySchema = formModule.entity;
this.collectBaEntitySchema(this.rootEntitySchema, null);
}
}
}
/**
* 根据Label获取WrappedEntitySchema
*/
getWrappedEntitySchemaByLabel(label) {
if (!this.labelAndEntitySchemaMap.has(label)) {
throw new Error(`EntitySchema(label=${label}) not found`);
}
return this.labelAndEntitySchemaMap.get(label);
}
/**
* 根据ID获取WrappedFieldSchema
*/
getWrappedFieldSchemaById(id) {
if (!this.idAndFieldSchemaMap.has(id)) {
throw new Error(`FieldSchema(id=${id}) not found`);
}
return this.idAndFieldSchemaMap.get(id);
}
collectBaEntitySchema(entitySchemas, wrappedParentEntitySchema) {
entitySchemas.forEach((entitySchema) => {
const bindingPath = this.getEntityBindingPath(wrappedParentEntitySchema, entitySchema);
const wrappedEntitySchema = {
entitySchema,
bindingPath,
wrappedParentEntitySchema
};
this.labelAndEntitySchemaMap.set(entitySchema.code, wrappedEntitySchema);
this.collectBaFieldSchemas(entitySchema.columns, wrappedEntitySchema);
});
}
collectBaFieldSchemas(fieldSchemas, wrappedEntitySchema) {
fieldSchemas.forEach((fieldSchema) => {
this.collectBaFieldSchema(fieldSchema, wrappedEntitySchema);
});
}
collectBaFieldSchema(fieldSchema, wrappedEntitySchema) {
const bindingPath = this.getFieldBindingPath(wrappedEntitySchema, fieldSchema);
const wrappedFieldSchema = { bindingPath, fieldSchema };
this.idAndFieldSchemaMap.set(fieldSchema.id, wrappedFieldSchema);
}
/**
* 搜集所有实体信息
*/
collectEntitySchema(entitySchema, wrappedParentEntitySchema) {
const bindingPath = this.getEntityBindingPath(wrappedParentEntitySchema, entitySchema);
const wrappedEntitySchema = {
entitySchema,
bindingPath,
wrappedParentEntitySchema
};
this.labelAndEntitySchemaMap.set(entitySchema.label, wrappedEntitySchema);
this.collectFieldSchemas(entitySchema.type.fields, wrappedEntitySchema);
const childEntitySchemas = entitySchema.type.entities;
if (Array.isArray(childEntitySchemas)) {
childEntitySchemas.forEach((childEntitySchema) => {
this.collectEntitySchema(childEntitySchema, wrappedEntitySchema);
});
}
}
/**
* 搜集所有字段信息
*/
collectFieldSchemas(fieldSchemas, wrappedEntitySchema) {
fieldSchemas.forEach((fieldSchema) => {
this.collectFieldSchema(fieldSchema, wrappedEntitySchema);
});
}
/**
* 搜集字段信息
*/
collectFieldSchema(fieldSchema, wrappedEntitySchema) {
const bindingPath = this.getFieldBindingPath(wrappedEntitySchema, fieldSchema);
const wrappedFieldSchema = { bindingPath, fieldSchema };
this.idAndFieldSchemaMap.set(fieldSchema.id, wrappedFieldSchema);
if (fieldSchema.$type === "ComplexField") {
const assoOrUdtFieldSchemas = fieldSchema.type.fields;
this.collectFieldSchemas(assoOrUdtFieldSchemas, wrappedEntitySchema);
}
}
/**
* 获取实体绑定路径
*/
getEntityBindingPath(wrappedParentEntitySchema, entitySchema) {
if (!wrappedParentEntitySchema) {
return `/`;
}
const parentBindingPath = wrappedParentEntitySchema.bindingPath;
if (parentBindingPath === "/") {
return `/${entitySchema.label}`;
} else {
return `${parentBindingPath}/${entitySchema.label}`;
}
}
/**
* 获取字段绑定路径
*/
getFieldBindingPath(wrappedEntitySchema, fieldSchema) {
const entityBindingPath = wrappedEntitySchema.bindingPath;
const shortBindingPath = (fieldSchema.bindingPath ? fieldSchema.bindingPath : fieldSchema.columnName).split(".").join("/");
if (entityBindingPath === "/") {
return `/${shortBindingPath}`;
} else {
return `${entityBindingPath}/${shortBindingPath}`;
}
}
}
class FormMetadataQuery {
/**
* 构造函数
*/
constructor(formMetadata) {
/**
* 表单元数据
*/
__publicField(this, "formMetadata");
/**
* 表单模块
*/
__publicField(this, "formModule");
/**
* 视图模型Map
*/
__publicField(this, "viewModelMap");
/**
* 视图模型组件Map
*/
__publicField(this, "vmComponentMap");
/**
* 包装后的组件Map
*/
__publicField(this, "wrappedComponentMap");
/**
* EntitySchema查询
*/
__publicField(this, "entitySchemaQuery");
this.formMetadata = formMetadata;
this.formModule = formMetadata.content.module;
this.entitySchemaQuery = new EntitySchemaQuery(this.formModule);
this.initViewModelMap();
this.initVmComponentMap();
this.initWrappedComponentMap();
}
/**
* 根据label获取EntitySchema
*/
getWrappedEntitySchemaByLabel(label) {
return this.entitySchemaQuery.getWrappedEntitySchemaByLabel(label);
}
/**
* 根据ID获取FieldSchema
*/
getWrappedFieldSchemaById(id) {
return this.entitySchemaQuery.getWrappedFieldSchemaById(id);
}
/**
* 获取视图模型集合
*/
getViewModels() {
return this.formModule.viewmodels;
}
/**
* 根据ID获取视图模型
*/
getViewModelById(id) {
const viewModel = this.viewModelMap.get(id);
return viewModel;
}
/**
* 获取VM组件集合
*/
getVmComponents() {
return this.formModule.components;
}
/**
* 根据ID获取获取VM组件
*/
getVmComponentById(id) {
return this.vmComponentMap.get(id);
}
/**
* 根据视图模型ID获取VM组件
*/
getVmComponentByVmId(viewModelId) {
const vmComponents = this.getVmComponents();
const targetVmComponent = vmComponents.find((vmComponent) => {
return vmComponent.viewModel === viewModelId;
});
return targetVmComponent;
}
/**
* 根据ID获取包装组件
*/
getWrappedComponentById(id) {
const wrappedComponent = this.wrappedComponentMap.get(id);
return wrappedComponent;
}
/**
* 获取全部包装组件
*/
getWrappedComponents() {
const wrappedComponents = Array.from(this.wrappedComponentMap.values());
return wrappedComponents;
}
/**
* 根据类型获取包装组件的集合
*/
getWrappedComponentsByType(type) {
const allWrappedComponents = this.getWrappedComponents();
const filteredWrappedComponents = allWrappedComponents.filter((wrappedComponent) => {
return wrappedComponent.component.type === type;
});
return filteredWrappedComponents;
}
/**
* 初始化视图模型Map
*/
initViewModelMap() {
this.viewModelMap = /* @__PURE__ */ new Map();
const viewModels = this.formModule.viewmodels;
viewModels.forEach((viewModel) => {
this.viewModelMap.set(viewModel.id, viewModel);
});
}
/**
* 初始化VM组件Map
*/
initVmComponentMap() {
this.vmComponentMap = /* @__PURE__ */ new Map();
const vmComponents = this.formModule.components;
vmComponents.forEach((vmComponent) => {
this.vmComponentMap.set(vmComponent.id, vmComponent);
});
}
/**
* 初始化包装组件Map
*/
initWrappedComponentMap() {
this.wrappedComponentMap = /* @__PURE__ */ new Map();
const vmComponents = this.formModule.components;
vmComponents.forEach((vmComponent) => {
const currentViewModel = this.getViewModelById(vmComponent.viewModel);
this.appendWrappedComponentToMap(vmComponent, currentViewModel, vmComponent);
});
}
/**
* 追加组件到组件Map
*/
appendWrappedComponentToMap(component, currentViewModel, currentViewModelComponent) {
const componentSchema = null;
const wrappedComponent = {
component,
componentSchema,
viewModel: currentViewModel,
viewModelComponent: currentViewModelComponent
};
this.wrappedComponentMap.set(component.id, wrappedComponent);
if (component.editor) {
this.appendWrappedComponentToMap(component.editor, currentViewModel, currentViewModelComponent);
}
if (Array.isArray(component.contents)) {
component.contents.forEach((childComponent) => {
this.appendWrappedComponentToMap(childComponent, currentViewModel, currentViewModelComponent);
});
}
}
/**
* 获取外部表单信息
*/
getExternalFormInfos() {
const wrappedExternalContainers = this.getWrappedComponentsByType("external-container");
const externalFormInfos = [];
wrappedExternalContainers.forEach((wrappedExternalContainer) => {
const externalContainer = wrappedExternalContainer.component;
const externalFormInfo = this.buildExternalFormInfo(externalContainer);
if (!externalFormInfo) {
return;
}
externalFormInfos.push(externalFormInfo);
});
return externalFormInfos;
}
/**
* 获取外部表单信息
*/
buildExternalFormInfo(externalContainer) {
const { externalComponent } = externalContainer;
if (!externalComponent) {
return;
}
const externalFormInfo = {
id: externalComponent.id,
externalContainerId: externalContainer.id,
fileName: externalComponent.fileName,
projectPath: externalComponent.relativePath,
formMetadataPath: externalComponent.relativePath + "/" + externalComponent.fileName
};
return externalFormInfo;
}
/**
* 获取组件通讯节点集合
*/
getCommunications() {
const communicationNodes = this.formModule.communications;
return communicationNodes;
}
/**
* 获取外部组件集合
*/
getExternalComponents() {
const externalComponentNodes = this.formModule.externalComponents;
return externalComponentNodes;
}
/**
* 获取外部组件ID
*/
getExternalComponentById(id) {
const externalComponentNodes = this.getExternalComponents();
const targetExternalComponentNode = externalComponentNodes.find((externalComponentNode) => {
return externalComponentNode === id;
});
return targetExternalComponentNode;
}
/**
* 获取根视图模型组件
*/
getRootComponent() {
const vmComponents = this.getVmComponents();
return vmComponents[0];
}
}
class ComponentService {
constructor(injector) {
__publicField(this, "render");
this.injector = injector;
this.render = this.injector.get(RENDER_TOKEN);
}
getComponentById(id) {
return this.render && this.render.value.componentManager.get(id);
}
}
const serviceProviders = [
{ provide: FormMetadataService, useClass: FormMetadataService, deps: [FORM_METADATA_TOKEN, devkitVue.Injector] },
{ provide: ComponentService, useClass: ComponentService, deps: [devkitVue.Injector] }
];
const resolverProviders = [
{ provide: ModelValueResolver, useClass: ModelValueResolver, deps: [FormMetadataService, devkitVue.Module, devkitVue.Injector] }
];
var ConfigType = /* @__PURE__ */ ((ConfigType2) => {
ConfigType2["Expression"] = "Expression";
ConfigType2["Variable"] = "Variable";
ConfigType2["StateMachine"] = "StateMachine";
return ConfigType2;
})(ConfigType || {});
var DependencyKind = /* @__PURE__ */ ((DependencyKind2) => {
DependencyKind2["StateMachine"] = "StateMachine";
DependencyKind2["EntityState"] = "EntityState";
DependencyKind2["UIState"] = "UIState";
DependencyKind2["ExpressionResult"] = "ExpressionResult";
DependencyKind2["Static"] = "Static";
DependencyKind2["Empty"] = "Empty";
return DependencyKind2;
})(DependencyKind || {});
var ConfigurationType = /* @__PURE__ */ ((ConfigurationType2) => {
ConfigurationType2["Readonly"] = "Readonly";
ConfigurationType2["Editable"] = "Editable";
ConfigurationType2["Required"] = "Required";
ConfigurationType2["Visible"] = "Visible";
ConfigurationType2["Disabled"] = "Disabled";
return ConfigurationType2;
})(ConfigurationType || {});
class ComponentConfigRegistry {
constructor() {
__publicField(this, "registry");
this.registry = /* @__PURE__ */ new Map();
}
setConfigs(componentId, configs) {
this.registry.set(componentId, configs);
}
getComponents() {
return this.registry;
}
getConfigs(componentId) {
return this.registry.get(componentId);
}
/**
* 获取依赖指定表达式的组件ID列表
* @param expressionId 表达式ID
* @returns 组件ID数组
*/
getComponentsByExpressionId(expressionId) {
return Array.from(this.registry.entries()).reduce((result, [componentId, configurations]) => {
if (!(configurations == null ? void 0 : configurations.length)) {
return result;
}
const hasExpressionDependency = configurations.some(
(config) => {
var _a;
return (_a = config.deps) == null ? void 0 : _a.some(
(dep) => dep.type === DependencyKind.ExpressionResult && dep.path === expressionId
);
}
);
if (hasExpressionDependency) {
result.push(componentId);
}
return result;
}, []);
}
getComponentsByDependencyKind(kinds) {
return Array.from(this.registry.entries()).reduce((result, [componentId, configurations]) => {
if (!(configurations == null ? void 0 : configurations.length)) {
return result;
}
const hasExpressionDependency = configurations.some((config) => {
var _a;
return (_a = config.deps) == null ? void 0 : _a.some((dep) => kinds.includes(dep.type));
});
if (hasExpressionDependency) {
result.push(componentId);
}
return result;
}, []);
}
}
class ConfigResolver {
/**
* 构造函数
* @param viewModel 视图模型
* @param expressionResult 表达式执行结果???
* @param formMetadataService 表单元数据服务
*/
constructor(viewModel, expressionResult, formMetadataService) {
this.viewModel = viewModel;
this.expressionResult = expressionResult;
this.formMetadataService = formMetadataService;
}
/**
* 解析配置
* @param config 配置值,可能是布尔值(比如是否可见)、字符串、对象(比如字段绑定、变量绑定等)
* @param metadataId 组件ID
* @returns
*/
resolve(config, metadataId) {
var _a;
if (typeof config === "boolean") {
return config;
}
if (config && typeof config === "object" && "type" in config) {
return this.resolveStandardConfig(config, metadataId);
}
const relatedComponent = this.formMetadataService.getRelatedComponent(metadataId);
if (!relatedComponent) {
console.warn(`解析配置异常,未找到相关组件: ${metadataId}`);
return;
}
const viewModel = this.viewModel.getModule().getViewModel(relatedComponent.id);
if (!viewModel) {
return;
}
const context = {
stateMachine: viewModel.state.stateMachine,
uiState: viewModel.state.uiState,
entityState: (_a = viewModel.state.entityState) == null ? void 0 : _a.currentEntity
};
return new Function("viewModel", `return ${config}`)(context);
}
/**
* 解析标准属性值对象
*/
resolveStandardConfig(config, metadataId) {
if (config.type === ConfigType.Expression) {
return this.resolveExpressionConfig(config);
} else if (config.type === ConfigType.Variable) {
return this.resolveVariableConfig(config, metadataId);
} else if (config.type === ConfigType.StateMachine) {
return this.resolveStateMachineConfig(config);
}
}
/**
* 解析变量绑定
*/
resolveVariableConfig(config, metadataId) {
var _a;
const relatedComponent = this.formMetadataService.getRelatedComponent(metadataId);
if (!relatedComponent) {
return;
}
const { fullPath, path } = config;
let viewModelId = relatedComponent.id;
if (fullPath !== path) {
viewModelId = path.split(".")[0];
}
const viewModel = this.viewModel.getModule().getViewModel(viewModelId);
if (!viewModel) {
return;
}
return (_a = viewModel.uiStore) == null ? void 0 : _a.getValue(fullPath);
}
/**
* 解析表达式绑定
*/
resolveExpressionConfig(config) {
return this.expressionResult[config.expressionId];
}
/**
* 解析状态机绑定
*/
resolveStateMachineConfig(config) {
const { field } = config;
const { stateMachine } = this.viewModel;
const value = stateMachine == null ? void 0 : stateMachine.getValue(field);
const status = typeof config.status === "boolean" ? config.status : config.status === "!" ? false : true;
return status ? value : !value;
}
}
const configProviders = [
{ provide: ComponentConfigRegistry, useClass: ComponentConfigRegistry, deps: [] },
{ provide: ConfigResolver, useClass: ConfigResolver, deps: [devkitVue.ViewModel, devkitVue.ExpressionResult, FormMetadataService] }
];
class StateMachineConfigDependencyResolver {
resolve(config) {
if (typeof config !== "string") {
return null;
}
if (!config || config.length < 1) {
return null;
}
config = config.trim().replace(/\[\s*(['"])([^\1]+?)\1\s*\]/g, ".$2");
const regex = /^[!]?viewModel.stateMachine\.([a-zA-Z0-9_.]+)$/g;
const results = [];
let matches;
while ((matches = regex.exec(config)) !== null) {
results.push(matches[1]);
}
if (results.length < 1) {
return null;
}
return results.map((path) => {
return { type: DependencyKind.StateMachine, path };
});
}
}
class StaticConfigDependencyResolver {
resolve(config) {
if (typeof config === "boolean") {
return [{ type: DependencyKind.Static }];
}
if (typeof config === "string" && config.match(/true|false/g)) {
return [{ type: DependencyKind.Static }];
}
return null;
}
}
class UIStateConfigDependencyResolver {
resolve(config, viewSchema) {
if (typeof config !== "string") {
return null;
}
if (!config || config.length < 1) {
return null;
}
const regex = /(?:viewModel)\.uiState\.([a-zA-Z0-9_.-]+)/g;
const results = [];
let matches;
while ((matches = regex.exec(config)) !== null) {
results.push(matches[1]);
}
if (results.length < 1) {
return null;
}
return results.map((path) => {
return { path, type: DependencyKind.UIState };
});
}
}
class EmptyConfigDependencyResolver {
resolve(config) {
if (config === "" || config === null || config === void 0) {
return [{ type: DependencyKind.Empty }];
}
return null;
}
}
class EntityStateConfigDependencyResolver {
constructor() {
}
resolve(config) {
if (typeof config !== "string") {
return null;
}
if (!config || config.length < 1) {
return null;
}
const regex = /(?:viewModel)\.entityState\.([a-zA-Z0-9_.-]+)/g;
const results = [];
let matches;
while ((matches = regex.exec(config)) !== null) {
results.push(matches[1]);
}
if (results.length < 1) {
return null;
}
return results.map((path) => {
path = "/" + path.split(".").join("/");
return { path, type: DependencyKind.EntityState };
});
}
}
class StandardConfigDependencyResolver {
constructor(formMetadataService) {
this.formMetadataService = formMetadataService;
}
resolve(config, viewSchema) {
if (typeof config === "string" || typeof config === "boolean") {
return null;
}
if (!this.isValidConfig(config)) {
return null;
}
switch (config.type) {
case "Expression":
return this.resolveExpressionConfig(config);
case "Variable":
return this.resolveVariableConfig(config, viewSchema);
case "StateMachine":
return this.resolveStateMachineConfig(config);
default:
return null;
}
}
isValidConfig(config) {
return config && typeof config === "object" && "type" in config;
}
resolveExpressionConfig(config) {
return [{
type: DependencyKind.ExpressionResult,
path: config.expressionId
}];
}
resolveVariableConfig(config, viewSchema) {
if (config.path.indexOf(".") !== -1) {
return [{
type: DependencyKind.UIState,
path: config.path
}];
}
if (viewSchema) {
const schemaId = viewSchema.id;
const relatedComponent = this.formMetadataService.getRelatedComponent(schemaId);
if (relatedComponent) {
return [{
type: DependencyKind.UIState,
path: `${relatedComponent.id}.${config.path}`
}];
}
}
return [{
type: DependencyKind.UIState,
path: config.path
}];
}
resolveStateMachineConfig(config) {
return [{
type: DependencyKind.StateMachine,
path: config.field
}];
}
}
class ConfigDependencyResolveService {
constructor(configDependencyResolverRegistry) {
this.configDependencyResolverRegistry = configDependencyResolverRegistry;
}
resolve(config, viewSchema) {
const { resolvers } = this.configDependencyResolverRegistry;
if (!resolvers) {
return null;
}
return resolvers.reduce((dependencies, resolver) => {
const configs = resolver.resolve(config, viewSchema);
if (configs) {
dependencies.push(...configs);
}
return dependencies;
}, []);
}
}
class ConfigDependencyResolverRegistry {
constructor(injector) {
__publicField(this, "resolvers");
this.injector = injector;
this.resolvers = this.injector.get(CONFIG_DEPENDENCY_RESOLVER_TOKEN);
}
}
const configDependencyResolverProviders = [
{ provide: CONFIG_DEPENDENCY_RESOLVER_TOKEN, useClass: StateMachineConfigDependencyResolver, deps: [], multi: true },
{ provide: CONFIG_DEPENDENCY_RESOLVER_TOKEN, useClass: UIStateConfigDependencyResolver, deps: [], multi: true },
{ provide: CONFIG_DEPENDENCY_RESOLVER_TOKEN, useClass: StaticConfigDependencyResolver, deps: [], multi: true },
{ provide: CONFIG_DEPENDENCY_RESOLVER_TOKEN, useClass: EmptyConfigDependencyResolver, deps: [], multi: true },
{ provide: CONFIG_DEPENDENCY_RESOLVER_TOKEN, useClass: EntityStateConfigDependencyResolver, deps: [], multi: true },
{ provide: CONFIG_DEPENDENCY_RESOLVER_TOKEN, useClass: StandardConfigDependencyResolver, deps: [FormMetadataService], multi: true },
{ provide: ConfigDependencyResolverRegistry, useClass: ConfigDependencyResolverRegistry, deps: [devkitVue.Injector] },
{ provide: ConfigDependencyResolveService, useClass: ConfigDependencyResolveService, deps: [ConfigDependencyResolverRegistry] }
];
class BeforeEditCallCallbackHandler extends CallbackHandler {
/**
* 构造函数
*/
constructor(module2, formMetadataService, expressionEvaluator) {
super();
/**
* 回调类型
*/
__publicField(this, "callbackType", "beforeEditCell");
this.module = module2;
this.formMetadataService = formMetadataService;
this.expressionEvaluator = expressionEvaluator;
}
/**
* 回调处理
*/
handle(callbackType, args) {
const { column, rawData } = args[0];
const viewSchema = args[1];
const canEdit = this.canEditCell(column, rawData, viewSchema);
if (!canEdit) {
return false;
}
this.setLookupValue(column, rawData);
return true;
}
/**
* 单元格是否允许编辑
* @param column
* @param rowData
* @param viewSchema
* @returns
*/
canEditCell(column, rowData, viewSchema) {
var _a;
const { id } = column;
const { dataSource } = viewSchema;
const resolvedEntity = DataSourceResolver.resolve(this.entitySchema, dataSource);
if (!resolvedEntity) {
console.warn(`当前单元格无法编辑,表格数据源${dataSource}不存在,请检查数据源配置`);
return false;
}
const { bindingPaths = [], primaryKey = "id" } = resolvedEntity;
const columnSchema = viewSchema.columns.find((column2) => column2.id === id);
const readonlyConfig = (_a = columnSchema == null ? void 0 : columnSchema.editor) == null ? void 0 : _a.readonlyConfig;
if (readonlyConfig && readonlyConfig.type === ConfigType.Expression) {
const { expressionId } = readonlyConfig;
const result = this.expressionEvaluator.evaluate(expressionId, [{ bindingPath: bindingPaths.join("/"), primaryValue: rowData[primaryKey] }]);
return !result;
}
return !column.editor.readonly;
}
/**
* 设置帮助的值
*/
setLookupValue(column, rowData) {
const { editor, binding } = column || {};
if (!editor || !binding) {
return;
}
const { type, mappingFields, dataSource } = editor;
if (type !== "lookup" || !dataSource) {
return;
}
const { idField } = dataSource;
const mappingFieldsObject = typeof mappingFields === "string" ? JSON.parse(mappingFields) : mappingFields;
if (!mappingFieldsObject || Object.keys(mappingFieldsObject).length < 1) {
return;
}
const valueField = lodashEs.get(mappingFieldsObject, idField);
const entityStoreId = this.formMetadataService.getModuleCode() + ENTITY_STORE_SUFFIX;
const entityStore = this.module.getEntityStore(entityStoreId);
if (!entityStore) {
return;
}
const { field } = binding;
if (!field) {
return;
}
const { dataSource: entitySource } = FieldResolver.resolve(this.entitySchema, field) || {};
if (!entitySource) {
return;
}
const { bindingPaths = [], primaryKey = "id" } = DataSourceResolver.resolve(this.entitySchema, entitySource) || {};
const entityListPath = `/${bindingPaths.join("/")}`;
const entityList = entityStore == null ? void 0 : entityStore.getEntityListByPath(entityListPath);
const primaryValue = rowData[primaryKey];
const entity = entityList == null ? void 0 : entityList.getEntityById(primaryValue);
if (!entity) {
return;
}
editor.idValue = lodashEs.get(entity, valueField);
}
get entitySchema() {
return this.formMetadataService.getEntity();
}
}
class BeforeLoadDataCallbackHandler extends CallbackHandler {
constructor(module2, formMetadataService) {
super();
__publicField(this, "callbackType", "beforeLoadData");
this.module = module2;
this.formMetadataService = formMetadataService;
}
handle(type, args) {
const payload = args[0];
const viewSchema = args[1];
const { beforeLoadData, id } = viewSchema;
if (!beforeLoadData) {
return payload.data;
}
const relatedComponent = this.formMetadataService.getRelatedComponent(id);
const viewModel = this.module.getViewModel(relatedComponent.id);
return viewModel[beforeLoadData]({ payload, schema: viewSchema });
}
}
class BeforeSelectDataCallbackHandler extends CallbackHandler {
constructor(module2, formMetadataService) {
super();
__publicField(this, "callbackType", "beforeSelectData");
this.module = module2;
this.formMetadataService = formMetadataService;
}
handle(type, args) {
const payload = args[0];
const viewSchema = args[1];
const { beforeSelectData, id } = viewSchema;
if (!beforeSelectData) {
return true;
}
const relatedComponent = this.formMetadataService.getRelatedComponent(id);
const viewModel = this.module.getViewModel(relatedComponent.id);
return viewModel[beforeSelectData]({ payload, schema: viewSchema });
}
}
class CallbackHandlerRegistry {
/**
* 构造函数
*/
constructor(injector) {
/**
* 回调方法处理器集合
*/
__publicField(this, "handlers");
this.injector = injector;
this.handlers = injector.get(CALLBACK_HANDLER_TOKEN);
}
/**
* 根据回调类型,获取回调处理器
* @param callbackType 回调类型
* @returns
*/
getHandler(callbackType) {
return this.handlers.find((handler) => handler.callbackType === callbackType) || null;
}
}
class DictPickedCallbackHandler extends CallbackHandler {
/**
* 构造函数
* @param module 模块实例
* @param formMetadataService 元数据服务
*/
constructor(module2, formMetadataService) {
super();
/**
* 回调类型
*/
__publicField(this, "callbackType", "dictPicked");
this.module = module2;
this.formMetadataService = formMetadataService;
}
/**
* 处理回调
* @param type 回调类型
* @param args 回调参数
* @