UNPKG

@doreamonjs/page-creator

Version:
1,010 lines 57.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.create = void 0; const doreamon_1 = require("@zodash/doreamon"); const doreamonJSSDK = require("@doreamonjs/sdk"); function createRules(field) { const rules = []; const type = field.dataType; const uiType = field.type; if (type === 'string') { rules.push({ type, min: field.minLength, max: field.maxLength, len: field.length, pattern: field.regexp, validator: field.validator, transform: field.transform, }); } else if (type === 'number') { rules.push({ type, min: field.minimum, max: field.maximum, validator: field.validator, transform: field.transform, }); } if (field.required) { rules.push({ required: true, message: `${field.title} 是必须的` }); } // ui type functional if (uiType === 'email') { rules.push({ type: 'email', message: `${field.title} 格式不正确` }); } else if (['phone', 'mobile', 'telphone'].includes(uiType)) { rules.push({ pattern: /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/, message: `${field.title} 格式不正确`, }); } else if (uiType === 'url') { rules.push({ type: 'url', message: `${field.title} 格式不正确` }); } else if (uiType === 'idcard') { rules.push({ pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: `${field.title} 格式不正确`, }); } return rules; } // interface Field { // } function createFormField(actionKey, field, apis, manifest) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0; let formField = { ...field, // title: field.title, dataIndex: (_a = field.key) !== null && _a !== void 0 ? _a : field.dataIndex, placeholder: field.title, // rule related dataType: field.type, // common required: field.required, // type string minLength: field.minLength, maxLength: field.maxLength, length: field.length, regexp: field.regexp, // type number minimum: field.minimum, maximum: field.maximum, // type enhanced validator: field.validator, transform: field.transform, // ui type: (_b = field.uiType) !== null && _b !== void 0 ? _b : field.type, // rollout @TODO rollout: (_r = (_p = (_l = (_g = (_f = (_e = (_d = (_c = field.ui) === null || _c === void 0 ? void 0 : _c.form) === null || _d === void 0 ? void 0 : _d.custom) === null || _e === void 0 ? void 0 : _e[actionKey]) === null || _f === void 0 ? void 0 : _f.rollout) !== null && _g !== void 0 ? _g : (_k = (_j = (_h = field.ui) === null || _h === void 0 ? void 0 : _h.form) === null || _j === void 0 ? void 0 : _j[actionKey]) === null || _k === void 0 ? void 0 : _k.rollout) !== null && _l !== void 0 ? _l : (_o = (_m = field.ui) === null || _m === void 0 ? void 0 : _m.form) === null || _o === void 0 ? void 0 : _o.rollout) !== null && _p !== void 0 ? _p : (_q = field.ui) === null || _q === void 0 ? void 0 : _q.rollout) !== null && _r !== void 0 ? _r : field.rollout, // ...doreamon_1.default.object.omit(((_s = field.ui) === null || _s === void 0 ? void 0 : _s.form) || {}, ['custom']), ...(_v = (_u = (_t = field.ui) === null || _t === void 0 ? void 0 : _t.form) === null || _u === void 0 ? void 0 : _u.custom) === null || _v === void 0 ? void 0 : _v[actionKey], }; // 开启新篇章,使用对象方式隔离组件属性 if (typeof formField[formField.type] === 'object') { formField = { ...formField, ...formField[formField.type], }; } if (!formField.rules) { formField.rules = createRules(formField); } else { formField.rules = [...formField.rules, ...createRules(formField)]; } if (formField.type === 'select') { if (formField.dataType === 'boolean') { if (typeof formField.initialValue === 'undefined') { formField.initialValue = false; } if (typeof formField.options === 'undefined') { formField.options = [ { label: '是', value: true }, { label: '否', value: false }, ]; } } if ((_w = formField.options) === null || _w === void 0 ? void 0 : _w.dict) { const uuid = (_y = (_x = formField.options) === null || _x === void 0 ? void 0 : _x.dict) === null || _y === void 0 ? void 0 : _y.uuid; if (!uuid) { throw new Error(`options.dict.uuid is required in ${manifest.path}#${manifest.namespace}#${field.key}`); } formField.options = { api: { method: 'GET', path: '/api/v1/dicts/:uuid', }, params: { uuid, }, }; } if ((_z = formField.options) === null || _z === void 0 ? void 0 : _z.api) { // if select + api, default searchable + clearable if (formField.searchable != false) { formField.searchable = true; formField.clearable = true; } formField.options = doreamon_1.default.immer.produce(formField.options, (draft) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j; // if not string, means it has been replace before if (typeof ((_a = formField.options) === null || _a === void 0 ? void 0 : _a.api) === 'string') { if (!!((_b = formField.options) === null || _b === void 0 ? void 0 : _b.api) && !(apis === null || apis === void 0 ? void 0 : apis[(_c = formField.options) === null || _c === void 0 ? void 0 : _c.api])) { throw new Error(`${(_d = formField.options) === null || _d === void 0 ? void 0 : _d.api} not found in apis`); } draft.api = (_f = apis === null || apis === void 0 ? void 0 : apis[(_e = formField.options) === null || _e === void 0 ? void 0 : _e.api]) !== null && _f !== void 0 ? _f : (_g = formField.options) === null || _g === void 0 ? void 0 : _g.api; if (typeof draft.api === 'string') { draft.api = { method: 'GET', path: draft.api, }; } } draft.searchKey = (_j = (_h = draft.search) === null || _h === void 0 ? void 0 : _h.key) !== null && _j !== void 0 ? _j : 'q'; }); } formField.option = doreamon_1.default.immer.produce(formField.option, (draft) => { var _a, _b, _c, _d; const optionApiName = (_c = (_b = (_a = formField.option) === null || _a === void 0 ? void 0 : _a.request) === null || _b === void 0 ? void 0 : _b.api) !== null && _c !== void 0 ? _c : (_d = formField.option) === null || _d === void 0 ? void 0 : _d.api; if (optionApiName) { const api = apis === null || apis === void 0 ? void 0 : apis[optionApiName]; if (!draft.request) { draft.request = {}; } if (!draft.response) { draft.response = {}; } if (typeof api === 'string') { draft.request.method = 'GET'; draft.request.path = api; } else if (typeof api === 'object') { draft.request.method = api.method; draft.request.path = api.path; if (!draft.request.query && api.query) { draft.request.query = api.query; } if (!draft.request.params && api.params) { draft.request.params = api.params; } if (!draft.request.body && api.body) { draft.request.body = api.body; } } else { throw new Error(`Cannot found api named ${optionApiName}`); } if (draft.transform) { draft.response.item = draft.transform.item; draft.response.label = draft.transform.label; draft.response.value = draft.transform.value; } } }); // add new if (formField.addNew) { if (typeof ((_0 = formField.addNew.service) === null || _0 === void 0 ? void 0 : _0.api) === 'string') { if (!(apis === null || apis === void 0 ? void 0 : apis[formField.addNew.service.api])) { throw new Error(`${formField.addNew.service.api} not found in apis for select.addNew`); } formField.addNew.service.api = apis === null || apis === void 0 ? void 0 : apis[formField.addNew.service.api]; } if (!formField.addNew.onAdd) { // @TODO // formField.addNew.onAdd = async (value: string) => { // }; } } // extra if (formField.extra) { if (formField.extra.action) { if (!formField.extra.action.namespace) { formField.extra.action.namespace = manifest.namespace; } } } } else if (formField.type === 'upload') { if (formField.dataType === 'array') { if (typeof formField.initialValue === 'undefined') { formField.initialValue = []; } formField.multiple = true; } else if (formField.dataType === 'string') { formField.multiple = false; } else { throw new Error(`upload type only support form data type array or string, but got ${formField.dataType}`); } } return formField; } function createSearchField(field, apis, manifest) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r; const attrs = { // default: clearable clearable: true, key: field.key, dataType: field.type, advanced: field.advanced, title: field.title, dataIndex: field.key, placeholder: field.title, isFuzzy: field.isFuzzy, rollout: (_e = (_c = (_b = (_a = field.ui) === null || _a === void 0 ? void 0 : _a.search) === null || _b === void 0 ? void 0 : _b.rollout) !== null && _c !== void 0 ? _c : (_d = field.ui) === null || _d === void 0 ? void 0 : _d.rollout) !== null && _e !== void 0 ? _e : field.rollout, ...(_f = field.ui) === null || _f === void 0 ? void 0 : _f.search, }; if (attrs.type === 'select') { // if select, set fuzzy = false attrs.isFuzzy = false; if ((_h = (_g = attrs.options) === null || _g === void 0 ? void 0 : _g.dict) === null || _h === void 0 ? void 0 : _h.uuid) { const uuid = attrs.options.dict.uuid; attrs.options = { api: { method: 'GET', path: '/api/v1/dicts/:uuid', }, params: { uuid, }, }; } if ((_j = attrs.options) === null || _j === void 0 ? void 0 : _j.api) { if (!!((_k = attrs.options) === null || _k === void 0 ? void 0 : _k.api) && typeof attrs.options.api === 'string' && !(apis === null || apis === void 0 ? void 0 : apis[attrs.options.api])) { throw new Error(`${(_l = attrs.options) === null || _l === void 0 ? void 0 : _l.api} not found in apis`); } attrs.options = { // if select + api, default searchable searchable: true, ...attrs.options, api: (_o = apis === null || apis === void 0 ? void 0 : apis[(_m = attrs.options) === null || _m === void 0 ? void 0 : _m.api]) !== null && _o !== void 0 ? _o : (_p = attrs.options) === null || _p === void 0 ? void 0 : _p.api, searchKey: (_r = (_q = attrs.options) === null || _q === void 0 ? void 0 : _q.search) === null || _r === void 0 ? void 0 : _r.key, }; if (typeof attrs.options.api === 'string') { attrs.options.api = { method: 'GET', path: attrs.options.api, }; } } } if (attrs.dataType === 'boolean') { if (!attrs.type) attrs.type = 'select'; if (!attrs.options) { attrs.options = [ { label: '是', value: true }, { label: '否', value: false }, ]; } } return attrs; } function createTableColumField(field, apis, manifest) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z; const table = (_b = (_a = field.ui) === null || _a === void 0 ? void 0 : _a.table) === null || _b === void 0 ? void 0 : _b.list; const form = (_c = field.ui) === null || _c === void 0 ? void 0 : _c.form; const formUIType = (form === null || form === void 0 ? void 0 : form.type) || ((_e = (_d = form === null || form === void 0 ? void 0 : form.custom) === null || _d === void 0 ? void 0 : _d.create) === null || _e === void 0 ? void 0 : _e.type) || ((_g = (_f = form === null || form === void 0 ? void 0 : form.custom) === null || _f === void 0 ? void 0 : _f.update) === null || _g === void 0 ? void 0 : _g.type); const column = { key: field.key, title: field.title, dataIndex: field.key, dataType: field.type, uiType: (_h = table === null || table === void 0 ? void 0 : table.type) !== null && _h !== void 0 ? _h : field.uiType, // able sortable: false, // rollout rollout: (_o = (_l = (_k = (_j = field.ui) === null || _j === void 0 ? void 0 : _j.table) === null || _k === void 0 ? void 0 : _k.rollout) !== null && _l !== void 0 ? _l : (_m = field.ui) === null || _m === void 0 ? void 0 : _m.rollout) !== null && _o !== void 0 ? _o : field.rollout, // category category: (_x = (_v = (_s = (_r = (_q = (_p = field.ui) === null || _p === void 0 ? void 0 : _p.table) === null || _q === void 0 ? void 0 : _q.list) === null || _r === void 0 ? void 0 : _r.category) !== null && _s !== void 0 ? _s : (_u = (_t = field.ui) === null || _t === void 0 ? void 0 : _t.table) === null || _u === void 0 ? void 0 : _u.category) !== null && _v !== void 0 ? _v : (_w = field.ui) === null || _w === void 0 ? void 0 : _w.category) !== null && _x !== void 0 ? _x : field.category, // ...table, }; // @TODO // 如果 dataType 是 object 且 form uiType 是 select // 需要设置 // id: { key: string } // 或 id: (value, item) => string // 或 getId({ value }) => string if (column.dataType === 'object' && formUIType === 'select') { if (column.getId) { // valid } else if (typeof column.id === 'function' || ((_y = column.id) === null || _y === void 0 ? void 0 : _y.key)) { // valid } else { throw new Error(`id: { key: string } 或 id: (value, item) => string 是 ${column.uiType} 是必须的 (页面(path: ${manifest.path}, namespace: ${manifest.namespace}) ${field.key} 字段的数据类型是 ${column.dataType} 且 UI 类型)`); } } if (column.dataType === 'boolean') { if (!column.type) column.type = 'select'; if (!((_z = column.select) === null || _z === void 0 ? void 0 : _z.options)) { if (!column.select) column.select = {}; column.select.options = [ { label: '是', value: true }, { label: '否', value: false }, ]; } } // if first column is id, set default with 56px if (column.key === 'id' && typeof column.width === 'undefined') { column.width = '56px'; } return column; } function createApis(path, apis, configs) { var _a, _b, _c, _d; const prefix = (_b = (_a = configs === null || configs === void 0 ? void 0 : configs.apis) === null || _a === void 0 ? void 0 : _a.prefix) !== null && _b !== void 0 ? _b : '/api'; const version = (_d = (_c = configs === null || configs === void 0 ? void 0 : configs.apis) === null || _c === void 0 ? void 0 : _c.version) !== null && _d !== void 0 ? _d : ''; const standardAPIs = {}; const builtIn = [ 'query', 'create', 'update', 'delete', // 'get', 'list', 'count', ].reduce((acc, key) => { let fullPath = `${prefix}/${version}${path}`; switch (key) { case 'query': break; case 'create': break; case 'update': fullPath = `${fullPath}/:id`; break; case 'delete': fullPath = `${fullPath}/:id`; break; case 'retrive': fullPath = `${fullPath}/:id`; break; default: throw new Error(`unknown built in apis key: ${key}`); } acc[key] = fullPath.replace(/\/\//g, '/'); return acc; }, {}); for (const [key, path] of Object.entries(builtIn)) { let method = 'GET'; switch (key) { case 'query': method = 'GET'; break; case 'create': method = 'POST'; break; case 'update': method = 'PUT'; break; case 'delete': method = 'DELETE'; break; case 'retrive': method = 'GET'; break; } standardAPIs[key] = { method, path, }; } // apis if (apis) { for (const [key, api] of Object.entries(apis !== null && apis !== void 0 ? apis : {})) { if (typeof api === 'string') { switch (key) { case 'query': // path only standardAPIs[key] = { method: 'GET', path: api, }; break; case 'create': // path only standardAPIs[key] = { method: 'POST', path: api, }; break; case 'update': // path only standardAPIs[key] = { method: 'PUT', path: api, }; break; case 'delete': // path only standardAPIs[key] = { method: 'DELETE', path: api, }; break; case 'retrive': // path only standardAPIs[key] = { method: 'GET', path: api, }; break; default: // path only standardAPIs[key] = { method: 'GET', path: api, }; } } else if (typeof api === 'object') { // { method, path } standardAPIs[key] = api; } else if (typeof api === 'function') { const builtAPI = api(); if (typeof builtAPI === 'string') { // path only standardAPIs[key] = { method: 'GET', path: builtAPI, }; } else if (typeof builtAPI === 'object') { // { method, path } standardAPIs[key] = builtAPI; } } else { throw new Error(`unknown apis type: ${typeof api} (${JSON.stringify(api)})`); } } } // // build params // const params: Record<string, string> = {}; // for (const [key, value] of Object.entries(configs?.apis?.params ?? {})) { // if (typeof value === 'string') { // params[key] = value; // } else if (typeof value === 'function') { // try { // const v = value(); // if (['string', 'number', 'boolean'].includes(typeof v)) { // params[key] = String(v); // } // } catch (error) { // // // } // } else { // throw new Error(`unknown params type: ${typeof value} (${JSON.stringify(value)})`); // } // } // console.log('params:', params); // for (const [_, api] of Object.entries(standardAPIs)) { // api.path = api.path // .replace(/:([^/]+)/g, (_, key) => { // return doreamon.object.get(params, key, `:${key}`); // }); // } return standardAPIs; } function create(manifest) { var _a, _b, _c, _d, _e, _f, _g, _h; const apis = createApis(manifest.path, manifest.apis, manifest.configs); const actions = createActions(manifest, apis); const filters = createFilters(manifest, apis); const columns = createColumns(manifest, apis); const configs = createConfigs(manifest, manifest.configs); const categories = createCategories(manifest, manifest.categories); const detail = createDetail(manifest, manifest.detail); if (!manifest.path) { throw new Error(`manifest.path is required`); } return { page: { ...(_a = manifest.configs) === null || _a === void 0 ? void 0 : _a.page, // path: manifest.path, namespace: (_b = manifest.namespace) !== null && _b !== void 0 ? _b : `p#${manifest.path}`, mode: (_e = (_d = (_c = manifest.configs) === null || _c === void 0 ? void 0 : _c.page) === null || _d === void 0 ? void 0 : _d.mode) !== null && _e !== void 0 ? _e : 'list', urlSearchUsedForListAPI: (_h = (_g = (_f = manifest.configs) === null || _f === void 0 ? void 0 : _f.page) === null || _g === void 0 ? void 0 : _g.urlSearchUsedForListAPI) !== null && _h !== void 0 ? _h : false, }, title: manifest.title, description: manifest.description, actions, filters, columns, apis, configs, categories, lifeCycle: manifest.lifeCycle, // detail, }; } exports.create = create; function createActions(manifest, apis) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63; const customActions = createCustomActions(manifest, apis); const allActions = [ { key: 'query', title: '查询', type: 'filter', buttonType: 'primary', direction: 'right', hidden: true, // sort: 1, // pipeline: [ ...(((_d = (_c = (_b = (_a = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _a === void 0 ? void 0 : _a.actions) === null || _b === void 0 ? void 0 : _b.runtime) === null || _c === void 0 ? void 0 : _c.query) === null || _d === void 0 ? void 0 : _d.prePipeline) || []), { type: 'service', service: { ...apis.query, quiet: true, }, }, ...(((_h = (_g = (_f = (_e = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _e === void 0 ? void 0 : _e.actions) === null || _f === void 0 ? void 0 : _f.runtime) === null || _g === void 0 ? void 0 : _g.query) === null || _h === void 0 ? void 0 : _h.postPipeline) || []), ], ...(_l = (_k = (_j = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _j === void 0 ? void 0 : _j.actions) === null || _k === void 0 ? void 0 : _k.runtime) === null || _l === void 0 ? void 0 : _l.query, }, { key: 'create', title: '创建', type: 'single', buttonType: 'primary', direction: 'right', // sort: 2, // pipeline: [ ...(((_q = (_p = (_o = (_m = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _m === void 0 ? void 0 : _m.actions) === null || _o === void 0 ? void 0 : _o.runtime) === null || _p === void 0 ? void 0 : _p.create) === null || _q === void 0 ? void 0 : _q.prePipeline) || []), { type: 'form', form: { type: (_x = (_u = (_t = (_s = (_r = manifest.configs) === null || _r === void 0 ? void 0 : _r.dialog) === null || _s === void 0 ? void 0 : _s.form) === null || _t === void 0 ? void 0 : _t.type) !== null && _u !== void 0 ? _u : (_w = (_v = manifest.configs) === null || _v === void 0 ? void 0 : _v.dialog) === null || _w === void 0 ? void 0 : _w.type) !== null && _x !== void 0 ? _x : 'modal', layout: (_1 = (_0 = (_z = (_y = manifest.configs) === null || _y === void 0 ? void 0 : _y.dialog) === null || _z === void 0 ? void 0 : _z.form) === null || _0 === void 0 ? void 0 : _0.layout) !== null && _1 !== void 0 ? _1 : 'horizontal', // title: '创建', rules: manifest === null || manifest === void 0 ? void 0 : manifest.rules, fields: (_2 = manifest.fields) === null || _2 === void 0 ? void 0 : _2.filter((e) => { var _a, _b, _c, _d; const visible = (_a = e.ui) === null || _a === void 0 ? void 0 : _a.visible; if (typeof (visible === null || visible === void 0 ? void 0 : visible.form) === 'boolean') { return visible.form; } if (typeof (visible === null || visible === void 0 ? void 0 : visible.default) !== 'undefined') { if (typeof ((_b = visible === null || visible === void 0 ? void 0 : visible.form) === null || _b === void 0 ? void 0 : _b['create']) !== 'undefined') { return (_c = visible === null || visible === void 0 ? void 0 : visible.form) === null || _c === void 0 ? void 0 : _c['create']; } return visible === null || visible === void 0 ? void 0 : visible.default; } return ((_d = visible === null || visible === void 0 ? void 0 : visible.form) === null || _d === void 0 ? void 0 : _d['create']) !== false; }).map((e) => createFormField('create', e, apis, manifest)).filter((e) => !e.rollout ? true : doreamonJSSDK.rollout.isRollout(e.rollout)), }, }, { type: 'service', service: apis.create, }, ...(((_6 = (_5 = (_4 = (_3 = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _3 === void 0 ? void 0 : _3.actions) === null || _4 === void 0 ? void 0 : _4.runtime) === null || _5 === void 0 ? void 0 : _5.create) === null || _6 === void 0 ? void 0 : _6.postPipeline) || []), ], ...(_9 = (_8 = (_7 = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _7 === void 0 ? void 0 : _7.actions) === null || _8 === void 0 ? void 0 : _8.runtime) === null || _9 === void 0 ? void 0 : _9.create, }, { key: 'refresh', title: '刷新', type: 'single', buttonType: 'default', direction: 'right', icon: 'reload', iconOnly: true, // sort: 10000, pipeline: [ { type: 'service', service: apis.query, }, ], ...(_12 = (_11 = (_10 = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _10 === void 0 ? void 0 : _10.actions) === null || _11 === void 0 ? void 0 : _11.runtime) === null || _12 === void 0 ? void 0 : _12.refresh, }, { key: 'update', title: '编辑', type: 'item', sort: 5, pipeline: [ ...(((_16 = (_15 = (_14 = (_13 = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _13 === void 0 ? void 0 : _13.actions) === null || _14 === void 0 ? void 0 : _14.runtime) === null || _15 === void 0 ? void 0 : _15.update) === null || _16 === void 0 ? void 0 : _16.prePipeline) || []), { type: 'form', form: { type: (_23 = (_20 = (_19 = (_18 = (_17 = manifest.configs) === null || _17 === void 0 ? void 0 : _17.dialog) === null || _18 === void 0 ? void 0 : _18.form) === null || _19 === void 0 ? void 0 : _19.type) !== null && _20 !== void 0 ? _20 : (_22 = (_21 = manifest.configs) === null || _21 === void 0 ? void 0 : _21.dialog) === null || _22 === void 0 ? void 0 : _22.type) !== null && _23 !== void 0 ? _23 : 'modal', layout: (_27 = (_26 = (_25 = (_24 = manifest.configs) === null || _24 === void 0 ? void 0 : _24.dialog) === null || _25 === void 0 ? void 0 : _25.form) === null || _26 === void 0 ? void 0 : _26.layout) !== null && _27 !== void 0 ? _27 : 'horizontal', // title: '编辑', rules: manifest === null || manifest === void 0 ? void 0 : manifest.rules, fields: (_28 = manifest.fields) === null || _28 === void 0 ? void 0 : _28.filter((e) => { var _a, _b, _c, _d; const visible = (_a = e.ui) === null || _a === void 0 ? void 0 : _a.visible; if (typeof (visible === null || visible === void 0 ? void 0 : visible.form) === 'boolean') { return visible.form; } if (typeof (visible === null || visible === void 0 ? void 0 : visible.default) !== 'undefined') { if (typeof ((_b = visible === null || visible === void 0 ? void 0 : visible.form) === null || _b === void 0 ? void 0 : _b['update']) !== 'undefined') { return (_c = visible === null || visible === void 0 ? void 0 : visible.form) === null || _c === void 0 ? void 0 : _c['update']; } return visible === null || visible === void 0 ? void 0 : visible.default; } return ((_d = visible === null || visible === void 0 ? void 0 : visible.form) === null || _d === void 0 ? void 0 : _d['update']) !== false; }).map((e) => createFormField('update', e, apis, manifest)).filter((e) => !e.rollout ? true : doreamonJSSDK.rollout.isRollout(e.rollout)), }, }, { type: 'service', service: apis.update, }, ...(((_32 = (_31 = (_30 = (_29 = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _29 === void 0 ? void 0 : _29.actions) === null || _30 === void 0 ? void 0 : _30.runtime) === null || _31 === void 0 ? void 0 : _31.update) === null || _32 === void 0 ? void 0 : _32.postPipeline) || []), ], ...(_35 = (_34 = (_33 = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _33 === void 0 ? void 0 : _33.actions) === null || _34 === void 0 ? void 0 : _34.runtime) === null || _35 === void 0 ? void 0 : _35.update, }, { // key: 'delete', title: '删除', type: 'item', buttonType: 'danger', sort: 1000, pipeline: [ ...(((_39 = (_38 = (_37 = (_36 = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _36 === void 0 ? void 0 : _36.actions) === null || _37 === void 0 ? void 0 : _37.runtime) === null || _38 === void 0 ? void 0 : _38.delete) === null || _39 === void 0 ? void 0 : _39.prePipeline) || []), { type: 'confirm', confirm: { type: (_46 = (_43 = (_42 = (_41 = (_40 = manifest.configs) === null || _40 === void 0 ? void 0 : _40.dialog) === null || _41 === void 0 ? void 0 : _41.confirm) === null || _42 === void 0 ? void 0 : _42.type) !== null && _43 !== void 0 ? _43 : (_45 = (_44 = manifest.configs) === null || _44 === void 0 ? void 0 : _44.dialog) === null || _45 === void 0 ? void 0 : _45.type) !== null && _46 !== void 0 ? _46 : 'modal', placement: (_51 = (_50 = (_49 = (_48 = (_47 = manifest.configs) === null || _47 === void 0 ? void 0 : _47.dialog) === null || _48 === void 0 ? void 0 : _48.confirm) === null || _49 === void 0 ? void 0 : _49.drawer) === null || _50 === void 0 ? void 0 : _50.placement) !== null && _51 !== void 0 ? _51 : 'right', width: (_56 = (_55 = (_54 = (_53 = (_52 = manifest.configs) === null || _52 === void 0 ? void 0 : _52.dialog) === null || _53 === void 0 ? void 0 : _53.confirm) === null || _54 === void 0 ? void 0 : _54.drawer) === null || _55 === void 0 ? void 0 : _55.width) !== null && _56 !== void 0 ? _56 : 700, title: `是否删除`, description: `此行为不可撤销。是否继续?`, cancelText: '取消', confirmText: '确认', }, }, { type: 'service', service: apis.delete, }, ...(((_60 = (_59 = (_58 = (_57 = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _57 === void 0 ? void 0 : _57.actions) === null || _58 === void 0 ? void 0 : _58.runtime) === null || _59 === void 0 ? void 0 : _59.delete) === null || _60 === void 0 ? void 0 : _60.postPipeline) || []), ], ...(_63 = (_62 = (_61 = manifest === null || manifest === void 0 ? void 0 : manifest.configs) === null || _61 === void 0 ? void 0 : _61.actions) === null || _62 === void 0 ? void 0 : _62.runtime) === null || _63 === void 0 ? void 0 : _63.delete, }, // custom actions ...customActions, ]; // // same key override // const indexedAllActions = allActions.reduce((all, action) => { // all[action.key] = action as any as ManifestAction; // return all; // }, {} as any as Record<string, ManifestAction>); return (allActions .map((action) => { return doreamon_1.default.immer.produce(action, (draft) => { var _a; // handle default direction draft.direction = (_a = draft.direction) !== null && _a !== void 0 ? _a : 'right'; // handle pipeline draft.pipeline = createPipeline(draft.pipeline, draft, apis, manifest); }); }) .map((action, index, all) => { var _a; const pipelineTo = (_a = action) === null || _a === void 0 ? void 0 : _a.pipelineTo; if (pipelineTo) { const actionKey = pipelineTo.action; const onBefore = pipelineTo.onBefore; const actionX = all.find((e) => e.key === actionKey); if (actionX) { return { ...action, pipeline: [ ...((action === null || action === void 0 ? void 0 : action.pipeline) || []), { type: 'values', values: { onBefore, }, }, ...(actionX.pipeline || []), ], }; } else { throw new Error(`unknown action: ${actionKey}`); } } return action; }) .filter((e) => { var _a; if ((_a = e) === null || _a === void 0 ? void 0 : _a.rollout) { return doreamonJSSDK.rollout.isRollout(e.rollout); } return true; }) // .filter((e) => { // if ((e as any as ManifestAction)?.permissions) { // return (e as any as ManifestAction).permissions?.some( // (e: string) => { // // build-in permission // const buildIntPermissions = { // '@@superadmin': () => doreamonJSSDK.user.isSuperAdmin(), // // '@@owner': (owner: string) => doreamonJSSDK.user.isCurrentUser(owner), // }; // if (!!buildIntPermissions[e]) { // return buildIntPermissions[e](); // } // return doreamonJSSDK.permission.hasPermission(e); // }, // ); // } // return true; // }) .filter((e) => { var _a, _b; const disable = (_b = (_a = manifest.configs) === null || _a === void 0 ? void 0 : _a.actions) === null || _b === void 0 ? void 0 : _b.disable; if (!(disable === null || disable === void 0 ? void 0 : disable.length)) { return e; } return !disable.includes(e.key); }) .sort((a, b) => { return a.sort - b.sort; }) .map((e) => { var _a; if (!((_a = e) === null || _a === void 0 ? void 0 : _a.permissions)) { return e; } // build-in permission const buildIntPermissions = { '@@superadmin': () => doreamonJSSDK.user.isSuperAdmin(), '@@owner': (owner) => doreamonJSSDK.user.isCurrentUser(owner), '@@email': (email, emails) => emails === null || emails === void 0 ? void 0 : emails.includes(email), }; const checkPermission = (e, values) => { var _a; return (_a = e.permissions) === null || _a === void 0 ? void 0 : _a.some((e) => { let permissionKey = e; let args; if (Array.isArray(e)) { permissionKey = e[0]; args = e.slice(1); } if (buildIntPermissions[permissionKey]) { let _args = args; let _args_rest = []; let operator = ''; if (typeof (args === null || args === void 0 ? void 0 : args[0]) === 'function') { _args = args[0](values); } else if (typeof (args === null || args === void 0 ? void 0 : args[0]) === 'string') { const key = args[0]; if (['not', '!'].includes(args[1])) { operator = args[1]; } else { _args_rest = args.slice(1); } if (key === undefined) { _args = values; } else { _args = doreamon_1.default.object.get(values, key); } } const ok = buildIntPermissions[permissionKey](_args, ..._args_rest); if (operator) { switch (operator) { case 'not': return !ok; default: return ok; } } else { return ok; } } return doreamonJSSDK.permission.hasPermission(permissionKey); }); }; const visible = (values, index, items, context) => { // @1 check permission if (checkPermission(e, values) == false) { return false; } // @2 check visible if (typeof e.visible !== 'undefined') { if (typeof e.visible === 'function') { return e.visible(values, index, items, context); } // bool return !!e.visible; } return true; }; return { ...e, visible, }; })); } function createPipeline(pipeline, action, apis, manifest) { if (!pipeline) return []; return pipeline.map((one) => { switch (one.type) { case 'confirm': return doreamon_1.default.immer.produce(one, (d) => { var _a, _b; if (!d.confirm) { d.confirm = {}; } if (!((_a = d.confirm) === null || _a === void 0 ? void 0 : _a.title)) { d.confirm.title = action.title; } if (!((_b = d.confirm) === null || _b === void 0 ? void 0 : _b.description)) { d.confirm.description = '是否确认?'; } }); case 'service': return doreamon_1.default.immer.produce(one, (d) => { if (typeof d.service.api === 'string') { d.service.api = apis[d.service.api]; } }); case 'preview': return doreamon_1.default.immer.produce(one, (d) => { var _a, _b, _c; if (typeof ((_a = d.preview) === null || _a === void 0 ? void 0 : _a.fields) === 'undefined') { if (typeof d.preview === 'undefined') { d.preview = {}; } d.preview.fields = (_b = manifest.fields) === null || _b === void 0 ? void 0 : _b.map((e) => ({ key: e.key, title: e.title, })); } // key => dataIndex for preview component if (Array.isArray((_c = d.preview) === null || _c === void 0 ? void 0 : _c.fields)) { d.preview.fields = d.preview.fields.map((e) => ({ ...e, dataIndex: e.key, })); } }); case 'dialog': if (typeof one.dialog.render === 'string') { const rendererName = one.dialog.render; return doreamon_1.default.immer.produce(one, (d) => { d.dialog.render = (...args) => { const render = window.doreamonjs.renders.get(rendererName); // console.log('doreamonjs.renders.get(rendererName):', render, args); return render.apply(null, args); }; }); } } return one; }); } function createFilters(manifest, apis) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; if (((_b = (_a = manifest.configs) === null || _a === void 0 ? void 0 : _a.actions) === null || _b === void 0 ? void 0 : _b.search) === false) return []; if (((_c = manifest.configs) === null || _c === void 0 ? void 0 : _c.searchable) === false) return []; if (((_e = (_d = manifest.configs) === null || _d === void 0 ? void 0 : _d.search) === null || _e === void 0 ? void 0 : _e.disabled) === true) return []; if (((_g = (_f = manifest.configs) === null || _f === void 0 ? void 0 : _f.search) === null || _g === void 0 ? void 0 : _g.mode) !== undefined && ((_j = (_h = manifest.configs) === null || _h === void 0 ? void 0 : _h.search) === null || _j === void 0 ? void 0 : _j.mode) !== 'default') return []; if (!((_k = manifest.fields) === null || _k === void 0 ? void 0 : _k.length)) return []; return (_l = manifest.fields) === null || _l === void 0 ? void 0 : _l.filter((e) => { var _a; const visible = (_a = e.ui) === null || _a === void 0 ? void 0 : _a.visible; if (typeof (visible === null || visible === void 0 ? void 0 : visible.search) === 'boolean') { return visible.search; } if (typeof (visible === null || visible === void 0 ? void 0 : visible.default) !== 'undefined') { if (typeof (visible === null || visible === void 0 ? void 0 : visible.search) !== 'undefined') { return visible === null || visible === void 0 ? void 0 : visible.search; } return visible === null || visible === void 0 ? void 0 : visible.default; } return (visible === null || visible === void 0 ? void 0 : visible.search) !== false; }).map((e) => createSearchField(e, apis, manifest)).filter((e) => { // rollout if (!e.rollout) { return true; } return doreamonJSSDK.rollout.isRollout(e.rollout); }).filter((e) => { var _a, _b, _c, _d, _e; if (!((_c = (_b = (_a = manifest.configs) === null || _a === void 0 ? void 0 : _a.filters) === null || _b === void 0 ? void 0 : _b.disable) === null || _c === void 0 ? void 0 : _c.length)) { return e; } return !((_e = (_d = manifest.configs) === null || _d === void 0 ? void 0 : _d.filters) === null || _e === void 0 ? void 0 : _e.disable.includes(e.key)); }); } function createColumns(manifest, apis) { var _a; if (!((_a = manifest.fields) === null || _a === void 0 ? void 0 : _a.length)) { return []; } return manifest.fields .filter((e) => { var _a, _b, _c, _d; // visible const visible = (_a = e.ui) === null || _a === void 0 ? void 0 : _a.visible; if (typeof (visible === null || visible === void 0 ? void 0 : visible.table) === 'boolean') { return visible.table; } if (typeof (visible === null || visible === void 0 ? void 0 : visible.default) !== 'undefined') { if (typeof ((_b = visible === null || visible === void 0 ? void 0 : visible.table) === null || _b === void 0 ? void 0 : _b.list) !== 'undefined') { return (_c = visible === null || visible === void 0 ? void 0 : visible.table) === null || _c === void 0 ? void 0 : _c.list; } return visible === null || visible === void 0 ? void 0 : visible.default; } return ((_d = visible === null || visible === void 0 ? void 0 : visible.table) === null || _d === void 0 ? void 0 : _d.list) !== false; }) .map((e) => { return createTableColumField(e, apis, manifest); }) .filter((e) => { // rollout if (!e.rollout) { return true; } return doreamonJSSDK.rollout.isRollout(e.rollout); }) .filter((e) => { var _a, _b, _c, _d, _e, _f; if (!((_c = (_b = (_a = manifest.configs) === null || _a === void 0 ? void 0 : _a.columns) === null || _b === void 0 ? void 0 : _b.disable) === null || _c === void 0 ? void 0 : _c.length)) { return e; } return !((_f = (_e = (_d = manifest.configs) === null || _d === void 0 ? void 0 : _d.columns) === null || _e === void 0 ? void 0 : _e.disable) === null || _f === void 0 ? void 0 : _f.includes(e.key)); }); } function createCustomActions(manifest, apis) { var _a; if (!((_a = manifest.actions) === null || _a === void 0 ? void 0 : _a.length)) return []; return (manifest.actions // 不能去掉,否则调用的时候 action 找不到,因为 model 没有了 // .filter(({ visible, hidden }) => { // if (visible === false) return false; // if (hidden === true) return false; // return true; // }) .map((action, index) => { var _a, _b; const sort = (_a = action.sort) !== null && _a !== void 0 ? _a : 7 + index; const pipeline = (_b = action.pipeline) === null || _b === void 0 ? void 0 : _b.map((step) =