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

101 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-07-17 22:35:50 +08:00
import { Model, ModelCtor } from '@nocobase/database';
import { actions, middlewares } from '@nocobase/actions';
import { sort } from '@nocobase/actions/src/actions/common';
import { cloneDeep, omit } from 'lodash';
export const create = async (ctx: actions.Context, next: actions.Next) => {
const values = cloneDeep(ctx.action.params.values);
ctx.action.mergeParams(
{
values: omit(values, [
'__insertAfter__',
'__insertBefore__',
'__prepend__',
'_isJSONSchemaObject',
]),
},
{
payload: 'replace',
},
);
2021-07-18 17:16:57 +08:00
await actions.common.create(ctx, async () => { });
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-17 22:35:50 +08:00
console.log({
associatedKey: values.parentKey,
resourceKey: ctx.body.key,
2021-07-18 17:16:57 +08:00
values: sticky ? {
2021-07-17 22:35:50 +08:00
field: 'sort',
2021-07-18 17:16:57 +08:00
sticky: true,
} : {
field: 'sort',
insertAfter: !!values['__insertAfter__'],
2021-07-17 22:35:50 +08:00
target: {
key: targetKey,
},
},
});
ctx.action.mergeParams(
{
associatedKey: values.parentKey,
resourceKey: ctx.body.key,
2021-07-18 17:16:57 +08:00
values: sticky ? {
field: 'sort',
sticky: true,
target: {},
} : {
2021-07-17 22:35:50 +08:00
field: 'sort',
insertAfter: !!values['__insertAfter__'],
target: {
key: targetKey,
},
},
},
{
payload: 'replace',
},
);
2021-07-18 17:16:57 +08:00
await middlewares.associated(ctx, async () => { });
await sort(ctx, async () => { });
2021-07-17 22:35:50 +08:00
}
await next();
};
export const getTree = async (ctx: actions.Context, next: actions.Next) => {
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'],
}),
);
console.log({ schemas });
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();
};