UNPKG

@lcap/asl

Version:

NetEase Application Specific Language

262 lines 8.39 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Enum = void 0; const decorators_1 = require("../decorators"); const __1 = require(".."); const data_1 = require("../../service/data"); const dataTypes_1 = require("./dataTypes"); /** * 枚举类型 */ class Enum extends __1.Vertex { /** * @param source 需要合并的部分参数 */ constructor(source) { super(); /** * 概念类型 */ this.level = __1.LEVEL_ENUM.enum; // 后面换成 enum /** * 枚举 Id */ this.id = undefined; /** * dataTypes 中的唯一标识 */ this.schemaRef = undefined; /** * 枚举名称 */ this.name = undefined; /** * 枚举标题 */ this.label = undefined; /** * 枚举描述 */ this.description = undefined; /** * 枚举类型 * @deprecated 兼容老版 */ this.type = 'enum'; /** * 枚举项 */ this.enumItemList = []; /** * 所属服务 Id */ this.serviceId = undefined; /** * 所属服务类型 */ this.serviceType = undefined; /** * 所属服务 */ this.service = undefined; /** * 父节点 */ this.dataNode = undefined; this.existingNames = []; source && this.assign(source); } /** * 添加枚举 */ async create(none, actionOptions) { const body = this.toJSON(); __1.utils.logger.debug('添加枚举', body); const result = await data_1.enumService.create({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Enum.create', operationDesc: actionOptions?.actionDesc || `添加枚举"${this.name}"`, }, body, }); this.deepPick(result, ['id']); this.assign({ schemaRef: `#/${this.dataNode.service.id}/${result.id}`, }); dataTypes_1.dataTypesMap[this.schemaRef] = this; __1.updateDataTypeList(); this.dataNode.service.emit('dataTypesChange'); this.dataNode.service.emit('enumsChange'); this.dataNode.service.emit('vertexIdToNameChange', this.id, this.name); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 删除枚举 */ async delete(none, actionOptions) { __1.config.defaultApp?.emit('saving'); if (this.id) { await data_1.enumService.delete({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Enum.delete', operationDesc: actionOptions?.actionDesc || `删除枚举"${this.name}"`, }, query: { id: this.id, }, }); const index = this.dataNode.enums.indexOf(this); ~index && this.dataNode.enums.splice(index, 1); delete dataTypes_1.dataTypesMap[this.schemaRef]; } __1.updateDataTypeList(); this.destroy(); this.dataNode.service.emit('dataTypesChange'); this.dataNode.service.emit('enumsChange'); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); } /** * 修改枚举 */ async update(none, actionOptions, then) { __1.config.defaultApp?.emit('saving'); const body = this.toJSON('', ['enumItemList']); __1.utils.logger.debug('修改枚举', body); const result = await data_1.enumService.update({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Enum.update', operationDesc: actionOptions?.actionDesc || `修改枚举"${this.name}"`, }, body, }); this.plainAssign(result); await then?.(); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 设置枚举名称 * @param name 名称 */ async setName(name) { const oldName = this.name; this.assign({ name }); await this.update(undefined, { actionDesc: `设置枚举"${oldName}"的名称为"${name}"`, }); __1.updateDataTypeList(); this.dataNode.service.emit('dataTypesChange'); this.dataNode.service.emit('enumsChange'); this.dataNode.service.emit('vertexIdToNameChange', this.id, this.name); } /** * 设置枚举标题 * @param name 标题 */ async setLabel(label) { const oldLabel = this.label; this.assign({ label }); await this.update(undefined, { actionDesc: `设置枚举"${oldLabel}"的标题为"${label}"`, }); } /** * 设置枚举描述 * @param description 描述 */ async setDescription(description) { this.assign({ description }); await this.update(undefined, { actionDesc: `设置枚举"${this.name}"的描述为"${description}"`, }); } /** * 从后端 JSON 生成规范的 Enum 对象 */ static from(source, service) { __1.convert2RefType(source); const enumObject = new Enum(source); enumObject.assign({ dataNode: service.data, schemaRef: `#/${service.id}/${enumObject.id}`, }); const enumItemList = enumObject.enumItemList; enumItemList.forEach((property, index) => { property = enumItemList[index] = new __1.EnumItem(property); property.assign({ root: enumObject }); }); dataTypes_1.dataTypesMap[enumObject.schemaRef] = enumObject; return enumObject; } } __decorate([ decorators_1.immutable() ], Enum.prototype, "level", void 0); __decorate([ decorators_1.immutable() ], Enum.prototype, "id", void 0); __decorate([ decorators_1.immutable() ], Enum.prototype, "schemaRef", void 0); __decorate([ decorators_1.immutable() ], Enum.prototype, "name", void 0); __decorate([ decorators_1.immutable() ], Enum.prototype, "label", void 0); __decorate([ decorators_1.immutable() ], Enum.prototype, "description", void 0); __decorate([ decorators_1.immutable() ], Enum.prototype, "type", void 0); __decorate([ decorators_1.immutable() ], Enum.prototype, "enumItemList", void 0); __decorate([ decorators_1.immutable() ], Enum.prototype, "serviceId", void 0); __decorate([ decorators_1.immutable() ], Enum.prototype, "serviceType", void 0); __decorate([ decorators_1.immutable() ], Enum.prototype, "service", void 0); __decorate([ decorators_1.circular(), decorators_1.immutable() ], Enum.prototype, "dataNode", void 0); __decorate([ decorators_1.excludedInJSON() ], Enum.prototype, "existingNames", void 0); __decorate([ decorators_1.action('添加枚举') ], Enum.prototype, "create", null); __decorate([ decorators_1.action('删除枚举') ], Enum.prototype, "delete", null); __decorate([ decorators_1.action('设置枚举名称') ], Enum.prototype, "setName", null); __decorate([ decorators_1.action('设置枚举标题') ], Enum.prototype, "setLabel", null); __decorate([ decorators_1.action('设置枚举描述') ], Enum.prototype, "setDescription", null); exports.Enum = Enum; exports.default = Enum; //# sourceMappingURL=Enum.js.map