tachybase_todo/packages/plugin-ui-schema/src/actions/index.ts

144 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-09-11 18:53:26 +08:00
import { actions, middlewares, Context, Next } from '@nocobase/actions';
2021-07-17 22:35:50 +08:00
import { cloneDeep, omit } from 'lodash';
2021-09-11 18:53:26 +08:00
export const create = async (ctx: Context, next: Next) => {
2021-07-17 22:35:50 +08:00
const values = cloneDeep(ctx.action.params.values);
ctx.action.mergeParams(
{
2021-09-11 18:53:26 +08:00
values: cloneDeep(
omit(values, [
'__insertAfter__',
'__insertBefore__',
'__prepend__',
'_isJSONSchemaObject',
]),
),
2021-07-17 22:35:50 +08:00
},
{
payload: 'replace',
},
);
2021-09-11 18:53:26 +08:00
await actions.create(ctx, async () => {});
2021-07-18 17:16:57 +08:00
const sticky = values['__prepend__'];
2021-07-17 22:35:50 +08:00
const targetKey = values['__insertAfter__'] || values['__insertBefore__'];
2021-07-18 17:16:57 +08:00
if (sticky || targetKey) {
2021-07-19 23:57:54 +08:00
const body = ctx.body;
ctx.action.mergeParams(
{
associatedKey: values.parentKey,
resourceKey: body.key,
2021-09-11 18:53:26 +08:00
...(sticky
? {
sticky: true,
}
: {
method: values['__insertAfter__'] ? 'insertAfter' : null,
targetId: targetKey,
}),
2021-07-17 22:35:50 +08:00
},
2021-07-19 23:57:54 +08:00
{
payload: 'replace',
},
);
2021-09-11 18:53:26 +08:00
await middlewares.associated(ctx, async () => {});
await actions.sort(ctx, async () => {});
2021-07-19 23:57:54 +08:00
}
await next();
};
2021-09-11 18:53:26 +08:00
export const update = async (ctx: Context, next: Next) => {
2021-07-19 23:57:54 +08:00
const values = cloneDeep(ctx.action.params.values);
ctx.action.mergeParams(
{
2021-09-11 18:53:26 +08:00
values: cloneDeep(
omit(values, [
'__insertAfter__',
'__insertBefore__',
'__prepend__',
'_isJSONSchemaObject',
]),
),
2021-07-19 23:57:54 +08:00
},
{
payload: 'replace',
},
);
2021-09-11 18:53:26 +08:00
await actions.update(ctx, async () => {});
2021-07-19 23:57:54 +08:00
const sticky = values['__prepend__'];
const targetKey = values['__insertAfter__'] || values['__insertBefore__'];
if (sticky || targetKey) {
const body = ctx.body;
2021-07-17 22:35:50 +08:00
ctx.action.mergeParams(
{
associatedKey: values.parentKey,
2021-07-19 23:57:54 +08:00
resourceKey: body.key,
2021-09-11 18:53:26 +08:00
...(sticky
? {
sticky: true,
}
: {
method: values['__insertAfter__'] ? 'insertAfter' : null,
// insertAfter: !!values['__insertAfter__'],
targetId: targetKey,
}),
2021-07-17 22:35:50 +08:00
},
{
payload: 'replace',
},
);
2021-07-23 12:34:15 +08:00
// console.log(ctx.action.params.values);
2021-09-11 18:53:26 +08:00
await middlewares.associated(ctx, async () => {});
await actions.sort(ctx, async () => {});
2021-07-23 12:34:15 +08:00
// console.log(ctx.body.toJSON());
2021-07-17 22:35:50 +08:00
}
await next();
};
2021-09-11 18:53:26 +08:00
export const getTree = async (ctx: Context, next: Next) => {
2021-07-17 22:35:50 +08:00
const { resourceKey, filter } = ctx.action.params;
const UISchema = ctx.db.getModel('ui_schemas');
if (resourceKey) {
const schema = await UISchema.findByPk(resourceKey);
const property = schema.toProperty();
const properties = await schema.getProperties();
if (Object.keys(properties).length) {
property.properties = properties;
}
ctx.body = property;
} else {
const schemas = await UISchema.findAll(
UISchema.parseApiJson({
filter,
sort: ['sort'],
}),
);
2021-07-23 12:34:15 +08:00
// console.log({ schemas });
2021-07-17 22:35:50 +08:00
let properties = {};
for (const schema of schemas) {
const property = schema.toProperty();
const childProperties = await schema.getProperties();
if (Object.keys(childProperties).length) {
property.properties = childProperties;
}
properties[property.name] = property;
}
ctx.body = {
type: 'object',
properties,
};
}
await next();
};
2021-08-11 14:27:45 +08:00
2021-09-11 18:53:26 +08:00
export const getMenuItems = async (ctx: Context, next: Next) => {
2021-08-11 14:27:45 +08:00
const UISchema = ctx.db.getModel('ui_schemas');
const schema = await UISchema.findOne({
where: {
'x-component': 'Menu',
2021-09-11 18:53:26 +08:00
},
2021-08-11 14:27:45 +08:00
});
ctx.body = await schema.getHierarchy();
await next();
};