@lcap/asl
Version:
NetEase Application Specific Language
255 lines (253 loc) • 10.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.genTableColumnBlock = exports.genTableBlock = exports.genTableRemoveLogic = exports.genTableLoadLogic = exports.genTableTemplate = exports.genTableColumnTemplate = void 0;
const __1 = require("../..");
const _1 = require(".");
/**
* 根据实体属性生成表格列模板
* @param property 实体属性
*/
function genTableColumnTemplate(property) {
const lowerEntityName = __1.utils.firstLowerCase(property.root.name);
let expression = `scope.item.${lowerEntityName}.${property.name}`;
const title = property.label || property.name;
if (__1.dataTypesMap[property.typeKey].type === 'enum')
expression = _1.getExpression(property, `scope.item.${lowerEntityName}.${property.name}`);
else if (property.$relationEntity) { // 有外键关联
const relationEntity = __1.vertexsMap.get(property.$relationEntity);
if (relationEntity) {
const displayedProperty = _1.getFirstDisplayedProperty(relationEntity);
if (displayedProperty) {
expression = _1.getExpression(property, `scope.item.${__1.utils.firstLowerCase(relationEntity.name)}.${displayedProperty.name}`);
}
else
return '';
}
else
return '';
}
else
expression = _1.getExpression(property, `scope.item.${lowerEntityName}.${property.name}`);
return `<u-table-view-column title="${title}">
<template #cell="scope">
<u-linear-layout gap="small">
<u-text :text="${expression}"></u-text>
</u-linear-layout>
</template>
</u-table-view-column>`;
}
exports.genTableColumnTemplate = genTableColumnTemplate;
/**
* 生成表格模板
* @param entity 实体
* @param nameGroup 命名组
*/
function genTableTemplate(entity, nameGroup) {
const propertyList = entity.propertyList
.filter(_1.filterProperty('inTable'));
return `<u-table-view ref="tableView" :data-source="${nameGroup.load}" data-schema="${nameGroup.structure}"
value-field="${nameGroup.lowerEntity}.id"
pageable remote-paging>
<u-table-view-column type="index" width="60" title="序号"></u-table-view-column>
${propertyList.map((property) => genTableColumnTemplate(property) + '\n').join('')}
<u-table-view-column title="操作">
<template #cell="scope">
<u-linear-layout gap="small">
<u-link @click="${nameGroup.modify || 'modify'}($event, scope)">修改</u-link>
<u-link @click="${nameGroup.remove}($event, scope)">删除</u-link>
</u-linear-layout>
</template>
</u-table-view-column>
</u-table-view>
`;
}
exports.genTableTemplate = genTableTemplate;
/**
* 生成表格 load 逻辑
* @param entity 实体
*/
function genTableLoadLogic(entity, nameGroup, newStructures, newInterfaces, supportFilter) {
const paramsSchema = {
$ref: '#/systemTypes/DataSourceParams',
};
const resultSchema = _1.genGenericTypeSchema('PageOf', { T: { $ref: nameGroup.structure } });
const params = [
_1.genInterParam(`${newInterfaces[0].name}.${newInterfaces[0].logic.params[0].name}`, 'params.page', [undefined, undefined]),
_1.genInterParam(`${newInterfaces[0].name}.${newInterfaces[0].logic.params[1].name}`, 'params.size', [undefined, undefined]),
_1.genInterParam(`${newInterfaces[0].name}.${newInterfaces[0].logic.params[2].name}`, 'params.sort', [undefined, undefined]),
_1.genInterParam(`${newInterfaces[0].name}.${newInterfaces[0].logic.params[3].name}`, 'params.order', [undefined, undefined]),
];
if (supportFilter)
params.push(_1.genInterParam(`${newInterfaces[0].name}.${newInterfaces[0].logic.params[4].name}`, 'filter', [undefined]));
return {
level: 'logic',
name: nameGroup.load,
params: [{
level: 'param',
type: 'Identifier',
name: 'params',
schema: paramsSchema,
}],
returns: [
_1.genVariable('result', resultSchema, __1.LEVEL_ENUM.return),
],
variables: [],
body: [{
level: 'logicNode',
type: 'Start',
label: '开始',
}, {
level: 'logicNode',
type: 'AssignmentExpression',
label: '赋值',
operator: '=',
left: {
level: 'expressionNode',
type: 'Identifier',
name: 'result',
},
right: _1.genCallInterfaceFromTempInterface(newInterfaces[0], params),
}, {
level: 'logicNode',
type: 'End',
label: '结束',
}],
};
}
exports.genTableLoadLogic = genTableLoadLogic;
/**
* 生成表格 remove 逻辑
* @param entity 实体
*/
function genTableRemoveLogic(entity, nameGroup) {
const scopeStructureSchema = _1.genGenericTypeSchema('ScopeOf', { T: { $ref: nameGroup.structure } });
const deleteResolver = entity.resolvers.find((resolver) => resolver.name === 'delete');
const deleteInterface = deleteResolver.interface;
const idParam = _1.getParamFromResolver(deleteResolver, 'id');
const idProperty = entity.propertyList.find((property) => property.primaryKey || property.name === 'id');
return {
level: 'logic',
name: nameGroup.remove,
params: [{
level: 'param',
type: 'Identifier',
name: 'event',
schema: { $ref: '#/basicTypes/String' },
}, {
level: 'param',
name: 'scope',
schema: scopeStructureSchema,
}],
returns: [],
variables: [],
body: [{
level: 'logicNode',
type: 'Start',
label: '开始',
},
_1.genCallInterface(deleteInterface, [
_1.genInterParam(idParam.id, `scope.item.${nameGroup.lowerEntity}.id`, ['', '', `${nameGroup.structure}.${nameGroup.lowerEntity}`, idProperty.id]),
]),
_1.genCallComponentLogic('tableView', 'reload'),
{
level: 'logicNode',
label: '结束',
type: 'End',
}],
};
}
exports.genTableRemoveLogic = genTableRemoveLogic;
/**
* 生成表格区块
* @param entity 实体
* @param view 所插入的页面,用于生成逻辑名字,去重等
* @notice 目前 logic 名去重做成前置处理了,与 mergeBlock 的后置处理不冲突
* (页面 load 名) -产生-> (interface 名) -产生-> (structure 名)
* load -> load_someView_tableView
* load_select_student -> load_someView_select_student
*/
function genTableBlock(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.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.push(relationEntity);
}
}
});
const newStructures = [_1.genQueryStructure(allEntities, nameGroup)];
const newInterfaces = [_1.genQueryInterface(allEntities, nameGroup, false, true)];
return `
<template>
${genTableTemplate(entity, nameGroup)}
</template>
<definition>
{
"logics": [
${JSON.stringify(genTableLoadLogic(entity, nameGroup, newStructures, newInterfaces, false))},
${JSON.stringify(genTableRemoveLogic(entity, nameGroup))}
],
"interfaces": ${JSON.stringify(newInterfaces)},
"structures": ${JSON.stringify(newStructures)}
}
</definition>
`;
}
exports.genTableBlock = genTableBlock;
function genTableColumnBlock(property, view) {
const entity = property.root;
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.lowerEntity = __1.utils.firstLowerCase(entity.name);
// 收集所有和本实体关联的实体
const allEntities = [entity];
const propertyList = [property];
propertyList.forEach((property) => {
if (property.$relationEntity) { // 有外键关联
const relationEntity = __1.vertexsMap.get(property.$relationEntity);
if (relationEntity) {
const displayedProperty = _1.getFirstDisplayedProperty(relationEntity);
if (displayedProperty)
allEntities.push(relationEntity);
}
}
});
const newStructures = [_1.genQueryStructure(allEntities, nameGroup)];
const newInterfaces = [_1.genQueryInterface(allEntities, nameGroup, false, true)];
return `
<template>
<u-table-view ref="tableView" :data-source="${nameGroup.load}" pageable remote-paging data-schema="${nameGroup.structure}">
${propertyList.map((property) => genTableColumnTemplate(property) + '\n').join('')}
</u-table-view>
</template>
<definition>
{
"logics": [
${JSON.stringify(genTableLoadLogic(entity, nameGroup, newStructures, newInterfaces, false))}
],
"interfaces": ${JSON.stringify(newInterfaces)},
"structures": ${JSON.stringify(newStructures)}
}
</definition>
`;
}
exports.genTableColumnBlock = genTableColumnBlock;
exports.default = genTableBlock;
//# sourceMappingURL=genTableBlock.js.map