From 4decab86bed444b0f832b4a74e353f38db74a5fc Mon Sep 17 00:00:00 2001 From: chenos Date: Sat, 12 Dec 2020 16:36:02 +0800 Subject: [PATCH] feat: pages actions & tabs --- packages/app/src/api/index.ts | 4 ++- .../src/collections/tabs.ts | 14 +++++++-- .../src/hooks/collections-after-create.ts | 20 ++++++------ .../plugin-pages/src/actions/getCollection.ts | 31 +++++++------------ .../plugin-pages/src/actions/getPageInfo.ts | 2 +- packages/plugin-pages/src/actions/getView.ts | 6 ++-- packages/plugin-pages/src/server.ts | 25 +++++++++++++-- 7 files changed, 63 insertions(+), 39 deletions(-) diff --git a/packages/app/src/api/index.ts b/packages/app/src/api/index.ts index 5d96342a1..6359cd55c 100644 --- a/packages/app/src/api/index.ts +++ b/packages/app/src/api/index.ts @@ -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(); diff --git a/packages/plugin-collections/src/collections/tabs.ts b/packages/plugin-collections/src/collections/tabs.ts index d57daaa44..19989d70f 100644 --- a/packages/plugin-collections/src/collections/tabs.ts +++ b/packages/plugin-collections/src/collections/tabs.ts @@ -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, diff --git a/packages/plugin-collections/src/hooks/collections-after-create.ts b/packages/plugin-collections/src/hooks/collections-after-create.ts index 08aad2534..9b162b01b 100644 --- a/packages/plugin-collections/src/hooks/collections-after-create.ts +++ b/packages/plugin-collections/src/hooks/collections-after-create.ts @@ -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, diff --git a/packages/plugin-pages/src/actions/getCollection.ts b/packages/plugin-pages/src/actions/getCollection.ts index 59b762eca..9d6c0c272 100644 --- a/packages/plugin-pages/src/actions/getCollection.ts +++ b/packages/plugin-pages/src/actions/getCollection.ts @@ -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); } diff --git a/packages/plugin-pages/src/actions/getPageInfo.ts b/packages/plugin-pages/src/actions/getPageInfo.ts index d1725f827..cb59809c0 100644 --- a/packages/plugin-pages/src/actions/getPageInfo.ts +++ b/packages/plugin-pages/src/actions/getPageInfo.ts @@ -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(); diff --git a/packages/plugin-pages/src/actions/getView.ts b/packages/plugin-pages/src/actions/getView.ts index dfbf06ceb..a544506eb 100644 --- a/packages/plugin-pages/src/actions/getView.ts +++ b/packages/plugin-pages/src/actions/getView.ts @@ -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, }); } diff --git a/packages/plugin-pages/src/server.ts b/packages/plugin-pages/src/server.ts index 6d412fe09..7e47fe7eb 100644 --- a/packages/plugin-pages/src/server.ts +++ b/packages/plugin-pages/src/server.ts @@ -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')}`, + }, + }); + }); }