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
|
|
|
|
|
2020-11-13 22:01:14 +08:00
|
|
|
|
const transforms = {
|
2020-12-08 14:33:28 +08:00
|
|
|
|
table: async (fields: Model[], context?: any) => {
|
2020-11-13 22:01:14 +08:00
|
|
|
|
const arr = [];
|
|
|
|
|
for (const field of fields) {
|
2020-12-01 20:11:39 +08:00
|
|
|
|
if (!get(field.component, 'showInTable')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-11-13 22:01:14 +08:00
|
|
|
|
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);
|
2020-11-19 21:12:15 +08:00
|
|
|
|
const schema = {};
|
2020-11-13 22:01:14 +08:00
|
|
|
|
for (const field of fields) {
|
2020-12-08 21:19:39 +08:00
|
|
|
|
if (!field.get('component.showInForm')) {
|
2020-12-01 20:11:39 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-12-08 21:19:39 +08:00
|
|
|
|
const interfaceType = field.get('interface');
|
|
|
|
|
const type = field.get('component.type') || 'string';
|
2020-12-01 20:11:39 +08:00
|
|
|
|
const prop: any = {
|
|
|
|
|
type,
|
2020-11-19 21:12:15 +08:00
|
|
|
|
title: field.title||field.name,
|
2020-12-01 20:11:39 +08:00
|
|
|
|
...(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);
|
|
|
|
|
}
|
2020-12-01 20:11:39 +08:00
|
|
|
|
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)) {
|
2020-12-01 20:11:39 +08:00
|
|
|
|
prop.enum = get(field.options, 'dataSource', []);
|
|
|
|
|
}
|
|
|
|
|
schema[field.name] = {
|
|
|
|
|
...prop,
|
2020-11-19 21:12:15 +08:00
|
|
|
|
};
|
2020-11-13 22:01:14 +08:00
|
|
|
|
}
|
2020-11-19 21:12:15 +08:00
|
|
|
|
return schema;
|
2020-11-13 22:01:14 +08:00
|
|
|
|
},
|
2020-12-08 14:33:28 +08:00
|
|
|
|
details: async (fields: Model[], context?: any) => {
|
2020-11-13 22:01:14 +08:00
|
|
|
|
const arr = [];
|
|
|
|
|
for (const field of fields) {
|
2020-12-01 20:11:39 +08:00
|
|
|
|
if (!get(field.component, 'showInDetail')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-11-13 22:01:14 +08:00
|
|
|
|
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-13 22:01:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
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,
|
|
|
|
|
},
|
2020-11-13 22:01:14 +08:00
|
|
|
|
// 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,
|
|
|
|
|
},
|
|
|
|
|
});
|
2020-11-19 21:12:15 +08:00
|
|
|
|
const fields = await collection.getFields({
|
2020-12-08 14:33:28 +08:00
|
|
|
|
where: {
|
|
|
|
|
developerMode: ctx.state.developerMode,
|
|
|
|
|
},
|
2020-11-19 21:12:15 +08:00
|
|
|
|
order: [
|
|
|
|
|
['sort', 'asc'],
|
|
|
|
|
]
|
|
|
|
|
});
|
|
|
|
|
const actions = await collection.getActions({
|
2020-12-08 14:33:28 +08:00
|
|
|
|
where: {
|
|
|
|
|
developerMode: ctx.state.developerMode,
|
|
|
|
|
},
|
2020-11-19 21:12:15 +08:00
|
|
|
|
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();
|
|
|
|
|
};
|