@lcap/asl
Version:
NetEase Application Specific Language
319 lines • 10.5 kB
JavaScript
"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.Structure = void 0;
const decorators_1 = require("../decorators");
const __1 = require("..");
const data_1 = require("../../service/data");
const dataTypeUtils_1 = require("./dataTypeUtils");
const index_1 = require("../index");
/**
* 数据结构类
*/
class Structure extends __1.Vertex {
/**
* 周边存在的名称
*/
constructor(source) {
super();
/**
* 概念类型
*/
this.level = __1.LEVEL_ENUM.structure;
/**
* Id
*/
this.id = undefined;
/**
* dataTypes 中的唯一标识
*/
this.schemaRef = undefined;
/**
* 名称
*/
this.name = undefined;
/**
* 描述
*/
this.description = undefined;
/**
* 类型
*/
this.type = 'object';
/**
* 属性列表
*/
this.propertyList = [];
this.resolvers = undefined;
/**
* 所属服务 Id
*/
this.serviceId = undefined;
/**
* catogory Name
*/
this.category = undefined;
/**
* 所属服务类型
*/
this.serviceType = undefined;
/**
* 所属实体 Id
*/
this.entityId = undefined;
/**
* 所属服务
*/
this.service = undefined;
/**
* 父节点
*/
this.dataNode = undefined;
/**
* 周边存在的名称
*/
this.existingNames = [];
source && this.assign(source);
}
/**
* 按当前 id 加载数据结构数据
* @requires this.id
*/
async load() {
const result = await data_1.structureService.loadDetail({
query: {
id: this.id,
},
config: {
mock: __1.config.mock,
},
});
this.assign(result);
return this;
}
/**
* 按当前 id 加载数据结构数据
* @requires this.id
*/
async loadPro() {
const result = await data_1.structureService.loadDetail({
query: {
id: this.id,
},
});
Object.assign(result, result.definition);
delete result.definition;
dataTypeUtils_1.convert2RefType(result);
const structure = new Structure(result);
const propertyList = structure.propertyList;
propertyList.forEach((property, index) => {
property = propertyList[index] = new __1.StructureProperty(property);
property.assign({ root: structure });
});
const service = this.dataNode.service;
structure.assign({
schemaRef: `#/${service.id}/${structure.id}`,
dataNode: service.data,
expanded: true,
});
__1.dataTypesMap[structure.schemaRef] = structure;
this.assign(structure);
const pos = this.dataNode.structures.findIndex((item) => item.id === this.id);
if (pos === -1)
this.dataNode.structures.unshift(this);
else {
this.dataNode.structures.splice(pos, 1, structure);
}
__1.updateDataTypeList();
index_1.updateAllVariablesChildrenSchema();
this.dataNode.service.emit('dataTypesChange');
this.dataNode.service.emit('vertexIdToNameChange', this.id, this.name);
return this;
}
/**
* 添加数据结构
*/
async create(none, actionOptions, then) {
__1.config.defaultApp?.emit('saving');
const body = this.toJSON();
__1.utils.logger.debug('添加数据结构', body);
const result = await data_1.structureService.create({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Structure.create',
operationDesc: actionOptions?.actionDesc || `添加数据结构"${this.name}"`,
},
body,
});
this.deepPick(result, ['id']);
this.assign({
schemaRef: `#/${this.dataNode.service.id}/${result.id}`,
});
__1.dataTypesMap[this.schemaRef] = this;
await then?.();
__1.updateDataTypeList();
this.dataNode.service.emit('dataTypesChange');
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.structureService.delete({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Structure.delete',
operationDesc: actionOptions?.actionDesc || `删除数据结构"${this.name}"`,
},
query: {
id: this.id,
},
});
}
const index = this.dataNode.structures.indexOf(this);
~index && this.dataNode.structures.splice(index, 1);
this.dataNode.service.globalLogic.removeCategoryStructure(this);
delete __1.dataTypesMap[this.schemaRef];
__1.updateDataTypeList();
this.destroy();
this.dataNode.service.emit('dataTypesChange');
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('', ['propertyList']);
__1.utils.logger.debug('修改数据结构', body);
const result = await data_1.structureService.update({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Structure.update',
operationDesc: actionOptions?.actionDesc || `修改数据结构"${this.name}"`,
},
body,
});
dataTypeUtils_1.convert2RefType(result);
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('vertexIdToNameChange', this.id, this.name);
}
/**
* 设置数据结构描述
* @param description 描述
*/
async setDescription(description) {
this.assign({ description });
await this.update(undefined, {
actionDesc: `设置数据结构"${this.name}"的描述为"${description}"`,
});
}
/**
* 从后端 JSON 生成规范的 Structure 对象
*/
static from(source, service) {
dataTypeUtils_1.convert2RefType(source);
const structure = new Structure(source);
structure.assign({
dataNode: service.data,
schemaRef: `#/${service.id}/${structure.id}`,
});
const propertyList = structure.propertyList;
propertyList.forEach((property, index) => {
property = propertyList[index] = new __1.StructureProperty(property);
property.assign({ root: structure });
});
structure.assign({ resolvers: undefined });
__1.dataTypesMap[structure.schemaRef] = structure;
return structure;
}
}
__decorate([
decorators_1.immutable()
], Structure.prototype, "level", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "id", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "schemaRef", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "name", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "description", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "type", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "propertyList", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "resolvers", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "serviceId", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "category", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "serviceType", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "entityId", void 0);
__decorate([
decorators_1.immutable()
], Structure.prototype, "service", void 0);
__decorate([
decorators_1.circular(),
decorators_1.immutable()
], Structure.prototype, "dataNode", void 0);
__decorate([
decorators_1.excludedInJSON()
], Structure.prototype, "existingNames", void 0);
__decorate([
decorators_1.action('添加数据结构')
], Structure.prototype, "create", null);
__decorate([
decorators_1.action('删除数据结构')
], Structure.prototype, "delete", null);
__decorate([
decorators_1.action('设置数据结构名称')
], Structure.prototype, "setName", null);
__decorate([
decorators_1.action('设置数据结构描述')
], Structure.prototype, "setDescription", null);
exports.Structure = Structure;
exports.default = Structure;
//# sourceMappingURL=Structure.js.map