@lcap/asl
Version:
NetEase Application Specific Language
380 lines • 12.9 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.StructureProperty = void 0;
const decorators_1 = require("../decorators");
const __1 = require("..");
const data_1 = require("../../service/data");
const basicTypes_1 = require("./basicTypes");
const dataTypeUtils_1 = require("./dataTypeUtils");
const BaseVariable_1 = require("../logic/BaseVariable");
const common_1 = require("../../service/common");
/**
* 数据结构属性类
*/
class StructureProperty extends __1.Vertex {
/**
* @param source 需要合并的部分参数
*/
constructor(source) {
super();
/**
* 概念类型
*/
this.level = __1.LEVEL_ENUM.property;
/**
* Id
*/
this.id = undefined;
/**
* 名称
*/
this.name = undefined;
/**
* 标签
*/
this.label = undefined;
/**
* 描述
*/
this.description = undefined;
/**
* type
*/
this.type = undefined;
/**
* 数据格式
*/
this.format = undefined;
/**
* generic 数据格式
*/
this.typeInstantiation = undefined;
/**
* 是否必须
*/
this.required = undefined;
/**
* 数据类型
*/
this.$ref = undefined;
/**
* 是否为数组,老的 Schema 中仍然用这种形式
*/
this.isArray = undefined;
/**
* 规则列表
*/
this.rules = undefined;
/**
* 默认值
*/
this.defaultValue = undefined;
/**
* 父级 Id
*/
this.structureId = undefined;
/**
* 父级引用
*/
this.root = undefined;
/**
* 节点是否为叶子节点
* 前端 UI 状态
*/
this.isLeaf = true;
/**
* 周边存在的名称
*/
this.existingNames = [];
source && this.assign(source);
}
/**
* 添加数据结构属性
*/
async create(none, actionOptions) {
__1.config.defaultApp?.emit('saving');
const body = this.toJSON();
body._posIndex = this.root.propertyList.indexOf(this);
__1.utils.logger.debug('添加数据结构属性', body);
const result = await data_1.structureService.addProperty({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'StructureProperty.create',
operationDesc: actionOptions?.actionDesc || `添加数据结构"${this.root.name}"属性"${this.name}"`,
},
body,
});
// convert2RefType(result);
this.deepPick(result, ['id']);
BaseVariable_1.updateAllVariablesChildrenSchema();
this.root.dataNode.service.emit('dataTypesChange');
this.root.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.deleteProperty({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'StructureProperty.delete',
operationDesc: actionOptions?.actionDesc || `删除数据结构"${this.root.name}"属性"${this.name}"`,
},
query: {
id: this.id,
},
});
}
const index = this.root.propertyList.indexOf(this);
~index && this.root.propertyList.splice(index, 1);
BaseVariable_1.updateAllVariablesChildrenSchema();
this.destroy();
this.root.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();
delete body.schemaRef;
__1.utils.logger.debug('修改数据结构属性', body);
const result = await data_1.structureService.updateProperty({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'StructureProperty.update',
operationDesc: actionOptions?.actionDesc || `修改数据结构"${this.root.name}"属性"${this.name}"`,
},
body,
});
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: `设置数据结构"${this.root.name}"的属性"${oldName}"的名称为"${name}"`,
});
this.root.dataNode.service.emit('dataTypesChange');
this.root.dataNode.service.emit('vertexIdToNameChange', this.id, this.name);
}
/**
* 设置数据结构属性标题
* @param label 标题
*/
async setLabel(label) {
this.assign({ label });
await this.update(undefined, {
actionDesc: `设置数据结构"${this.root.name}"的属性"${this.name}"的标题为"${label}"`,
});
}
/**
* 设置数据结构属性描述
* @param description 描述
*/
async setDescription(description) {
this.assign({ description });
await this.update(undefined, {
actionDesc: `设置数据结构"${this.root.name}"的属性"${this.name}"的描述为"${description}"`,
});
}
/**
* 查找schema 顶点被引用的逻辑顶点列表
*/
async getSchemaUsage() {
const result = await common_1.schemaService.getSchemaUsage({
query: {
schemaId: this.id,
valSource: 'structure',
},
});
return result;
}
/**
* 设置数据结构属性类型
*/
async setDataType(schema) {
// 用于update失败后还原数据
const originalData = {
$ref: this.$ref,
type: this.type,
format: this.format,
typeInstantiation: this.typeInstantiation,
defaultValue: this.defaultValue,
rules: this.rules,
};
this.assign({
$ref: undefined,
type: undefined,
format: undefined,
typeInstantiation: undefined,
});
this.assign(schema);
this.assign({
defaultValue: basicTypes_1.getBasicTypeDefaultValue(),
rules: [],
});
try {
await this.update(undefined, {
actionDesc: `设置数据结构"${this.root.name}"的属性"${this.name}"的类型为"${schema.typeKey}"`,
});
BaseVariable_1.updateAllVariablesChildrenSchema();
this.root.dataNode.service.emit('dataTypesChange');
}
catch (err) {
this.assign(dataTypeUtils_1.convert2RefType(originalData));
__1.config.defaultApp?.emit('saved');
throw err;
}
}
/**
* 设置数据结构属性是否为列表
* @param isArray
*/
async setAsList(isArray) {
this.assign({ isArray });
this.assign({ defaultValue: basicTypes_1.getBasicTypeDefaultValue() });
await this.update(undefined, {
actionDesc: `设置数据结构"${this.root.name}"的属性"${this.name}"${isArray ? '为' : '不为'}列表`,
});
this.root.dataNode.service.emit('dataTypesChange');
}
/**
* 设置数据结构属性默认值
*/
async setDefaultValue(defaultValue) {
this.assign({ defaultValue });
await this.update(undefined, {
actionDesc: `设置数据结构"${this.root.name}"的属性"${this.name}"的默认值为"${defaultValue}"`,
});
this.root.dataNode.service.emit('dataTypesChange');
}
/**
* 设置数据结构属性必须
* @param required 必须
*/
async setRequired(required) {
this.assign({ required });
await this.update(undefined, {
actionDesc: `设置数据结构"${this.root.name}"的属性"${this.name}"为"${required ? '必须' : '不必须'}"`,
});
this.root.dataNode.service.emit('dataTypesChange');
}
async setRules(rules) {
this.assign({ rules });
await this.update(undefined, {
actionDesc: `设置数据结构属性规则`,
});
}
/**
* 从后端 JSON 生成规范的 StructureProperty 对象
*/
static from(source, root) {
dataTypeUtils_1.convert2RefType(source.schema);
}
}
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "level", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "id", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "name", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "label", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "description", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "type", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "format", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "typeInstantiation", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "required", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "$ref", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "isArray", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "rules", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "defaultValue", void 0);
__decorate([
decorators_1.immutable()
], StructureProperty.prototype, "structureId", void 0);
__decorate([
decorators_1.circular(),
decorators_1.immutable()
], StructureProperty.prototype, "root", void 0);
__decorate([
decorators_1.excludedInJSON()
], StructureProperty.prototype, "isLeaf", void 0);
__decorate([
decorators_1.excludedInJSON()
], StructureProperty.prototype, "existingNames", void 0);
__decorate([
decorators_1.action('添加数据结构属性')
], StructureProperty.prototype, "create", null);
__decorate([
decorators_1.action('删除数据结构属性')
], StructureProperty.prototype, "delete", null);
__decorate([
decorators_1.action('设置数据结构属性名称')
], StructureProperty.prototype, "setName", null);
__decorate([
decorators_1.action('设置数据结构属性标题')
], StructureProperty.prototype, "setLabel", null);
__decorate([
decorators_1.action('设置数据结构属性描述')
], StructureProperty.prototype, "setDescription", null);
__decorate([
decorators_1.action('设置数据结构属性类型')
], StructureProperty.prototype, "setDataType", null);
__decorate([
decorators_1.action('设置数据结构属性是否为列表')
], StructureProperty.prototype, "setAsList", null);
__decorate([
decorators_1.action('设置数据结构属性默认值')
], StructureProperty.prototype, "setDefaultValue", null);
__decorate([
decorators_1.action('设置数据结构属性必须')
], StructureProperty.prototype, "setRequired", null);
__decorate([
decorators_1.action('设置数据结构属性规则')
], StructureProperty.prototype, "setRules", null);
exports.StructureProperty = StructureProperty;
exports.default = StructureProperty;
//# sourceMappingURL=StructureProperty.js.map