feat: add ctx.state.developerMode

This commit is contained in:
chenos 2020-12-08 14:33:28 +08:00
parent 7467441276
commit 276d218357
8 changed files with 35 additions and 9 deletions

View File

@ -37,8 +37,9 @@ const api = Api.create({
api.resourcer.use(async (ctx, next) => { api.resourcer.use(async (ctx, next) => {
const { resourceName } = ctx.action.params; const { resourceName } = ctx.action.params;
const table = ctx.db.getTable(resourceName); const table = ctx.db.getTable(resourceName);
ctx.state.developerMode = false;
if (table && table.hasField('developerMode')) { if (table && table.hasField('developerMode')) {
ctx.action.setParam('filter.developerMode', false); ctx.action.setParam('filter.developerMode', ctx.state.developerMode);
} }
await next(); await next();
}); });

View File

@ -77,6 +77,16 @@ export default {
type: 'drawerSelect', type: 'drawerSelect',
}, },
}, },
{
interface: 'boolean',
type: 'boolean',
name: 'developerMode',
title: '开发者模式',
defaultValue: false,
component: {
type: 'boolean',
},
},
{ {
interface: 'json', interface: 'json',
type: 'json', type: 'json',

View File

@ -38,6 +38,7 @@ export default {
interface: 'string', interface: 'string',
type: 'string', type: 'string',
name: 'name', name: 'name',
createOnly: true,
title: '标识', title: '标识',
unique: true, unique: true,
required: true, required: true,

View File

@ -40,6 +40,7 @@ export default {
name: 'name', name: 'name',
title: '标识', title: '标识',
required: true, required: true,
createOnly: true,
component: { component: {
type: 'string', type: 'string',
showInTable: true, showInTable: true,
@ -92,6 +93,7 @@ export default {
type: 'string', type: 'string',
name: 'type', name: 'type',
title: '数据类型', title: '数据类型',
developerMode: true,
component: { component: {
type: 'string', type: 'string',
showInTable: true, showInTable: true,

View File

@ -52,7 +52,7 @@ export default {
dataSource: [ dataSource: [
{ label: '详情数据', value: 'details' }, { label: '详情数据', value: 'details' },
{ label: '相关数据', value: 'association' }, { label: '相关数据', value: 'association' },
{ label: '模块组合', value: 'module' }, { label: '模块组合', value: 'module', disabled: true },
], ],
component: { component: {
type: 'radio', type: 'radio',

View File

@ -22,7 +22,7 @@ export default async (ctx, next) => {
collection.setDataValue('defaultViewName', get(views, [0, 'name'])); collection.setDataValue('defaultViewName', get(views, [0, 'name']));
const tabs = await collection.getTabs({ const tabs = await collection.getTabs({
where: { where: {
developerMode: false, developerMode: ctx.state.developerMode,
}, },
order: [['sort', 'asc']], order: [['sort', 'asc']],
}) as Model[]; }) as Model[];

View File

@ -36,7 +36,7 @@ export default async function getRoutes(ctx, next) {
const Page = database.getModel('pages'); const Page = database.getModel('pages');
let pages = await Page.findAll({ let pages = await Page.findAll({
where: { where: {
developerMode: false, developerMode: ctx.state.developerMode,
}, },
order: [['sort', 'asc']], order: [['sort', 'asc']],
}); });

View File

@ -1,9 +1,9 @@
import { ResourceOptions } from '@nocobase/resourcer'; import { ResourceOptions } from '@nocobase/resourcer';
import { Model, ModelCtor } from '@nocobase/database'; import { Model, ModelCtor } from '@nocobase/database';
import { get } from 'lodash'; import { get, set } from 'lodash';
const transforms = { const transforms = {
table: async (fields: Model[]) => { table: async (fields: Model[], context?: any) => {
const arr = []; const arr = [];
for (const field of fields) { for (const field of fields) {
if (!get(field.component, 'showInTable')) { if (!get(field.component, 'showInTable')) {
@ -17,7 +17,8 @@ const transforms = {
} }
return arr; 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 = {}; const schema = {};
for (const field of fields) { for (const field of fields) {
if (!get(field.component, 'showInForm')) { if (!get(field.component, 'showInForm')) {
@ -32,6 +33,9 @@ const transforms = {
if (type === 'select') { if (type === 'select') {
prop.type = 'string' prop.type = 'string'
} }
if (mode === 'update' && field.get('createOnly')) {
set(prop, 'x-component-props.disabled', true);
}
const defaultValue = get(field.options, 'defaultValue'); const defaultValue = get(field.options, 'defaultValue');
if (defaultValue) { if (defaultValue) {
prop.default = defaultValue; prop.default = defaultValue;
@ -45,7 +49,7 @@ const transforms = {
} }
return schema; return schema;
}, },
details: async (fields: Model[]) => { details: async (fields: Model[], context?: any) => {
const arr = []; const arr = [];
for (const field of fields) { for (const field of fields) {
if (!get(field.component, 'showInDetail')) { if (!get(field.component, 'showInDetail')) {
@ -72,13 +76,20 @@ export default async (ctx, next) => {
// appends: ['actions', 'fields'], // appends: ['actions', 'fields'],
// }, // },
})); }));
// console.log('getView', ctx.action.params, mode);
const collection = await view.getCollection(); const collection = await view.getCollection();
const fields = await collection.getFields({ const fields = await collection.getFields({
where: {
developerMode: ctx.state.developerMode,
},
order: [ order: [
['sort', 'asc'], ['sort', 'asc'],
] ]
}); });
const actions = await collection.getActions({ const actions = await collection.getActions({
where: {
developerMode: ctx.state.developerMode,
},
order: [ order: [
['sort', 'asc'], ['sort', 'asc'],
] ]
@ -100,7 +111,8 @@ export default async (ctx, next) => {
ctx.body = { ctx.body = {
...view.toJSON(), ...view.toJSON(),
...(view.options||{}), ...(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 => ({ actions: actions.filter(action => actionNames.includes(action.name)).map(action => ({
...action.toJSON(), ...action.toJSON(),
...action.options, ...action.options,