UNPKG

@lcap/asl

Version:

NetEase Application Specific Language

202 lines 7.45 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.Directive = void 0; // import stringify = require('json-stringify-safe'); 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' }); } } ; /** * 前端指令 * 去除了 v-model */ class Directive extends __1.Vertex { /** * @param source 需要合并的部分参数 */ constructor(source) { super(); /** * 概念类型 */ this.level = __1.LEVEL_ENUM.directive; /** * 指令类型 */ this.type = 'string'; source && this.assign(source); } /** * 转换成 Vue 的模板格式 */ toVue(placeholder, finalCode) { let modifiersString = ''; Object.keys(this.modifiers || {}).forEach((key) => { const value = this.modifiers[key]; if (value) modifiersString += '.' + key; }); let value = this.type !== 'string' ? this.value : this.value ? `'${this.value}'` : ''; if (this.expression) { value = LogicItem_1.evaluate(this.expression, finalCode); value && (value = __1.genFinalCode(value, finalCode)); if (this.expression.type === 'StringLiteral') { value = `'${value}'`; } value = value.replace && value.replace(/"/g, "'"); } return value !== '' && value !== undefined && value !== null ? `v-${this.name}${modifiersString}${this.arg ? ':' + this.arg : ''}="${value}"` : ''; } /** * 从后端 JSON 生成规范的 Directive 对象 */ static from(source, element) { if (typeof source.modifiers === 'string') source.modifiers = JSON.parse(source.modifiers); const directive = new Directive(source); directive.assign({ element }); return directive; } /** * 添加组件指令 */ async create(none, actionOptions) { __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.directiveService.create({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Directive.create', operationDesc: actionOptions?.actionDesc || `添加组件"${this.element.tag}"指令"${this.name}"`, }, body, }); this.assign({ id: result.id }); } // this.element.view && this.element.view.emit('change'); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 删除组件指令 */ async delete(none, actionOptions) { __1.config.defaultApp?.emit('saving'); if (actionOptions?.actionMode !== __1.ACTION_MODE.undoRedo) { if (this.id) { try { await page_1.directiveService.delete({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Directive.delete', operationDesc: actionOptions?.actionDesc || `删除组件"${this.element.tag}"指令"${this.name}"`, }, query: { id: this.id, }, }); } catch (err) { await __1.config.defaultApp?.history.load(); throw err; } } } this.destroy(); this.element.view && 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.directiveService.update({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Directive.update', operationDesc: actionOptions?.actionDesc || `修改组件"${this.element.tag}"指令"${this.name}"`, }, body, }); this.assign(Directive.from(this, this.element)); } this.element.view && this.element.view.emit('change'); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } } __decorate([ decorators_1.immutable() ], Directive.prototype, "level", void 0); __decorate([ decorators_1.immutable() ], Directive.prototype, "type", void 0); __decorate([ decorators_1.immutable() ], Directive.prototype, "id", void 0); __decorate([ decorators_1.immutable() ], Directive.prototype, "name", void 0); __decorate([ decorators_1.immutable() ], Directive.prototype, "rawName", void 0); __decorate([ decorators_1.immutable() ], Directive.prototype, "value", void 0); __decorate([ decorators_1.immutable() ], Directive.prototype, "arg", void 0); __decorate([ decorators_1.immutable() ], Directive.prototype, "modifiers", void 0); __decorate([ decorators_1.immutable() ], Directive.prototype, "expression", void 0); __decorate([ decorators_1.immutable() ], Directive.prototype, "elementId", void 0); __decorate([ decorators_1.circular(), decorators_1.immutable() ], Directive.prototype, "element", void 0); __decorate([ decorators_1.action('添加组件指令') ], Directive.prototype, "create", null); __decorate([ decorators_1.action('删除组件指令') ], Directive.prototype, "delete", null); __decorate([ decorators_1.action('修改组件指令') ], Directive.prototype, "update", null); exports.Directive = Directive; exports.default = Directive; //# sourceMappingURL=Directive.js.map