From 276d2183575a48860cedd8fbb60ae588c0392fc4 Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 8 Dec 2020 14:33:28 +0800 Subject: [PATCH] feat: add ctx.state.developerMode --- packages/app/src/api/index.ts | 3 ++- .../src/collections/actions.ts | 10 +++++++++ .../src/collections/collections.ts | 1 + .../src/collections/fields.ts | 2 ++ .../src/collections/tabs.ts | 2 +- .../plugin-pages/src/actions/getCollection.ts | 2 +- .../plugin-pages/src/actions/getRoutes.ts | 2 +- packages/plugin-pages/src/actions/getView.ts | 22 ++++++++++++++----- 8 files changed, 35 insertions(+), 9 deletions(-) diff --git a/packages/app/src/api/index.ts b/packages/app/src/api/index.ts index aa8e594c8..5d96342a1 100644 --- a/packages/app/src/api/index.ts +++ b/packages/app/src/api/index.ts @@ -37,8 +37,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 = false; if (table && table.hasField('developerMode')) { - ctx.action.setParam('filter.developerMode', false); + ctx.action.setParam('filter.developerMode', ctx.state.developerMode); } await next(); }); diff --git a/packages/plugin-collections/src/collections/actions.ts b/packages/plugin-collections/src/collections/actions.ts index 39ce65b97..537cb4f8c 100644 --- a/packages/plugin-collections/src/collections/actions.ts +++ b/packages/plugin-collections/src/collections/actions.ts @@ -77,6 +77,16 @@ export default { type: 'drawerSelect', }, }, + { + interface: 'boolean', + type: 'boolean', + name: 'developerMode', + title: '开发者模式', + defaultValue: false, + component: { + type: 'boolean', + }, + }, { interface: 'json', type: 'json', diff --git a/packages/plugin-collections/src/collections/collections.ts b/packages/plugin-collections/src/collections/collections.ts index 49841c932..a2dc69289 100644 --- a/packages/plugin-collections/src/collections/collections.ts +++ b/packages/plugin-collections/src/collections/collections.ts @@ -38,6 +38,7 @@ export default { interface: 'string', type: 'string', name: 'name', + createOnly: true, title: '标识', unique: true, required: true, diff --git a/packages/plugin-collections/src/collections/fields.ts b/packages/plugin-collections/src/collections/fields.ts index 1382396a9..133e0d796 100644 --- a/packages/plugin-collections/src/collections/fields.ts +++ b/packages/plugin-collections/src/collections/fields.ts @@ -40,6 +40,7 @@ export default { name: 'name', title: '标识', required: true, + createOnly: true, component: { type: 'string', showInTable: true, @@ -92,6 +93,7 @@ export default { type: 'string', name: 'type', title: '数据类型', + developerMode: true, component: { type: 'string', showInTable: true, diff --git a/packages/plugin-collections/src/collections/tabs.ts b/packages/plugin-collections/src/collections/tabs.ts index 107c3ba06..62b13e5c9 100644 --- a/packages/plugin-collections/src/collections/tabs.ts +++ b/packages/plugin-collections/src/collections/tabs.ts @@ -52,7 +52,7 @@ export default { dataSource: [ { label: '详情数据', value: 'details' }, { label: '相关数据', value: 'association' }, - { label: '模块组合', value: 'module' }, + { label: '模块组合', value: 'module', disabled: true }, ], component: { type: 'radio', diff --git a/packages/plugin-pages/src/actions/getCollection.ts b/packages/plugin-pages/src/actions/getCollection.ts index 3ba3e9879..59b762eca 100644 --- a/packages/plugin-pages/src/actions/getCollection.ts +++ b/packages/plugin-pages/src/actions/getCollection.ts @@ -22,7 +22,7 @@ export default async (ctx, next) => { collection.setDataValue('defaultViewName', get(views, [0, 'name'])); const tabs = await collection.getTabs({ where: { - developerMode: false, + developerMode: ctx.state.developerMode, }, order: [['sort', 'asc']], }) as Model[]; diff --git a/packages/plugin-pages/src/actions/getRoutes.ts b/packages/plugin-pages/src/actions/getRoutes.ts index cf6c27fe4..252e993cd 100644 --- a/packages/plugin-pages/src/actions/getRoutes.ts +++ b/packages/plugin-pages/src/actions/getRoutes.ts @@ -36,7 +36,7 @@ export default async function getRoutes(ctx, next) { const Page = database.getModel('pages'); let pages = await Page.findAll({ where: { - developerMode: false, + developerMode: ctx.state.developerMode, }, order: [['sort', 'asc']], }); diff --git a/packages/plugin-pages/src/actions/getView.ts b/packages/plugin-pages/src/actions/getView.ts index 2f9293d56..06c8a8f3d 100644 --- a/packages/plugin-pages/src/actions/getView.ts +++ b/packages/plugin-pages/src/actions/getView.ts @@ -1,9 +1,9 @@ import { ResourceOptions } from '@nocobase/resourcer'; import { Model, ModelCtor } from '@nocobase/database'; -import { get } from 'lodash'; +import { get, set } from 'lodash'; const transforms = { - table: async (fields: Model[]) => { + table: async (fields: Model[], context?: any) => { const arr = []; for (const field of fields) { if (!get(field.component, 'showInTable')) { @@ -17,7 +17,8 @@ const transforms = { } return arr; }, - form: async (fields: Model[]) => { + form: async (fields: Model[], ctx?: any) => { + const mode = get(ctx.action.params, ['values', 'mode'], ctx.action.params.mode); const schema = {}; for (const field of fields) { if (!get(field.component, 'showInForm')) { @@ -32,6 +33,9 @@ const transforms = { if (type === 'select') { prop.type = 'string' } + if (mode === 'update' && field.get('createOnly')) { + set(prop, 'x-component-props.disabled', true); + } const defaultValue = get(field.options, 'defaultValue'); if (defaultValue) { prop.default = defaultValue; @@ -45,7 +49,7 @@ const transforms = { } return schema; }, - details: async (fields: Model[]) => { + details: async (fields: Model[], context?: any) => { const arr = []; for (const field of fields) { if (!get(field.component, 'showInDetail')) { @@ -72,13 +76,20 @@ export default async (ctx, next) => { // appends: ['actions', 'fields'], // }, })); + // console.log('getView', ctx.action.params, mode); const collection = await view.getCollection(); const fields = await collection.getFields({ + where: { + developerMode: ctx.state.developerMode, + }, order: [ ['sort', 'asc'], ] }); const actions = await collection.getActions({ + where: { + developerMode: ctx.state.developerMode, + }, order: [ ['sort', 'asc'], ] @@ -100,7 +111,8 @@ export default async (ctx, next) => { ctx.body = { ...view.toJSON(), ...(view.options||{}), - fields: await (transforms[view.type]||transforms.table)(fields), + ofs: fields, + fields: await (transforms[view.type]||transforms.table)(fields, ctx), actions: actions.filter(action => actionNames.includes(action.name)).map(action => ({ ...action.toJSON(), ...action.options,