@lcap/asl
Version:
NetEase Application Specific Language
217 lines • 7.77 kB
JavaScript
;
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.Attr = void 0;
const decorators_1 = require("../decorators");
const __1 = require("..");
const page_1 = require("../../service/page");
const LogicItem_1 = require("../logic/LogicItem");
const utils_1 = require("../utils");
function generateBody(body) {
if (body.expression) {
utils_1.traverse((current) => {
delete current.node.editable;
delete current.node.index;
delete current.node.memberIdentifierSchema;
}, { node: body.expression }, { mode: 'anyObject' });
}
}
/**
* 前端组件属性
* 纯组件属性,不包含事件、指令等
* @examples
* - 纯字符串:color="primary"
* - 静态表达式::min="23"
* - 静态表达式::data="[{ text: '选项1', value: '选项1' }, { text: '选项2', value: '选项2' }]"
* - 动态表达式::expr="$utils.LowerCase(a + b)"
*/
class Attr extends __1.Vertex {
/**
* @param source 需要合并的部分参数
*/
constructor(source) {
super();
/**
* 概念类型
*/
this.level = __1.LEVEL_ENUM.attr;
/**
* 属性类型
*/
this.type = 'string';
/**
* model 是否开启
*/
this.model = false;
/**
* sync 是否开启
*/
this.sync = false;
source && this.assign(source);
}
/**
* 转换成 Vue 的模板格式
*/
toVue(placeholder, finalCode) {
let value = this.value;
if (this.expression && this.type === 'dynamic') {
value = LogicItem_1.evaluate(this.expression, finalCode);
value && (value = __1.genFinalCode(value, finalCode));
}
if (value !== undefined && value !== null && typeof value === 'string') {
value = value.replace(/"/g, '\'');
}
if (this.type === 'string') {
return value !== undefined && value !== null ? `${this.name}="${value}"` : '';
}
else {
return value !== undefined && value !== null && value !== '' ? `:${this.name}${this.sync ? '.sync' : ''}="${value}"` : '';
}
}
/**
* 从后端 JSON 生成规范的 Attr 对象
*/
static from(source, element) {
const attr = new Attr(source);
attr.assign({ element });
return attr;
}
/**
* 添加组件属性
*/
async create(none, actionOptions) {
if (actionOptions?.actionMode === __1.ACTION_MODE.local) {
this.element?.view?.emit('local-change');
return this;
}
__1.config.defaultApp?.emit('saving');
if (actionOptions?.actionMode !== __1.ACTION_MODE.undoRedo) {
const body = this.toJSON();
generateBody(body);
__1.utils.logger.debug('添加组件属性', body);
const result = await page_1.attributeService.create({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Attr.create',
operationDesc: actionOptions?.actionDesc || `添加组件"${this.element.tag}"属性"${this.name}"`,
},
body,
});
this.assign({ id: result });
}
this.element?.view?.emit('change');
await __1.config.defaultApp?.history.load();
__1.config.defaultApp?.emit('saved');
return this;
}
/**
* 删除组件属性
*/
async delete(none, actionOptions) {
if (actionOptions?.actionMode === __1.ACTION_MODE.local) {
const index = this.element.attrList.indexOf(this);
~index && this.element.attrList.splice(index, 1);
this.element?.view?.emit('local-change');
return this;
}
__1.config.defaultApp?.emit('saving');
if (actionOptions?.actionMode !== __1.ACTION_MODE.undoRedo) {
if (this.id) {
try {
await page_1.attributeService.delete({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Attr.delete',
operationDesc: actionOptions?.actionDesc || `删除组件"${this.element.tag}"属性"${this.name}"`,
},
query: {
id: this.id,
},
});
}
catch (err) {
await __1.config.defaultApp?.history.load();
throw err;
}
}
}
const index = this.element.attrList.indexOf(this);
~index && this.element.attrList.splice(index, 1);
this.destroy();
this.element?.view?.emit('change');
await __1.config.defaultApp?.history.load();
__1.config.defaultApp?.emit('saved');
}
/**
* 修改组件属性
*/
async update(data, actionOptions) {
__1.config.defaultApp?.emit('saving');
data && this.assign(data);
if (actionOptions?.actionMode !== __1.ACTION_MODE.undoRedo) {
const body = this.toJSON();
generateBody(body);
__1.utils.logger.debug('修改组件属性', body);
await page_1.attributeService.update({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Attr.update',
operationDesc: actionOptions?.actionDesc || `修改组件"${this.element.tag}"属性"${this.name}"`,
},
body,
});
}
this.element?.view?.emit('change');
await __1.config.defaultApp?.history.load();
__1.config.defaultApp?.emit('saved');
return this;
}
}
__decorate([
decorators_1.immutable()
], Attr.prototype, "level", void 0);
__decorate([
decorators_1.immutable()
], Attr.prototype, "type", void 0);
__decorate([
decorators_1.immutable()
], Attr.prototype, "id", void 0);
__decorate([
decorators_1.immutable()
], Attr.prototype, "name", void 0);
__decorate([
decorators_1.immutable()
], Attr.prototype, "value", void 0);
__decorate([
decorators_1.immutable()
], Attr.prototype, "model", void 0);
__decorate([
decorators_1.immutable()
], Attr.prototype, "sync", void 0);
__decorate([
decorators_1.immutable()
], Attr.prototype, "expression", void 0);
__decorate([
decorators_1.immutable()
], Attr.prototype, "elementId", void 0);
__decorate([
decorators_1.circular(),
decorators_1.immutable()
], Attr.prototype, "element", void 0);
__decorate([
decorators_1.action('添加组件属性')
], Attr.prototype, "create", null);
__decorate([
decorators_1.action('删除组件属性')
], Attr.prototype, "delete", null);
__decorate([
decorators_1.action('修改组件属性')
], Attr.prototype, "update", null);
exports.Attr = Attr;
exports.default = Attr;
//# sourceMappingURL=Attr.js.map