UNPKG

@lcap/nasl

Version:

NetEase Application Specific Language

402 lines (400 loc) 21.7 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.genUpdateBlock = exports.genH5UpdateFormTemplate = exports.genUpdateFormTemplate = void 0; const config_1 = require("../config"); const utils = __importStar(require("../utils")); const utils_1 = require("./utils"); const _1 = require("."); function genUpdateFormTemplate(entity, nameGroup, selectNameGroupMap) { const dataSource = entity.parentNode; const properties = entity.properties.filter((0, _1.filterProperty)('inForm')); 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) { // 有外键关联 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); } else { formItem += `<u-input v-model="${vModel}" placeholder="请输入${label}" :emptyValueIsNull="true"></u-input>`; } } formItem += ` </u-form-item>\n`; return formItem; }) .join('')} <u-form-item layout="center"> <u-button color="primary" @click="${nameGroup.viewLogicSubmit}()">提交修改</u-button> </u-form-item> </u-form>`; } exports.genUpdateFormTemplate = genUpdateFormTemplate; function genH5UpdateFormTemplate(entity, nameGroup, selectNameGroupMap) { const dataSource = entity.parentNode; const module = dataSource.app; const properties = entity.properties.filter((0, _1.filterProperty)('inForm')); 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) { // 有外键关联 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" :show-toolbar="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="请输入${label}" :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="请输入${label}" :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="请输入${label}" :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="请输入${label}"></van-fieldtextarea></van-linear-layout></template>`; // @TODO } else if (propertyTypeName === 'Date') { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-calendar :clearable="true" :value.sync="${vModel}" placeholder="请选择${label}" 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="请选择${label}" 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="请选择${label}" labelField="" inputAlign="left"></van-datetime-picker></van-linear-layout></template>`; } else { const namespaceArr = propertyTypeNamespace?.split?.('.') || []; const type = namespaceArr.pop(); if (type === 'enums') { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal">${(0, _1.genEnumSelectBlock)(module.findEnumByName(propertyTypeName), `${vModel}`, '', `请选择${label}`, true)}</van-linear-layout></template>`; } else { formItem += `<template #input><van-linear-layout style="width:100%;" direction="horizontal"><van-fieldinput v-model="${vModel}" placeholder="请输入${label}"></van-fieldinput></van-linear-layout></template>`; } } formItem += `<template #title><van-text text="${label}"><van-text></template>`; formItem += ` </van-field>\n`; formItem = formItem.replace(/>\n<van-input|<\/van-input>/g, ''); return formItem; }) .join('')} <div> <van-button round block="blockb" type="info" @click="${nameGroup.viewLogicSubmit}()" text="提交修改"></van-button> </div> </van-form>`; } exports.genH5UpdateFormTemplate = genH5UpdateFormTemplate; function genSubmitLogic(updateLogic, nameGroup) { const params = [(0, _1.genLogicParam)(`${updateLogic.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: [ utils_1.NaslLogicItem.CallLogic({ calleeNamespace: updateLogic.getNamespace(), calleeName: updateLogic.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 genLoadLogic(getLogic, nameGroup) { const params = [(0, _1.genLogicParam)(`${getLogic.params[0].name}`, nameGroup.viewParamId)]; return utils_1.NaslNode.Logic({ name: nameGroup.viewLogicLoad, description: '', params: [], returns: [], body: [ utils_1.NaslLogicItem.Start, utils_1.NaslLogicItem.Assignment({ left: utils_1.NaslLogicItem.Identifier({ name: nameGroup.viewVariableEntity, }), right: utils_1.NaslLogicItem.CallLogic({ calleeNamespace: getLogic.getNamespace(), calleeName: getLogic.name, arguments: params, }), }), utils_1.NaslLogicItem.End, ], playground: [], }); } // 生成修改区块 function genUpdateBlock(entity, oldNode, useViewVariable = true, genUpdateTemplate, genLoadLogicLocal, genSubmitLogicLocal) { const likeComponent = oldNode?.likeComponent; const isBusinessComponent = likeComponent.concept === 'BusinessComponent'; const dataSource = entity.parentNode; const module = dataSource.app; const { ns } = entity; const getLogic = ns?.logics?.find((logic) => logic.name === `get`); const updateLogic = ns?.logics?.find((logic) => logic.name === `update`); const nameGroup = {}; nameGroup.viewElementMainView = likeComponent.getViewElementUniqueName('form1'); nameGroup.viewVariableEntity = likeComponent.getVariableUniqueName(utils.firstLowerCase(entity.name)); nameGroup.viewParamId = likeComponent.getParamUniqueName('id'); nameGroup.viewLogicLoad = likeComponent.getLogicUniqueName('load'); nameGroup.viewLogicSubmit = likeComponent.getLogicUniqueName('submit'); // 收集所有和本实体关联的实体 const selectNameGroupMap = new Map(); const newLogics = []; const newLoadSelectLogics = []; entity.properties.forEach((property) => { // 有外键关联 if (property.relationEntity) { 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 genTemplate = genUpdateTemplate || (config_1.config.scope === 'h5' ? genH5UpdateFormTemplate : genUpdateFormTemplate); return `<template> ${genTemplate(entity, nameGroup, selectNameGroupMap)} </template> <definition> { "viewParams": [ { "concept": "${isBusinessComponent ? 'ParamWithGroup' : 'Param'}", "name": "${nameGroup.viewParamId}", "typeAnnotation": ${JSON.stringify(utils_1.NaslCoreTypeAnnotation.Long)} } ], "viewVariables": [ ${useViewVariable ? `{ "concept": "Variable", "name": "${nameGroup.viewVariableEntity}", "typeAnnotation": ${JSON.stringify(utils_1.NaslTypeAnnotation.Reference({ typeNamespace: entity.getNamespace(), typeName: entity.name, }))} }` : ''} ], "viewLogics": [ ${JSON.stringify((genLoadLogicLocal || genLoadLogic)(getLogic, nameGroup, entity))}, ${JSON.stringify((genSubmitLogicLocal || genSubmitLogic)(updateLogic, nameGroup))} ${newLoadSelectLogics.map((logic) => `,${JSON.stringify(logic)}`).join('\n')} ], "viewBindEvents": [ { "concept": "BindEvent", "arguments": [], "name": "created", "calleeNamespace": "", "calleeName": "${nameGroup.viewLogicLoad}" } ], "logics": ${JSON.stringify(newLogics)} } </definition> `; } exports.genUpdateBlock = genUpdateBlock; exports.default = genUpdateBlock; //# sourceMappingURL=genUpdateBlock.js.map