UNPKG

@lcap/asl

Version:

NetEase Application Specific Language

648 lines (644 loc) 29.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.genCurdGridViewBlock = exports.genCurdBlock = void 0; const __1 = require("../.."); const _1 = require("."); const builtInFunctions = require("./builtInFunctions.json"); function genFilterTemplate(entity, nameGroup, selectNameGroupMap, tableRefName = 'tableView') { const propertyList = entity.propertyList .filter(_1.filterProperty('inFilter')); return ` <u-linear-layout justify="space-between"> <u-form layout="inline" label-size="auto"> ${propertyList.map((property) => { const label = property.label || property.name; let formItem = `<u-form-item label="${label}">`; if (property.$relationEntity) { // 有外键关联 const relationEntity = __1.vertexsMap.get(property.$relationEntity); if (relationEntity) { const relationProperty = __1.vertexsMap.get(property.$relationProperty); const displayedProperty = _1.getFirstDisplayedProperty(relationEntity, 'inFilter'); if (displayedProperty) { const lowerEntityName = __1.utils.firstLowerCase(relationEntity.name); formItem += ` <u-select clearable placeholder="请选择${label}" :data-source="${selectNameGroupMap.get(relationEntity.id).load}" text-field="${lowerEntityName}.${displayedProperty.name}" value-field="${lowerEntityName}.${relationProperty.name}" pageable remote-paging v-model="filter.${property.name}"> </u-select> `; } else return ''; } else return ''; } else if (property.typeKey === '#/basicTypes/Boolean') { formItem += ` <u-select clearable v-model="filter.${property.name}" placeholder="请输入${label}"> <u-select-item :value="true">是</u-select-item> <u-select-item :value="false">否</u-select-item> </u-select> `; } else if (property.typeKey === '#/basicTypes/Integer' || property.typeKey === '#/basicTypes/Long') { formItem += `<u-number-input v-model="filter.${property.name}" placeholder="请输入${label}"></u-number-input>`; } else if (property.typeKey === '#/basicTypes/Decimal') { formItem += `<u-number-input v-model="filter.${property.name}" :precision="0" :step="0" placeholder="请输入${label}"></u-number-input>`; } else if (property.typeKey === '#/basicTypes/Text') { formItem += `<u-textarea v-model="filter.${property.name}" placeholder="请输入${label}"></u-textarea>`; } else if (property.typeKey === '#/basicTypes/Date') { formItem += `<u-date-picker clearable :date.sync="filter.${property.name}" placeholder="请输入${label}"></u-date-picker>`; } else if (property.typeKey === '#/basicTypes/Time') { formItem += `<u-time-picker :time.sync="filter.${property.name}" placeholder="请输入${label}"></u-time-picker>`; } else if (property.typeKey === '#/basicTypes/DateTime') { formItem += `<u-date-time-picker clearable :date.sync="filter.${property.name}" placeholder="请输入${label}"></u-date-time-picker>`; } else if (__1.dataTypesMap[property.typeKey].type === 'enum') { formItem += _1.genEnumSelectBlock(__1.dataTypesMap[property.typeKey], `filter.${property.name}`, '', `请选择${label}`); } else { formItem += `<u-input v-model="filter.${property.name}" placeholder="请输入${label}"></u-input>`; } formItem += `</u-form-item>`; return formItem; }).join('')} <u-form-item> <u-button color="primary" @click="$refs.${tableRefName}.reload()">查询</u-button> </u-form-item> </u-form> <u-button color="primary" @click="${nameGroup.create}($event)">创建</u-button> </u-linear-layout> `; } function genSaveModalTemplate(entity, nameGroup, selectNameGroupMap) { const propertyList = entity.propertyList .filter(_1.filterProperty('inForm')); const idProperty = entity.propertyList.find((item) => item.primaryKey || item.name === 'id'); return ` <u-modal ref="saveModal"> <template #title> <div v-if="input.${idProperty.name}">修改</div> <div v-if="input.${idProperty.name} == undefined">创建</div> </template> <template #body> <u-form ref="form1"> ${propertyList.map((property) => { const label = property.label || property.name; const required = !!property.required; const rules = []; if (property.rules && property.rules.length) { property.rules.forEach((rule) => rules.push(rule)); } if (required) rules.push('required'); const rulesStr = rules.join(' | '); let formItem = `<u-form-item label="${label}" ${required ? 'required' : ''} ${rulesStr.length ? `rules="${rulesStr}"` : ''} >`; if (property.$relationEntity) { // 有外键关联 const relationEntity = __1.vertexsMap.get(property.$relationEntity); if (relationEntity) { const relationProperty = __1.vertexsMap.get(property.$relationProperty); const displayedProperty = _1.getFirstDisplayedProperty(relationEntity, 'inForm'); if (displayedProperty) { const lowerEntityName = __1.utils.firstLowerCase(relationEntity.name); formItem += ` <u-select clearable placeholder="请选择${label}" :data-source="${selectNameGroupMap.get(relationEntity.id).load}" text-field="${lowerEntityName}.${displayedProperty.name}" value-field="${lowerEntityName}.${relationProperty.name}" pageable remote-paging v-model="input.${property.name}"> </u-select> `; } else return ''; } else return ''; } else if (property.typeKey === '#/basicTypes/Boolean') { formItem += ` <u-select clearable v-model="input.${property.name}" placeholder="请输入${label}"> <u-select-item :value="true">是</u-select-item> <u-select-item :value="false">否</u-select-item> </u-select>`; } else if (property.typeKey === '#/basicTypes/Integer' || property.typeKey === '#/basicTypes/Long') { formItem += `<u-number-input v-model="input.${property.name}" placeholder="请输入${label}"></u-number-input>`; } else if (property.typeKey === '#/basicTypes/Decimal') { formItem += `<u-number-input v-model="input.${property.name}" :precision="0" :step="0" placeholder="请输入${label}"></u-number-input>`; } else if (property.typeKey === '#/basicTypes/Text') { formItem += `<u-textarea v-model="input.${property.name}" placeholder="请输入${label}"></u-textarea>`; } else if (property.typeKey === '#/basicTypes/Date') { formItem += `<u-date-picker clearable :date.sync="input.${property.name}" placeholder="请输入${label}"></u-date-picker>`; } else if (property.typeKey === '#/basicTypes/Time') { formItem += `<u-time-picker :time.sync="input.${property.name}" placeholder="请输入${label}"></u-time-picker>`; } else if (property.typeKey === '#/basicTypes/DateTime') { formItem += `<u-date-time-picker clearable :date.sync="input.${property.name}" placeholder="请输入${label}"></u-date-time-picker>`; } else if (__1.dataTypesMap[property.typeKey].type === 'enum') { formItem += _1.genEnumSelectBlock(__1.dataTypesMap[property.typeKey], `input.${property.name}`, '', `请选择${label}`); } else { formItem += `<u-input v-model="input.${property.name}" placeholder="请输入${label}"></u-input>`; } formItem += `</u-form-item>\n`; return formItem; }).join('')} </u-form> </template> <template #foot> <u-linear-layout> <u-button color="primary" @click="${nameGroup.submit}()" v-if="input.${idProperty.name}">提交修改</u-button> <u-button color="primary" @click="${nameGroup.submit}()" v-if="input.${idProperty.name} == undefined">立即创建</u-button> </u-linear-layout> </template> </u-modal>`; } function genInitLogic(entity, nameGroup) { return { level: 'logic', expanded: false, name: nameGroup.init, description: '', params: [], returns: [], variables: [], body: [ { level: 'logicNode', type: 'Start', label: '开始', }, { level: 'expressionNode', type: 'BuiltInFunction', label: '内置函数', calleeCode: 'Clear', builtInFuncParams: [ { parentAttr: 'builtInFuncParams', level: 'expressionNode', type: 'BuiltInFuncParam', name: 'obj', builtInFuncParamValue: { parentAttr: 'builtInFuncParamValue', level: 'expressionNode', type: 'Identifier', name: 'filter', }, schema: builtInFunctions.Clear.params[0].schema, }, ], }, { level: 'logicNode', type: 'End', label: '结束', }, ], playground: [], }; } function genCreateLogic(entity, nameGroup) { return { level: 'logic', expanded: false, name: nameGroup.create, description: '', params: [], returns: [], variables: [], body: [ { level: 'logicNode', type: 'Start', label: '开始', }, { level: 'logicNode', type: 'AssignmentExpression', label: '赋值', operator: '=', left: { level: 'expressionNode', type: 'Identifier', name: 'input', }, right: { parentAttr: 'right', level: 'expressionNode', type: 'BuiltInFunction', label: '内置函数', calleeCode: 'Clone', builtInFuncParams: [ { parentAttr: 'builtInFuncParams', level: 'expressionNode', type: 'BuiltInFuncParam', name: 'obj', builtInFuncParamValue: { parentAttr: 'builtInFuncParamValue', level: 'expressionNode', type: 'Identifier', name: nameGroup.lowerEntity, }, schema: builtInFunctions.Clone.params[0].schema, }, ], }, }, _1.genCallComponentLogic('saveModal', 'open'), { level: 'logicNode', type: 'End', label: '结束', }, ], playground: [], }; } function genModifyLogic(entity, nameGroup) { const scopeStructureSchema = _1.genGenericTypeSchema('ScopeOf', { T: { $ref: nameGroup.structure } }); return { level: 'logic', expanded: false, name: nameGroup.modify, description: '', params: [ { level: 'param', type: 'Identifier', name: 'event', schema: { type: 'string', format: '', }, }, { level: 'param', name: 'scope', schema: scopeStructureSchema, }, ], returns: [], variables: [], body: [ { level: 'logicNode', type: 'Start', label: '开始', }, { level: 'logicNode', type: 'AssignmentExpression', label: '赋值', operator: '=', left: { level: 'expressionNode', type: 'Identifier', name: 'input', }, right: { parentAttr: 'right', level: 'expressionNode', type: 'BuiltInFunction', label: '内置函数', calleeCode: 'Clone', builtInFuncParams: [ { parentAttr: 'builtInFuncParams', level: 'expressionNode', type: 'BuiltInFuncParam', name: 'obj', builtInFuncParamValue: { parentAttr: 'builtInFuncParamValue', level: 'expressionNode', type: 'MemberExpression', object: { parentAttr: 'builtInFuncParamValue', level: 'expressionNode', type: 'MemberExpression', object: { parentAttr: 'object', level: 'expressionNode', type: 'Identifier', name: 'scope', }, property: { parentAttr: 'property', level: 'expressionNode', type: 'Identifier', name: 'item', schemaRef: '', code: 'item', }, code: '', }, property: { parentAttr: 'property', level: 'expressionNode', type: 'Identifier', name: nameGroup.lowerEntity, schemaRef: `${nameGroup.structure}.${nameGroup.lowerEntity}`, code: '', }, code: '', }, schema: builtInFunctions.Clone.params[0].schema, }, ], }, }, _1.genCallComponentLogic('saveModal', 'open'), { level: 'logicNode', type: 'End', label: '结束', }, ], playground: [], }; } function genSubmitLogic(entity, nameGroup, tableRefName = 'tableView') { const updateResolver = entity.resolvers.find((item) => item.name === 'update'); const createResolver = entity.resolvers.find((item) => item.name === 'create'); const updateBodyParam = _1.getParamFromResolver(updateResolver, 'body'); const createBodyparam = _1.getParamFromResolver(createResolver, 'body'); const idProperty = entity.propertyList.find((item) => item.primaryKey || item.name === 'id'); return { level: 'logic', name: nameGroup.submit, params: [], returns: [], variables: [ _1.genVariable('validateResult', '#/systemTypes/ValidateEvent', __1.LEVEL_ENUM.variable), ], body: [ { level: 'logicNode', type: 'Start', label: '开始', }, { level: 'logicNode', type: 'AssignmentExpression', label: '赋值', operator: '=', left: { level: 'expressionNode', type: 'Identifier', name: 'validateResult', }, right: _1.genCallComponentLogic('form1', 'validate'), }, { level: 'logicNode', type: 'IfStatement', label: '条件分支', test: { level: 'expressionNode', type: 'MemberExpression', name: 'name', hide: false, object: { level: 'expressionNode', type: 'Identifier', name: 'validateResult', }, property: { level: 'expressionNode', type: 'Identifier', name: 'valid', }, }, consequent: [ { level: 'logicNode', type: 'IfStatement', label: '条件分支', test: { parentAttr: 'test', level: 'expressionNode', type: 'BuiltInFunction', label: '内置函数', calleeCode: 'Convert', builtInFuncParams: [ { parentAttr: 'builtInFuncParams', level: 'expressionNode', type: 'BuiltInFuncParam', name: 'value', builtInFuncParamValue: { level: 'expressionNode', type: 'MemberExpression', object: { level: 'expressionNode', type: 'Identifier', name: 'input', }, property: { level: 'expressionNode', type: 'Identifier', name: 'id', schemaRef: idProperty.id, code: `ID_${idProperty.id}`, }, }, schema: builtInFunctions.Convert.params[0].schema, }, { parentAttr: 'builtInFuncParams', level: 'expressionNode', type: 'BuiltInFuncParam', name: 'convertTo', builtInFuncParamValue: { level: 'expressionNode', type: 'TypeNote', schema: { format: '', type: 'boolean', }, }, schema: builtInFunctions.Convert.params[1].schema, }, ], }, consequent: [ _1.genCallInterface(updateResolver.interface, [ _1.genInterParam(updateBodyParam.id, 'input'), ]), ], alternate: [ _1.genCallInterface(createResolver.interface, [ _1.genInterParam(createBodyparam.id, 'input'), ]), ], }, _1.genCallComponentLogic('saveModal', 'close'), _1.genCallComponentLogic(tableRefName, 'reload'), ], alternate: [], }, { level: 'logicNode', type: 'End', label: '结束', }, ], playground: [], expanded: false, }; } function genCurdBlock(entity, view) { const existingNameSets = { viewLogic: new Set(view.$def.logics.map((logic) => logic.name)), interface: new Set(entity.dataNode.service.interfaces.map((itface) => itface.name)), structure: new Set(entity.dataNode.structures.map((structure) => structure.name)), }; const nameGroup = _1.genUniqueQueryNameGroup(existingNameSets, view.name, 'tableView'); nameGroup.remove = __1.utils.unique('remove', existingNameSets.viewLogic); nameGroup.init = __1.utils.unique('init', existingNameSets.viewLogic); nameGroup.create = __1.utils.unique('create', existingNameSets.viewLogic); nameGroup.modify = __1.utils.unique('modify', existingNameSets.viewLogic); nameGroup.submit = __1.utils.unique('submit', existingNameSets.viewLogic); nameGroup.lowerEntity = __1.utils.firstLowerCase(entity.name); // 收集所有和本实体关联的实体 const allEntities = [entity]; entity.propertyList.forEach((property) => { if (property.$relationEntity) { // 有外键关联 const relationEntity = __1.vertexsMap.get(property.$relationEntity); if (relationEntity) { const displayedProperty = _1.getFirstDisplayedProperty(relationEntity); if (displayedProperty && !allEntities.includes(relationEntity)) allEntities.push(relationEntity); } } }); const newStructures = [_1.genQueryStructure(allEntities, nameGroup)]; const newInterfaces = [_1.genQueryInterface(allEntities, nameGroup, true, true)]; const selectNameGroupMap = new Map(); const loadSelectLogics = allEntities.slice(1).map((entity) => { const selectNameGroup = _1.genUniqueQueryNameGroup(existingNameSets, view.name, 'select', false, entity.name); selectNameGroupMap.set(entity.id, selectNameGroup); return _1.genLoadSelectLogic(entity, selectNameGroup, newStructures, newInterfaces); }); return `<template> <u-linear-layout direction="vertical"> ${genFilterTemplate(entity, nameGroup, selectNameGroupMap)} ${_1.genTableTemplate(entity, nameGroup)} ${genSaveModalTemplate(entity, nameGroup, selectNameGroupMap)} </u-linear-layout> </template> <definition> { "variables": [ ${JSON.stringify(_1.genVariable(__1.utils.firstLowerCase(entity.name), entity.schemaRef, __1.LEVEL_ENUM.variable))}, ${JSON.stringify(_1.genVariable('input', entity.schemaRef, __1.LEVEL_ENUM.variable))}, ${JSON.stringify(_1.genVariable('filter', entity.schemaRef, __1.LEVEL_ENUM.variable))} ], "lifecycles": [ { "level": "lifecycle", "name": "created", "logicId": "${nameGroup.init}" } ], "logics": [ ${JSON.stringify(_1.genTableLoadLogic(entity, nameGroup, newStructures, newInterfaces, true))}, ${JSON.stringify(_1.genTableRemoveLogic(entity, nameGroup))}, ${JSON.stringify(genInitLogic(entity, nameGroup))}, ${JSON.stringify(genCreateLogic(entity, nameGroup))}, ${JSON.stringify(genModifyLogic(entity, nameGroup))}, ${JSON.stringify(genSubmitLogic(entity, nameGroup))} ${loadSelectLogics.map((logic) => ',' + JSON.stringify(logic)).join('\n')} ], "interfaces": ${JSON.stringify(newInterfaces)}, "structures": ${JSON.stringify(newStructures)} } </definition> `; } exports.genCurdBlock = genCurdBlock; function genCurdGridViewBlock(entity, view) { const existingNameSets = { viewLogic: new Set(view.$def.logics.map((logic) => logic.name)), interface: new Set(entity.dataNode.service.interfaces.map((itface) => itface.name)), structure: new Set(entity.dataNode.structures.map((structure) => structure.name)), }; const nameGroup = _1.genUniqueQueryNameGroup(existingNameSets, view.name, 'gridView'); nameGroup.remove = __1.utils.unique('remove', existingNameSets.viewLogic); nameGroup.init = __1.utils.unique('init', existingNameSets.viewLogic); nameGroup.create = __1.utils.unique('create', existingNameSets.viewLogic); nameGroup.modify = __1.utils.unique('modify', existingNameSets.viewLogic); nameGroup.submit = __1.utils.unique('submit', existingNameSets.viewLogic); nameGroup.lowerEntity = __1.utils.firstLowerCase(entity.name); // 收集所有和本实体关联的实体 const allEntities = [entity]; entity.propertyList.forEach((property) => { if (property.$relationEntity) { // 有外键关联 const relationEntity = __1.vertexsMap.get(property.$relationEntity); if (relationEntity) { const displayedProperty = _1.getFirstDisplayedProperty(relationEntity); if (displayedProperty && !allEntities.includes(relationEntity)) allEntities.push(relationEntity); } } }); const newStructures = [_1.genQueryStructure(allEntities, nameGroup)]; const newInterfaces = [_1.genQueryInterface(allEntities, nameGroup, true, true)]; const selectNameGroupMap = new Map(); const loadSelectLogics = allEntities.slice(1).map((entity) => { const selectNameGroup = _1.genUniqueQueryNameGroup(existingNameSets, view.name, 'select', false, entity.name); selectNameGroupMap.set(entity.id, selectNameGroup); return _1.genLoadSelectLogic(entity, selectNameGroup, newStructures, newInterfaces); }); return `<template> <u-linear-layout direction="vertical"> ${genFilterTemplate(entity, nameGroup, selectNameGroupMap, 'gridView')} ${_1.genGridViewTemplate(entity, nameGroup)} ${genSaveModalTemplate(entity, nameGroup, selectNameGroupMap)} </u-linear-layout> </template> <definition> { "variables": [ ${JSON.stringify(_1.genVariable(__1.utils.firstLowerCase(entity.name), entity.schemaRef, __1.LEVEL_ENUM.variable))}, ${JSON.stringify(_1.genVariable('input', entity.schemaRef, __1.LEVEL_ENUM.variable))}, ${JSON.stringify(_1.genVariable('filter', entity.schemaRef, __1.LEVEL_ENUM.variable))} ], "lifecycles": [ { "level": "lifecycle", "name": "created", "logicId": "${nameGroup.init}" } ], "logics": [ ${JSON.stringify(_1.genGridViewLoadLogic(entity, nameGroup, newStructures, newInterfaces, true))}, ${JSON.stringify(_1.genGridViewRemoveLogic(entity, nameGroup))}, ${JSON.stringify(genInitLogic(entity, nameGroup))}, ${JSON.stringify(genCreateLogic(entity, nameGroup))}, ${JSON.stringify(genModifyLogic(entity, nameGroup))}, ${JSON.stringify(genSubmitLogic(entity, nameGroup, 'gridView'))} ${loadSelectLogics.map((logic) => ',' + JSON.stringify(logic)).join('\n')} ], "interfaces": ${JSON.stringify(newInterfaces)}, "structures": ${JSON.stringify(newStructures)} } </definition> `; } exports.genCurdGridViewBlock = genCurdGridViewBlock; exports.default = genCurdBlock; //# sourceMappingURL=genCurdBlock.js.map