@lcap/nasl
Version:
NetEase Application Specific Language
439 lines (437 loc) • 17 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genSelectItemAddLogic = exports.genSelectFooterBlock = exports.genSelectBlock = exports.genLoadSelectLogic = exports.genH5SelectTemplate = exports.genSelectTemplate = void 0;
const config_1 = require("../config");
const utils = __importStar(require("../utils"));
const _1 = require(".");
const utils_1 = require("./utils");
/**
* 生成select的模板(pc)
* @param property
* @param nameGroup
* @returns
*/
function genSelectTemplate(property, nameGroup, newLogic) {
let dataSourceValue = `(params) => ${newLogic.name}(elements.$ce.page, elements.$ce.size)`;
return `<u-select :clearable="true" placeholder="请选择" ref="${nameGroup.viewElementMainView}" :dataSource="${dataSourceValue}" :pageSize="50" dataSchema="${nameGroup.structure}"
textField="${nameGroup.lowerEntity}.${property.name}"
valueField="${nameGroup.lowerEntity}.id" :pagination="true" :emptyValueIsNull="true">
</u-select>`;
}
exports.genSelectTemplate = genSelectTemplate;
/**
* 生成select的模板(h5)
* @param property
* @param nameGroup
* @returns
*/
function genH5SelectTemplate(property, nameGroup, newLogic) {
const label = property.label || property.name;
let dataSourceValue = `(params) => ${newLogic.name}(elements.$ce.page, elements.$ce.size)`;
return `<van-pickerson type="list" :showToolbar="true" title="请选择" placeholder="请选择" ref="${nameGroup.viewElementMainView}" :dataSource="${dataSourceValue}" :pageSize="50" dataSchema="${nameGroup.structure}"
textField="${nameGroup.lowerEntity}.${property.name}"
valueField="${nameGroup.lowerEntity}.id" :pageable="true" :remotePaging="true">
<template #title>${label}</template>
<template #pannel-title>
<van-text text="请选择"></van-text>
</template>
<template #picker-top>
<van-picker-action-slot targetMethod="cancel">
<van-iconv name="left-arrow" icotype="only"></van-iconv>
</van-picker-action-slot>
<van-picker-action-slot targetMethod="confirm"></van-picker-action-slot>
</template>
<template #picker-bottom>
<van-picker-action-slot targetMethod="cancel">
<van-button
type="info_secondary"
size="normal"
text="取消"
squareroud="round"
></van-button>
</van-picker-action-slot>
<van-picker-action-slot targetMethod="confirm">
<van-button
type="info"
size="normal"
text="确认"
squareroud="round"
></van-button>
</van-picker-action-slot>
</template>
</van-pickerson>`;
}
exports.genH5SelectTemplate = genH5SelectTemplate;
function genLoadSelectLogic(entity, nameGroup, newLogic) {
const moduleNode = entity.app;
const moduleNamespace = moduleNode?.getNamespace();
const params = [(0, _1.genLogicParam)(`${newLogic.params[0].name}`, 'params.page'), (0, _1.genLogicParam)(`${newLogic.params[1].name}`, 'params.size')];
return utils_1.NaslNode.Logic({
name: nameGroup.viewLogicLoad,
params: [
utils_1.NaslNode.Param({
name: 'params',
typeAnnotation: utils_1.NaslUITypeAnnotation.DataSourceParams,
}),
],
returns: [
utils_1.NaslNode.Return({
name: 'result',
typeAnnotation: null,
}),
],
variables: [],
body: [
utils_1.NaslLogicItem.Start,
utils_1.NaslLogicItem.Assignment({
left: utils_1.NaslLogicItem.Identifier({
name: 'result',
}),
right: utils_1.NaslLogicItem.CallLogic({
calleeNamespace: `${moduleNamespace}.logics`,
calleeName: newLogic.name,
arguments: params,
}),
}),
utils_1.NaslLogicItem.End,
],
});
}
exports.genLoadSelectLogic = genLoadSelectLogic;
/**
* 生成实体选择框区块
* @param entity
* @param view
* @returns
*/
function genSelectBlock(entity, oldNode, genSelectTemplateLocal) {
const likeComponent = oldNode?.likeComponent;
const dataSource = entity.parentNode;
const module = dataSource.app;
const viewElementMainView = likeComponent.getViewElementUniqueName('select');
const nameGroup = (0, _1.genUniqueQueryNameGroup)(module, likeComponent, viewElementMainView, false);
nameGroup.viewElementMainView = viewElementMainView;
nameGroup.lowerEntity = utils.firstLowerCase(entity.name);
const displayedProperty = (0, _1.getFirstDisplayedProperty)(entity);
const allEntities = [entity];
const newLogic = (0, _1.genQueryLogic)(allEntities, nameGroup, false, false, module);
const newLogics = [newLogic];
const templateFunc = genSelectTemplateLocal || (config_1.config.scope === 'h5' ? genH5SelectTemplate : genSelectTemplate);
return `<template>
${templateFunc(displayedProperty, nameGroup, newLogic)}
</template>
<definition>
{
"viewLogics": [
${JSON.stringify(genLoadSelectLogic(entity, nameGroup, newLogic))}
],
"logics": ${JSON.stringify(newLogics)}
}
</definition>
`;
}
exports.genSelectBlock = genSelectBlock;
exports.default = genSelectBlock;
function genSelectFooterBlock(entity, nameGroup, view, property) {
return `<template>
<template #renderFooter>
<u-grid-layout>
<u-grid-layout-row :repeat="24" alignment="center" gap="small">
<u-grid-layout-column :span="16">
<u-input placeholder="请输入" :value.sync="${nameGroup.itemInputVariableName}"></u-input>
</u-grid-layout-column>
<u-grid-layout-column :span="8">
<u-button style=" --custom-start: auto; min-width:auto;width:100%;padding:0;" color="default" icon="add" text="添 加" @click="${nameGroup.viewLogicAddItem}"></u-button>
</u-grid-layout-column>
</u-grid-layout-row>
</u-grid-layout>
</template>
<definition>
{
"viewVariables": [
{
"concept": "Variable",
"name": "${nameGroup.itemInputVariableName}",
"typeAnnotation": ${JSON.stringify({
concept: 'TypeAnnotation',
typeKind: 'primitive',
typeName: 'String',
typeNamespace: 'nasl.core',
})}
}
],
"viewLogics": [
${JSON.stringify(genSelectItemAddLogic(entity, nameGroup, view, property))}
]
}
</definition>
`;
}
exports.genSelectFooterBlock = genSelectFooterBlock;
function genSelectItemAddLogic(entity, nameGroup, view, property) {
const callLogics = [];
const variableName = view.getVariableUniqueName('itemVariable1');
const itemVariable1Snapshot = view.getVariableUniqueName(`${variableName}Snapshot`);
let variables = [
utils_1.NaslNode.Variable({
name: variableName,
typeAnnotation: null,
}),
];
const itemExpression = (0, _1.genLogicParam)(`item`, `${variableName}.${nameGroup.textField}`);
if (entity && entity.getNamespace) { // item是实体或者数据结构
callLogics.push(utils_1.NaslLogicItem.Assignment({
left: utils_1.NaslLogicItem.Identifier({
name: variableName,
}),
right: {
concept: 'CallFunction',
name: '',
label: '调用逻辑',
kind: 'Expression',
calleeNamespace: 'nasl.util',
calleeName: 'New',
arguments: [],
typeArguments: [
{
concept: 'TypeAnnotation',
typeKind: 'reference',
typeNamespace: entity.getNamespace(),
typeName: entity.name,
},
],
},
}));
}
else {
// item是基本类型
if (nameGroup.schemaRef === 'nasl.core.String') {
callLogics.push(utils_1.NaslLogicItem.Assignment({
left: utils_1.NaslLogicItem.Identifier({
name: variableName,
}),
right: utils_1.NaslLogicItem.Identifier({
name: nameGroup.itemInputVariableName,
}),
}));
}
else if (['nasl.core.Long', 'nasl.core.Decimal'].includes(nameGroup.schemaRef)) {
const typeAnnotation = {
concept: 'TypeAnnotation',
typeKind: 'primitive',
typeNamespace: 'nasl.core',
typeName: nameGroup.schemaRef.replace('nasl.core.', ''),
};
callLogics.push(utils_1.NaslLogicItem.Assignment({
left: utils_1.NaslLogicItem.Identifier({
name: variableName,
}),
right: utils_1.NaslLogicItem.CallFunction({
calleeName: 'Convert',
typeArguments: [typeAnnotation],
arguments: [
utils_1.NaslLogicItem.Argument({
keyword: 'value',
expression: utils_1.NaslLogicItem.Identifier({
name: nameGroup.itemInputVariableName,
}),
}),
],
}),
}));
}
else {
callLogics.push({
concept: 'Comment',
name: '',
value: '存储输入框的值做处理',
});
}
}
// 该属性可能不是String类型,如Integer,需要转换下,否则问题处会报错
if (property && property.typeAnnotation && property.typeAnnotation.typeNamespace === 'nasl.core' && property.typeAnnotation.typeName !== 'String') {
callLogics.push(utils_1.NaslLogicItem.Assignment({
left: itemExpression.expression,
right: utils_1.NaslLogicItem.CallFunction({
calleeName: 'Convert',
typeArguments: [property.typeAnnotation],
arguments: [
utils_1.NaslLogicItem.Argument({
keyword: 'value',
expression: utils_1.NaslLogicItem.Identifier({
name: nameGroup.itemInputVariableName,
}),
}),
],
}),
}));
}
else if (entity && entity.getNamespace) {
callLogics.push(utils_1.NaslLogicItem.Assignment({
left: itemExpression.expression,
right: utils_1.NaslLogicItem.Identifier({
name: nameGroup.itemInputVariableName,
}),
}));
}
callLogics.push(utils_1.NaslLogicItem.Assignment({
left: utils_1.NaslLogicItem.Identifier({
name: nameGroup.itemInputVariableName,
}),
right: utils_1.NaslLogicItem.StringLiteral({
value: '',
}),
}));
if (entity && entity.concept === 'Entity') {
const { ns } = entity;
const createLogic = ns?.logics?.find((logic) => logic.name === `create`);
callLogics.push(utils_1.NaslLogicItem.Assignment({
left: utils_1.NaslLogicItem.Identifier({
name: variableName,
}),
right: utils_1.NaslLogicItem.CallLogic({
calleeNamespace: createLogic.getNamespace(),
calleeName: createLogic.name,
arguments: [(0, _1.genLogicParam)(`${createLogic.params[0].name}`, variableName)],
}),
}));
// 因为插入的是{entity1:{}}这样的类型,而create接口返回的是{},所以需要加一层
if (nameGroup.prefixField) {
callLogics.push(utils_1.NaslLogicItem.Assignment({
left: utils_1.NaslLogicItem.Identifier({
name: itemVariable1Snapshot,
}),
right: {
concept: 'NewComposite',
name: '',
typeAnnotation: {
concept: 'TypeAnnotation',
name: '',
typeKind: 'anonymousStructure',
typeArguments: null,
returnType: null,
properties: [
{
concept: 'StructureProperty',
name: nameGroup.prefixField,
typeAnnotation: utils_1.NaslTypeAnnotation.Reference({
typeNamespace: entity.getNamespace(),
typeName: entity.name,
})
},
],
},
properties: [
{
concept: 'Identifier',
name: nameGroup.prefixField,
},
],
rights: [
{
concept: 'SelectMembers',
hideMembers: false,
expression: {
concept: 'Identifier',
name: variableName,
},
members: [],
},
],
assignmentLines: [
{
concept: 'AssignmentLine',
name: '',
leftIndex: [0, 0],
rightIndex: [0],
},
],
hideProperties: false,
hideDefaults: false,
autoConnection: true,
},
}));
}
variables.push(utils_1.NaslNode.Variable({
name: itemVariable1Snapshot,
typeAnnotation: null,
}));
}
else {
callLogics.push({
concept: 'Comment',
name: '',
value: '调用后端接口保存数据',
});
}
const argumentsTemp = [];
if (variables.length >= 2) { // 实体
argumentsTemp.push(utils_1.NaslLogicItem.Argument({
keyword: 'item',
expression: utils_1.NaslLogicItem.Identifier({
name: itemVariable1Snapshot,
}),
}));
}
else {
if (entity && entity.getNamespace || ['nasl.core.String', 'nasl.core.Long', 'nasl.core.Decimal'].includes(nameGroup.schemaRef)) {
argumentsTemp.push(utils_1.NaslLogicItem.Argument({
keyword: 'item',
expression: utils_1.NaslLogicItem.Identifier({
name: variableName,
}),
}));
}
else {
// 其他类型就置空
variables = [];
callLogics.push({
concept: 'Comment',
name: '',
value: '如下调用addItem将数据添加到下拉框里,item参数填上值',
});
argumentsTemp.push(utils_1.NaslLogicItem.Argument({
keyword: 'item',
expression: null,
}));
}
}
return utils_1.NaslNode.Logic({
name: nameGroup.viewLogicAddItem,
params: [],
variables,
returns: [],
body: [
utils_1.NaslLogicItem.Start,
...callLogics,
(0, _1.genCallComponentLogic)(nameGroup.viewElementMainView, 'addItem', argumentsTemp),
utils_1.NaslLogicItem.End,
],
playground: [],
});
}
exports.genSelectItemAddLogic = genSelectItemAddLogic;
//# sourceMappingURL=genSelectBlock.js.map