UNPKG

@lcap/nasl

Version:

NetEase Application Specific Language

527 lines (524 loc) 27.2 kB
"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.genCreateBlock = exports.genH5CreateFormTemplate = exports.genCreateFormTemplate = void 0; const config_1 = require("../config"); const utils = __importStar(require("../utils")); const concepts_1 = require("../concepts"); const utils_1 = require("./utils"); const _1 = require("."); function genCreateFormTemplate(entity, nameGroup, selectNameGroupMap, appointKeys = [], process, extra = {}, frontendType) { const dataSource = entity.parentNode; let properties = entity.properties.filter((0, _1.filterProperty)('inForm')); const { disabledRelationEntity } = extra; if (appointKeys.length) { properties = entity.properties.filter((property) => appointKeys.includes(property.name)); } return `<u-form ref="${nameGroup.viewElementMainView}"> ${properties .map((property) => { const vModel = `${nameGroup.viewVariableEntity}.${property.name}`; 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 ${required ? ' :required="true"' : ''}${rulesStr.length ? ` rules="${rulesStr}"` : ''} layout="center"><template #label><u-text text="${label}"></u-text></template>\n`; const { typeAnnotation } = property || {}; const { typeNamespace: propertyTypeNamespace } = typeAnnotation || {}; const propertyTypeName = (0, utils_1.transEntityMetadataTypes)(typeAnnotation, dataSource.app); const propertyTypeMaxLength = Number(property.rules .find((item) => item.indexOf('max') > -1) ?.split('(')[1] .slice(0, -1)) || 0; if (property.relationEntity && !disabledRelationEntity) { // 有外键关联 const relationEntity = dataSource?.findEntityByName(property.relationEntity); if (relationEntity) { const relationProperty = relationEntity.properties.find((prop) => prop.name === property.relationProperty); const displayedProperty = (0, _1.getFirstDisplayedProperty)(relationEntity); if (displayedProperty) { const lowerEntityName = utils.firstLowerCase(relationEntity.name); // 存在多个属性关联同一个实体的情况,因此加上属性名用以唯一标识 const key = [property.name, relationEntity.name].join('-'); const selectNameGroup = selectNameGroupMap.get(key); let dataSourceValue = `(params) => ${selectNameGroup.logic}(elements.$ce.page, elements.$ce.size)`; formItem += ` <u-select :clearable="true" placeholder="请选择${label}" :dataSource="${dataSourceValue}" :pageSize="50" textField="${lowerEntityName}.${displayedProperty.name}" valueField="${lowerEntityName}.${relationProperty.name}" :pagination="true" v-model="${vModel}" :emptyValueIsNull="true"> </u-select> `; } else return ''; } else return ''; } else if (propertyTypeName === 'Boolean') { formItem += ` <u-select :clearable="true" v-model="${vModel}" placeholder="请选择${label}" :emptyValueIsNull="true"> <u-select-item :value="true" text="是">是</u-select-item> <u-select-item :value="false" text="否">否</u-select-item> </u-select> `; } else if (propertyTypeName === 'Integer' || propertyTypeName === 'Long') { formItem += `<u-number-input v-model="${vModel}" placeholder="请输入${label}"></u-number-input>`; } else if (propertyTypeName === 'Double') { formItem += `<u-number-input v-model="${vModel}" :precision="0" :step="0" placeholder="请输入${label}"></u-number-input>`; } else if (propertyTypeName === 'Decimal') { formItem += `<u-number-input v-model="${vModel}" :precision="0" :step="0" placeholder="请输入${label}"></u-number-input>`; } else if (propertyTypeName === 'String' && propertyTypeMaxLength > 256) { formItem += `<u-textarea v-model="${vModel}" placeholder="请输入${label}" :emptyValueIsNull="true"></u-textarea>`; } else if (propertyTypeName === 'Date') { formItem += `<u-date-picker :clearable="true" :value.sync="${vModel}" placeholder="请选择${label}" :emptyValueIsNull="true"></u-date-picker>`; } else if (propertyTypeName === 'Time') { formItem += `<u-time-picker :value.sync="${vModel}" placeholder="请选择${label}" :emptyValueIsNull="true"></u-time-picker>`; } else if (propertyTypeName === 'DateTime') { formItem += `<u-date-time-picker :clearable="true" :value.sync="${vModel}" placeholder="请选择${label}" :emptyValueIsNull="true"></u-date-time-picker>`; } else { const namespaceArr = propertyTypeNamespace?.split?.('.') || []; const type = namespaceArr.pop(); if (type === 'enums') { const Enum = dataSource.app.findNodeByCompleteName(`${propertyTypeNamespace}.${propertyTypeName}`); formItem += (0, _1.genEnumSelectBlock)(Enum, `${vModel}`, '', `请选择${label}`, true, frontendType); } else { formItem += `<u-input v-model="${vModel}" placeholder="请输入${label}" :emptyValueIsNull="true"></u-input>`; } } formItem += ` </u-form-item>\n`; return formItem; }) .join('')} ${appointKeys.length ? '' : `<u-form-item layout="center"> <u-button color="primary" @click="${nameGroup.viewLogicSubmit}()">立即创建</u-button> </u-form-item>`} </u-form>`; } exports.genCreateFormTemplate = genCreateFormTemplate; function genH5CreateFormTemplate(entity, nameGroup, selectNameGroupMap, appointKeys = [], process, extra = {}, frontendType) { const dataSource = entity.parentNode; let properties = entity.properties.filter((0, _1.filterProperty)('inForm')); const { disabledRelationEntity } = extra; if (appointKeys.length) { properties = entity.properties.filter((property) => appointKeys.includes(property.name)); } return `<van-form ref="${nameGroup.viewElementMainView}"> ${properties .map((property) => { const vModel = `${nameGroup.viewVariableEntity}.${property.name}`; 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 = ` <van-field drole="other" ${required ? ' :required="true"' : ''}${rulesStr.length ? ` rules="${rulesStr}"` : ''}>\n`; const { typeAnnotation } = property || {}; const { typeNamespace: propertyTypeNamespace } = typeAnnotation || {}; const propertyTypeName = (0, utils_1.transEntityMetadataTypes)(typeAnnotation, dataSource.app); if (property.relationEntity && !disabledRelationEntity) { // 有外键关联 const relationEntity = dataSource?.findEntityByName(property.relationEntity); if (relationEntity) { const relationProperty = relationEntity.properties.find((prop) => prop.name === property.relationProperty); const displayedProperty = (0, _1.getFirstDisplayedProperty)(relationEntity); if (displayedProperty) { const lowerEntityName = utils.firstLowerCase(relationEntity.name); // 存在多个属性关联同一个实体的情况,因此加上属性名用以唯一标识 const key = [property.name, relationEntity.name].join('-'); const selectNameGroup = selectNameGroupMap.get(key); let dataSourceValue = `(params) => ${selectNameGroup.logic}(elements.$ce.page, elements.$ce.size)`; formItem += ` <template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-pickerson type="list" :showToolbar="true" title="请选择${label}" placeholder="请选择${label}" v-model="${vModel}" :dataSource="${dataSourceValue}" :pageSize="50" textField="${lowerEntityName}.${displayedProperty.name}" valueField="${lowerEntityName}.${relationProperty.name}" :notitleblock="true" :pageable="true" inputAlign="left" :remotePaging="true"> <template #pannel-title> <van-text text="请选择${label}"></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></van-linear-layout></template> `; } else return ''; } else return ''; } else if (propertyTypeName === 'Boolean') { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-switch v-model="${vModel}"></van-switch></van-linear-layout></template>`; } else if (propertyTypeName === 'Integer' || propertyTypeName === 'Long') { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-stepper-new v-model="${vModel}" placeholder="请输入${property.label || property.name}" :showPlus="false" :showMinus="false" align="left"></van-stepper-new></van-linear-layout></template>`; } else if (propertyTypeName === 'Double') { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-stepper-new v-model="${vModel}" placeholder="请输入${property.label || property.name}" :showPlus="false" :showMinus="false" align="left"></van-stepper-new></van-linear-layout></template>`; } else if (propertyTypeName === 'Decimal') { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-stepper-new v-model="${vModel}" placeholder="请输入${property.label || property.name}" :showPlus="false" :showMinus="false" align="left"></van-stepper-new></van-linear-layout></template>`; } else if (propertyTypeName === 'Text') { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-fieldtextarea v-model="${vModel}" placeholder="请输入${property.label || property.name}"></van-fieldtextarea></van-linear-layout></template>`; } else if (propertyTypeName === 'Date') { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-calendar :value.sync="${vModel}" title="选择日期" inputAlign="left"></van-calendar></van-linear-layout></template>`; } else if (propertyTypeName === 'Time') { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-datetime-picker type="time" v-model="${vModel}" title="请选择${property.label || property.name}" labelField="" inputAlign="left"></van-datetime-picker></van-linear-layout></template>`; } else if (propertyTypeName === 'DateTime') { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-datetime-picker type="datetime" v-model="${vModel}" title="请选择${property.label || property.name}" labelField="" inputAlign="left"></van-datetime-picker></van-linear-layout></template>`; } else { const namespaceArr = propertyTypeNamespace?.split?.('.') || []; const type = namespaceArr.pop(); if (type === 'enums') { const Enum = dataSource.app.findNodeByCompleteName(`${propertyTypeNamespace}.${propertyTypeName}`); formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal">${(0, _1.genEnumSelectBlock)(Enum, `${vModel}`, '', `请选择${label}`, true, frontendType)}</van-linear-layout></template>`; } else { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-fieldinput v-model="${vModel}" placeholder="请输入${property.label || property.name}"></van-fieldinput></van-linear-layout></template>`; } } formItem += `<template #title><van-text text="${property.label || property.name}"></van-text></template>`; formItem += ` </van-field>\n`; formItem = formItem.replace(/>\n<van-input|<\/van-input>/g, ''); return formItem; }) .join('')} ${appointKeys.length ? '' : `<div> <van-button round block="blockb" type="info" @click="${nameGroup.viewLogicSubmit}()" text="立即创建"></van-button> </div>`} </van-form>`; } exports.genH5CreateFormTemplate = genH5CreateFormTemplate; function genSubmitLogic(createLogic, nameGroup, extendConsequents = []) { const params = [(0, _1.genLogicParam)(`${createLogic.params[0].name}`, nameGroup.viewVariableEntity)]; return utils_1.NaslNode.Logic({ name: nameGroup.viewLogicSubmit, params: [], returns: [], variables: [], body: [ utils_1.NaslLogicItem.Start, utils_1.NaslLogicItem.IfStatement({ test: utils_1.NaslLogicItem.MemberExpression({ object: (0, _1.genCallComponentLogic)(nameGroup.viewElementMainView, 'validate'), property: utils_1.NaslLogicItem.Identifier({ name: 'valid', }), }), consequent: extendConsequents?.length > 0 ? [ ...extendConsequents, utils_1.NaslLogicItem.CallLogic({ label: '弹出消息', shortcut: true, calleeNamespace: 'nasl.ui', calleeName: 'showMessage', arguments: [ utils_1.NaslLogicItem.Argument({ keyword: 'text', expression: utils_1.NaslLogicItem.StringLiteral({ value: '创建成功!', }), }), ], }), ] : [ utils_1.NaslLogicItem.CallLogic({ calleeNamespace: createLogic.getNamespace(), calleeName: createLogic.name, arguments: params, }), utils_1.NaslLogicItem.CallLogic({ label: '弹出消息', shortcut: true, calleeNamespace: 'nasl.ui', calleeName: 'showMessage', arguments: [ utils_1.NaslLogicItem.Argument({ keyword: 'text', expression: utils_1.NaslLogicItem.StringLiteral({ value: '创建成功!', }), }), ], }), ], alternate: [], }), utils_1.NaslLogicItem.End, ], }); } function genProcessV2LaunchLogic(module, entity, process) { const name = module.getLogicUniqueName(`${process.name}Launch`); const bindTypeAnnotation = process?.bind?.typeAnnotation; const typeAnnotation = bindTypeAnnotation.toJSON(); const properties = bindTypeAnnotation.properties.map((property) => { return { name: property.name, concept: 'Identifier', }; }); return utils_1.NaslNode.Logic({ name, params: [ utils_1.NaslNode.Param({ name: 'param1', typeAnnotation: concepts_1.TypeAnnotation.from({ typeKind: 'reference', typeNamespace: entity.getNamespace(), typeName: entity.name, }), }), ], returns: [ utils_1.NaslNode.Return({ name: 'result', }), ], variables: [ { concept: 'Variable', name: 'variable1', defaultValue: null, }, ], body: [ utils_1.NaslLogicItem.Start, concepts_1.Assignment.from({ left: utils_1.NaslLogicItem.Identifier({ name: 'variable1', }), right: utils_1.NaslLogicItem.CallLogic({ calleeNamespace: `app.dataSources.defaultDS.entities.${entity.name}.logics`, calleeName: 'create', arguments: [ { concept: 'Argument', keyword: 'body', expression: { concept: 'Identifier', name: 'param1', }, }, ], }) }), concepts_1.Assignment.from({ left: utils_1.NaslLogicItem.Identifier({ name: 'result', }), right: utils_1.NaslLogicItem.CallLogic({ calleeNamespace: 'nasl.processV2', calleeName: 'launchProcess', arguments: [ { concept: 'Argument', keyword: 'data', expression: { concept: 'NewComposite', typeAnnotation: typeAnnotation, properties: properties, rights: [ { concept: 'SelectMembers', hideMembers: false, expression: { concept: 'Identifier', name: 'variable1', }, members: [], }, ], assignmentLines: [ { concept: 'AssignmentLine', leftIndex: [0, 0], rightIndex: [0], }, ], }, }, utils_1.NaslLogicItem.Argument({ keyword: 'procDefKey', expression: utils_1.NaslLogicItem.StringLiteral({ value: process.uniqueKey, }), }), ], }), }), utils_1.NaslLogicItem.End, ], }); } /** * 生成创建页区块 * @param resolver 实体创建 Action */ function genCreateBlock(entity, oldNode, appointKeys = [], frontendType, process, extra = {}, useViewVriable = true, localTemplate) { const likeComponent = oldNode?.likeComponent; const dataSource = entity.parentNode; const module = dataSource.app; const { ns } = entity; const { disabledRelationEntity } = extra; const createLogic = ns?.logics?.find((logic) => logic.name === `create`); const nameGroup = {}; nameGroup.viewElementMainView = likeComponent.getViewElementUniqueName('form1'); nameGroup.viewVariableEntity = likeComponent.getVariableUniqueName(utils.firstLowerCase(entity.name)); nameGroup.viewLogicSubmit = likeComponent.getLogicUniqueName('submit'); // 收集所有和本实体关联的实体 const selectNameGroupMap = new Map(); const newLogics = []; const newLoadSelectLogics = []; entity.properties.forEach((property) => { // 有外键关联 if (property.relationEntity && !disabledRelationEntity) { const relationEntity = dataSource?.findEntityByName(property.relationEntity); if (relationEntity) { const displayedProperty = (0, _1.getFirstDisplayedProperty)(relationEntity); if (displayedProperty) { const viewElementSelect = likeComponent.getViewElementUniqueName('select'); const selectNameGroup = (0, _1.genUniqueQueryNameGroup)(module, likeComponent, viewElementSelect, false, relationEntity.name); selectNameGroup.viewElementSelect = viewElementSelect; // 存在多个属性关联同一个实体的情况,因此加上属性名用以唯一标识 const key = [property.name, relationEntity.name].join('-'); selectNameGroupMap.set(key, selectNameGroup); const newLogic = (0, _1.genQueryLogic)([relationEntity], selectNameGroup, false, false, module); newLogics.push(newLogic); newLoadSelectLogics.push((0, _1.genLoadSelectLogic)(relationEntity, selectNameGroup, newLogic)); } } } }); const extendConsequent = []; if (process) { const processV2LunchLogic = genProcessV2LaunchLogic(module, entity, process); newLogics.push(processV2LunchLogic); extendConsequent.push(utils_1.NaslLogicItem.CallLogic({ calleeNamespace: 'app.logics', calleeName: processV2LunchLogic.name, arguments: [ { concept: 'Argument', keyword: 'param1', expression: { concept: 'Identifier', namespace: '', name: nameGroup.viewVariableEntity, }, }, ], })); } let viewBindEvents = ''; let viewLoadLogic; if (localTemplate?.genCreatedHandler) { nameGroup.viewLogicCreated = likeComponent.getLogicUniqueName('load'); viewLoadLogic = localTemplate.genCreatedHandler(entity, nameGroup); viewBindEvents = ` ,"viewBindEvents": [ { "concept": "BindEvent", "arguments": [], "name": "created", "calleeNamespace": "", "calleeName": "${nameGroup.viewLogicCreated}" } ]`; } const submitLogic = (localTemplate?.genSubmitLogicLocal || genSubmitLogic)(createLogic, nameGroup, extendConsequent); const genTemplate = localTemplate?.genCreateTemplate || ((frontendType || config_1.config.scope) === 'h5' ? genH5CreateFormTemplate : genCreateFormTemplate); return `<template> ${genTemplate(entity, nameGroup, selectNameGroupMap, appointKeys, process, extra, frontendType)} </template> <definition> { "viewVariables": [ ${useViewVriable ? `{ "concept": "Variable", "name": "${nameGroup.viewVariableEntity}", "typeAnnotation": ${JSON.stringify(utils_1.NaslTypeAnnotation.Reference({ typeNamespace: entity.getNamespace(), typeName: entity.name, }))}}` : ''} ] ${appointKeys.length ? '' : `,"viewLogics": [ ${JSON.stringify(submitLogic)} ${newLoadSelectLogics.map((logic) => `,${JSON.stringify(logic)}`).join('\n')} ${viewLoadLogic ? ',' + JSON.stringify(viewLoadLogic) : ''} ], "logics": ${JSON.stringify(newLogics)}`} ${viewBindEvents} } </definition> `; } exports.genCreateBlock = genCreateBlock; exports.default = genCreateBlock; //# sourceMappingURL=genCreateBlock.js.map