tachybase_todo/packages/plugin-pages/src/actions/getView.ts

152 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-11-11 20:57:18 +08:00
import { ResourceOptions } from '@nocobase/resourcer';
import { Model, ModelCtor } from '@nocobase/database';
2020-12-08 14:33:28 +08:00
import { get, set } from 'lodash';
2020-11-11 20:57:18 +08:00
const transforms = {
2020-12-08 14:33:28 +08:00
table: async (fields: Model[], context?: any) => {
const arr = [];
for (const field of fields) {
if (!get(field.component, 'showInTable')) {
continue;
}
arr.push({
...field.toJSON(),
...field.options,
dataIndex: field.name,
});
}
return arr;
},
2020-12-08 14:33:28 +08:00
form: async (fields: Model[], ctx?: any) => {
const mode = get(ctx.action.params, ['values', 'mode'], ctx.action.params.mode);
const schema = {};
for (const field of fields) {
2020-12-08 21:19:39 +08:00
if (!field.get('component.showInForm')) {
continue;
}
2020-12-08 21:19:39 +08:00
const interfaceType = field.get('interface');
const type = field.get('component.type') || 'string';
const prop: any = {
type,
title: field.title||field.name,
...(field.component||{}),
}
if (type === 'select') {
prop.type = 'string'
}
2020-12-08 14:33:28 +08:00
if (mode === 'update' && field.get('createOnly')) {
set(prop, 'x-component-props.disabled', true);
}
if (typeof field.get('showTime') === 'boolean') {
set(prop, 'x-component-props.showTime', field.get('showTime'));
}
const defaultValue = get(field.options, 'defaultValue');
if (defaultValue) {
prop.default = defaultValue;
}
2020-12-08 21:19:39 +08:00
if (['radio', 'select', 'checkboxes'].includes(interfaceType)) {
prop.enum = get(field.options, 'dataSource', []);
}
schema[field.name] = {
...prop,
};
}
return schema;
},
2020-12-08 14:33:28 +08:00
details: async (fields: Model[], context?: any) => {
const arr = [];
for (const field of fields) {
if (!get(field.component, 'showInDetail')) {
continue;
}
arr.push({
...field.toJSON(),
...field.options,
});
}
return arr;
},
2020-12-08 21:19:39 +08:00
filter: async (fields: Model[], ctx?: any) => {
const properties = {
filter: {
type: 'filter',
'x-component-props': {
fields,
},
}
}
return properties;
},
};
2020-11-11 20:57:18 +08:00
export default async (ctx, next) => {
const { resourceName, resourceKey } = ctx.action.params;
2020-12-08 21:19:39 +08:00
const [View, Collection, Field, Action] = ctx.db.getModels(['views', 'collections', 'fields', 'actions']) as ModelCtor<Model>[];
let view = await View.findOne(View.parseApiJson({
2020-11-11 20:57:18 +08:00
filter: {
collection_name: resourceName,
name: resourceKey,
},
// fields: {
// appends: ['actions', 'fields'],
// },
2020-11-11 20:57:18 +08:00
}));
2020-12-08 21:19:39 +08:00
if (!view) {
// 如果不存在 view新建一个
view = new View({type: resourceKey, template: 'FilterForm'});
}
const collection = await Collection.findOne({
where: {
name: resourceName,
},
});
const fields = await collection.getFields({
2020-12-08 14:33:28 +08:00
where: {
developerMode: ctx.state.developerMode,
},
order: [
['sort', 'asc'],
]
});
const actions = await collection.getActions({
2020-12-08 14:33:28 +08:00
where: {
developerMode: ctx.state.developerMode,
},
order: [
['sort', 'asc'],
]
});
2020-12-08 21:19:39 +08:00
const actionNames = view.get('actionNames') || [];
if (view.get('type') === 'table') {
2020-11-11 20:57:18 +08:00
const defaultTabs = await collection.getTabs({
where: {
default: true,
},
});
view.setDataValue('defaultTabName', get(defaultTabs, [0, 'name']));
}
2020-12-08 21:19:39 +08:00
if (view.get('updateViewName')) {
view.setDataValue('rowViewName', view.get('updateViewName'));
2020-11-11 20:57:18 +08:00
}
view.setDataValue('viewCollectionName', view.collection_name);
2020-12-08 21:19:39 +08:00
let title = collection.get('title');
const mode = get(ctx.action.params, ['values', 'mode'], ctx.action.params.mode);
if (mode === 'update') {
title = `编辑${title}`;
} else {
title = `创建${title}`;
}
2020-11-11 20:57:18 +08:00
ctx.body = {
2020-12-08 21:19:39 +08:00
...view.get(),
title,
original: fields,
2020-12-08 14:33:28 +08:00
fields: await (transforms[view.type]||transforms.table)(fields, ctx),
2020-11-11 20:57:18 +08:00
actions: actions.filter(action => actionNames.includes(action.name)).map(action => ({
...action.toJSON(),
...action.options,
viewCollectionName: action.collection_name,
})),
};
await next();
};