feat: pages actions & tabs

This commit is contained in:
chenos 2020-12-12 16:36:02 +08:00
parent 2dbbe29223
commit 4decab86be
7 changed files with 63 additions and 39 deletions

View File

@ -3,6 +3,7 @@ import dotenv from 'dotenv';
import path from 'path';
import actions from '../../../actions/src';
import associated from '../../../actions/src/middlewares/associated';
import { Op } from 'sequelize';
const sync = {
force: true,
@ -37,8 +38,9 @@ const api = Api.create({
api.resourcer.use(async (ctx, next) => {
const { resourceName } = ctx.action.params;
const table = ctx.db.getTable(resourceName);
// ctx.state.developerMode = {[Op.not]: null};
ctx.state.developerMode = false;
if (table && table.hasField('developerMode')) {
if (table && table.hasField('developerMode') && ctx.state.developerMode === false) {
ctx.action.setParam('filter.developerMode', ctx.state.developerMode);
}
await next();

View File

@ -25,6 +25,7 @@ export default {
type: 'string',
name: 'title',
title: '名称',
required: true,
component: {
type: 'string',
className: 'drag-visible',
@ -52,7 +53,7 @@ export default {
title: '类型',
dataSource: [
{ label: '详情数据', value: 'details' },
{ label: '相关数据', value: 'association' },
{ label: '相关数据', value: 'association', disabled: true },
{ label: '模块组合', value: 'module', disabled: true },
],
component: {
@ -60,6 +61,13 @@ export default {
showInTable: true,
showInDetail: true,
showInForm: true,
"x-linkages": [
{
"type": "value:visible",
"target": "association",
"condition": "{{ $self.value === 'association' }}"
},
],
},
},
{
@ -90,8 +98,8 @@ export default {
interface: 'boolean',
type: 'boolean',
name: 'enabled',
title: '启',
defaultValue: false,
title: '启',
defaultValue: true,
component: {
type: 'checkbox',
showInTable: true,

View File

@ -46,19 +46,19 @@ const defaultValues = {
actionNames: ['update'],
developerMode: true,
},
{
type: 'table',
name: 'simple',
title: '简易模式',
template: 'SimpleTable',
actionNames: ['create', 'destroy'],
detailsViewName: 'details',
updateViewName: 'form',
},
// {
// type: 'table',
// name: 'simple',
// title: '简易模式',
// template: 'SimpleTable',
// actionNames: ['create', 'destroy'],
// detailsViewName: 'details',
// updateViewName: 'form',
// },
{
type: 'table',
name: 'table',
title: '列表',
title: '全部数据',
template: 'Table',
actionNames: ['filter', 'create', 'destroy'],
default: true,

View File

@ -1,6 +1,4 @@
import { Model, ModelCtor } from '@nocobase/database';
import { ResourceOptions } from '@nocobase/resourcer';
import { get } from 'lodash';
export default async (ctx, next) => {
const { resourceName, resourceKey } = ctx.action.params;
@ -9,19 +7,18 @@ export default async (ctx, next) => {
filter: {
name: resourceName,
},
// fields: {
// // appends: ['tabs'],
// },
}));
const views = await collection.getViews({
const defaultView = await collection.getViews({
where: {
default: true,
},
limit: 1,
plain: true,
});
collection.setDataValue('defaultViewId', get(views, [0, 'id']));
collection.setDataValue('defaultViewName', get(views, [0, 'name']));
collection.setDataValue('defaultViewName', defaultView.get('name'));
const tabs = await collection.getTabs({
where: {
enabled: true,
developerMode: ctx.state.developerMode,
},
order: [['sort', 'asc']],
@ -29,23 +26,19 @@ export default async (ctx, next) => {
const tabItems = [];
for (const tab of tabs) {
const itemTab = {
...tab.toJSON(),
...tab.options,
...tab.get(),
};
if (itemTab.type === 'details' && !itemTab.viewName) {
itemTab.viewName = 'details';
}
if (itemTab.type == 'association') {
const field = await Field.findOne({
itemTab.field = await collection.getFields({
where: {
collection_name: itemTab.collection_name,
name: itemTab.association,
},
limit: 1,
plain: true,
});
itemTab.field = field ? {
...field.toJSON(),
...field.options,
} : {};
itemTab.viewCollectionName = itemTab.association;
} else {
itemTab.viewCollectionName = itemTab.collection_name;
}
tabItems.push(itemTab);
}

View File

@ -14,7 +14,7 @@ export default async (ctx, next) => {
order: [['sort', 'asc']],
});
ctx.body = {
pageTitle: field ? model.get(field.get('name')) : model.get(M.primaryKeyAttribute),
pageTitle: field ? (model.get(field.get('name')) || `#${model.get(M.primaryKeyAttribute)} 无标题`) : model.get(M.primaryKeyAttribute),
...model.toJSON(),
};
await next();

View File

@ -6,12 +6,12 @@ const transforms = {
table: async (fields: Model[], context?: any) => {
const arr = [];
for (const field of fields) {
if (!get(field.component, 'showInTable')) {
if (!field.get('component.showInTable')) {
continue;
}
arr.push({
...field.toJSON(),
...field.options,
...field.get(),
sorter: field.get('sortable'),
dataIndex: field.name,
});
}

View File

@ -21,16 +21,22 @@ export default async function (options = {}) {
const [Collection, Page] = database.getModels(['collections', 'pages']);
async function createCollectionPage(model) {
async function createCollectionPage(model, options) {
// const {
// transaction = await database.sequelize.transaction(),
// } = options;
if (model.get('internal')) {
return;
}
const transaction = await database.sequelize.transaction();
const parent = await Page.findOne({
transaction,
where: {
path: '/collections',
}
});
let page = await Page.findOne({
transaction,
where: {
collection: model.get('name'),
path: `/collections/${model.get('name')}`,
@ -43,6 +49,8 @@ export default async function (options = {}) {
path: `/collections/${model.get('name')}`,
sort: 100,
parent_id: parent.id,
}, {
transaction,
});
}
page.set({
@ -50,9 +58,22 @@ export default async function (options = {}) {
icon: model.get('icon'),
showInMenu: !!model.get('showInDataMenu'),
});
page.save();
await page.save({
transaction,
});
await transaction.commit();
}
Collection.addHook('afterCreate', createCollectionPage);
Collection.addHook('afterUpdate', createCollectionPage);
Collection.addHook('afterDestroy', async (model, options) => {
const { transaction } = options;
console.log('afterDestroy', model);
await Page.destroy({
transaction,
where: {
path: `/collections/${model.get('name')}`,
},
});
});
}