2020-11-11 20:57:18 +08:00
|
|
|
import { ResourceOptions } from '@nocobase/resourcer';
|
|
|
|
import { Model, ModelCtor } from '@nocobase/database';
|
|
|
|
import { get } from 'lodash';
|
|
|
|
|
2020-11-13 22:01:14 +08:00
|
|
|
const transforms = {
|
|
|
|
table: async (fields: Model[]) => {
|
|
|
|
const arr = [];
|
|
|
|
for (const field of fields) {
|
|
|
|
arr.push({
|
|
|
|
...field.toJSON(),
|
|
|
|
...field.options,
|
|
|
|
dataIndex: field.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
},
|
|
|
|
form: async (fields: Model[]) => {
|
2020-11-19 21:12:15 +08:00
|
|
|
const schema = {};
|
2020-11-13 22:01:14 +08:00
|
|
|
for (const field of fields) {
|
2020-11-19 21:12:15 +08:00
|
|
|
schema[field.name] = {
|
|
|
|
type: 'string',
|
|
|
|
title: field.title||field.name,
|
|
|
|
};
|
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
|
|
|
},
|
|
|
|
details: async (fields: Model[]) => {
|
|
|
|
const arr = [];
|
|
|
|
for (const field of fields) {
|
|
|
|
arr.push({
|
|
|
|
...field.toJSON(),
|
|
|
|
...field.options,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-11-11 20:57:18 +08:00
|
|
|
export default async (ctx, next) => {
|
|
|
|
const { resourceName, resourceKey } = ctx.action.params;
|
|
|
|
const [View, Field, Action] = ctx.db.getModels(['views', 'fields', 'actions']) as ModelCtor<Model>[];
|
|
|
|
const view = await View.findOne(View.parseApiJson({
|
|
|
|
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
|
|
|
}));
|
|
|
|
const collection = await view.getCollection();
|
2020-11-19 21:12:15 +08:00
|
|
|
const fields = await collection.getFields({
|
|
|
|
order: [
|
|
|
|
['sort', 'asc'],
|
|
|
|
]
|
|
|
|
});
|
|
|
|
const actions = await collection.getActions({
|
|
|
|
order: [
|
|
|
|
['sort', 'asc'],
|
|
|
|
]
|
|
|
|
});
|
2020-11-11 20:57:18 +08:00
|
|
|
const actionNames = view.options.actionNames||[];
|
|
|
|
console.log(view.options);
|
|
|
|
if (view.type === 'table') {
|
|
|
|
const defaultTabs = await collection.getTabs({
|
|
|
|
where: {
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
view.setDataValue('defaultTabName', get(defaultTabs, [0, 'name']));
|
|
|
|
}
|
2020-11-13 22:01:14 +08:00
|
|
|
if (view.options.updateViewName) {
|
2020-11-11 20:57:18 +08:00
|
|
|
view.setDataValue('rowViewName', view.options.updateViewName);
|
|
|
|
}
|
|
|
|
view.setDataValue('viewCollectionName', view.collection_name);
|
|
|
|
ctx.body = {
|
|
|
|
...view.toJSON(),
|
|
|
|
...(view.options||{}),
|
2020-11-13 22:01:14 +08:00
|
|
|
fields: await (transforms[view.type]||transforms.table)(fields),
|
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();
|
|
|
|
};
|