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

195 lines
5.7 KiB
TypeScript
Raw Normal View History

2020-11-11 20:57:18 +08:00
import { Model, ModelCtor } from '@nocobase/database';
2020-12-08 14:33:28 +08:00
import { get, set } from 'lodash';
2020-12-13 00:09:25 +08:00
import { Op } from 'sequelize';
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) {
2020-12-12 16:36:02 +08:00
if (!field.get('component.showInTable')) {
continue;
}
arr.push({
2020-12-12 16:36:02 +08:00
...field.get(),
sorter: field.get('sortable'),
2020-12-13 20:48:48 +08:00
dataIndex: field.name.split('.'),
});
}
return arr;
},
2020-12-08 14:33:28 +08:00
form: async (fields: Model[], ctx?: any) => {
2020-12-13 00:09:25 +08:00
const [Field] = ctx.db.getModels(['fields']) as ModelCtor<Model>[];
2020-12-08 14:33:28 +08:00
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||{}),
}
2020-12-13 00:09:25 +08:00
if (field.get('name') === 'filter' && field.get('collection_name') === 'views') {
const { values } = ctx.action.params;
const all = await Field.findAll({
where: {
collection_name: get(values, 'associatedKey'),
developerMode: ctx.state.developerMode,
},
order: [['sort', 'asc']],
});
set(prop, 'x-component-props.fields', all.filter(f => f.get('filterable')));
}
if (type === 'select') {
prop.type = 'string'
}
2020-12-13 00:09:25 +08:00
if (field.get('component.tooltip')) {
prop.description = field.get('component.tooltip');
}
2020-12-13 17:20:54 +08:00
// if (field.get('name') === 'dataSource') {
// set(prop, 'items.properties.value.visible', false);
// }
if (['number', 'percent'].includes(interfaceType) && field.get('precision')) {
set(prop, 'x-component-props.step', field.get('precision'));
}
2020-12-13 00:09:25 +08:00
if (field.get('required')) {
prop.required = true;
}
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');
2020-12-13 17:20:54 +08:00
if (typeof defaultValue !== 'undefined') {
prop.default = defaultValue;
}
2020-12-13 00:09:25 +08:00
if (interfaceType === 'boolean') {
set(prop, 'x-component-props.children', prop.title);
delete prop.title;
}
2020-12-13 20:48:48 +08:00
if (interfaceType === 'multipleSelect') {
set(prop, 'x-component-props.mode', 'multiple');
}
2020-12-13 17:20:54 +08:00
if (['radio', 'select', 'multipleSelect', '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': {
2020-12-13 00:09:25 +08:00
fields: fields.filter(field => field.get('filterable')),
2020-12-08 21:19:39 +08:00
},
2020-12-13 00:09:25 +08:00
},
2020-12-08 21:19:39 +08:00
}
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,
},
});
2020-12-13 00:09:25 +08:00
const where: any = {
developerMode: ctx.state.developerMode,
}
if (!view.get('draggable')) {
where.type = {
[Op.not]: 'sort',
};
}
const fields = await collection.getFields({
2020-12-13 00:09:25 +08:00
where,
order: [
['sort', 'asc'],
]
});
const actions = await collection.getActions({
2020-12-08 14:33:28 +08:00
where: {
developerMode: ctx.state.developerMode,
},
order: [
['sort', 'asc'],
]
});
let actionNames = view.get('actionNames') || [];
if (actionNames.length === 0) {
actionNames = ['filter', 'create', 'destroy'];
}
2020-12-08 21:19:39 +08:00
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-13 20:48:48 +08:00
if (view.get('template') === 'SimpleTable') {
view.setDataValue('rowViewName', 'form');
}
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
}
2020-12-13 00:09:25 +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,
2020-12-13 00:09:25 +08:00
// viewCollectionName: action.collection_name,
2020-11-11 20:57:18 +08:00
})),
};
await next();
};