UNPKG

@lcap/asl

Version:

NetEase Application Specific Language

379 lines 13.3 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Page = void 0; const decorators_1 = require("../decorators"); const __1 = require(".."); const page_1 = __importDefault(require("../../service/page")); const utils_1 = require("../utils"); const preprocess_1 = require("../../service/common/preprocess"); /** * 页面(入口页)类,后端路由控制 * @category page */ class Page extends __1.Vertex { /** * @param source 需要合并的部分参数 */ constructor(source) { super(); /** * 概念类型 */ this.level = __1.LEVEL_ENUM.page; /** * 页面 Id */ this.id = undefined; /** * 页面名称 */ this.name = undefined; /** * 页面标题 */ this.title = undefined; /** * 是否接入权限 */ this.auth = undefined; /** * 是否为首页 */ this.isIndex = undefined; /** * 根页面视图 */ this.rootView = undefined; /** * 所属前端服务 Id */ this.serviceId = undefined; /** * 所属前端服务 */ this.service = undefined; /** * 树组件的子节点字段 */ this.childrenField = 'rootView.children'; /** * 树组件的子节点字段 */ this.moreChildrenFields = ['rootView.$def.params', 'rootView.$def.variables', 'rootView.$def.logics']; source && this.assign(source); } /** * 加载页面详情 * @requires this.id */ async load() { const result = await page_1.default.loadPage({ path: { id: this.id, }, }); this.assign(result); utils_1.traverse((current) => { if (current.parent) { const view = current.parent.children[current.index] = new __1.View(current.node); view.assign({ page: this, parent: current.parent }); // view.parseAll(); } }, { node: this.rootView }); const rootView = new __1.View(this.rootView); rootView.assign({ page: this }); this.assign({ rootView }); } addData(res) { const microService = this.service.app.firstMicroService; const interfaces = res.interfaces; if (interfaces && interfaces.length) { interfaces.forEach((item, index) => { const itface = interfaces[index] = __1.Interface.from(item, microService); preprocess_1.postServiceType(itface); }); microService.interfaces.unshift(...interfaces); microService.globalLogic.globalLogics.unshift(...interfaces); microService.emit('interfacesChange'); } const enums = res.enums; if (enums && enums.length) { enums.forEach((item, index) => enums[index] = __1.Enum.from(item, microService)); microService.data.enums.unshift(...enums); __1.updateDataTypeList(); } const entities = res.entities; if (entities && entities.length) { entities.forEach((item, index) => entities[index] = __1.Entity.from(item, microService)); microService.data.entities.unshift(...entities); __1.updateDataTypeList(); } const structures = res.structures; if (structures && structures.length) { structures.forEach((item, index) => structures[index] = __1.Structure.from(item, microService)); microService.data.structures.unshift(...structures); __1.updateDataTypeList(); } } /** * 添加页面 */ async importPage(none, actionOptions, then) { __1.config.defaultApp?.emit('saving'); const body = this.toJSON(); __1.utils.logger.debug('添加页面', body); const result = await page_1.default.importPage({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Page.create', operationDesc: actionOptions?.actionDesc || `添加页面"${this.name}${this.title ? `(${this.title})` : ''}"`, }, body, }); this.deepPick(result.page, ['id']); // 添加数据和接口 this.addData(result); const rootView = __1.View.from(result.page.rootView, null, this); rootView.assign({ tempPath: `/${rootView.name}`, code: `/ID_${rootView.id}`, }); // page 添加一次 rootView this.assign({ rootView }); // Destination选择页面时用的是page的code,这里需要赋值下 this.assign({ tempPath: `/${rootView.name}`, code: `/ID_${rootView.id}`, }); this.rootView.attachNodePath(); if (result.page.isIndex) { const tasks = this.service.pages.filter((page) => page.isIndex && page.id !== result.page.id) .map((page) => { page.assign({ isIndex: false }); return page.update(); }); Promise.all(tasks); } await then?.(); this.service.emit('pageTreeChange'); const microService = this.service.app.firstMicroService; microService.emit('dataTypesChange'); microService.emit('enumsChange'); microService.emit('vertexIdToNameChange'); this.rootView.emit('change'); // this.assign(Page.from(this, this.service)); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 添加页面 */ async create(none, actionOptions, then) { __1.config.defaultApp?.emit('saving'); const body = this.toJSON(); __1.utils.logger.debug('添加页面', body); const result = await page_1.default.addPage({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Page.create', operationDesc: actionOptions?.actionDesc || `添加页面"${this.name}${this.title ? `(${this.title})` : ''}"`, }, body, }); this.deepPick(result, ['id']); this.rootView.attachNodePath(); if (result.isIndex) { const tasks = this.service.pages.filter((page) => page.isIndex && page.id !== result.id) .map((page) => { page.assign({ isIndex: false }); return page.update(); }); Promise.all(tasks); } await then?.(); this.service.emit('pageTreeChange'); this.rootView.emit('change'); // this.assign(Page.from(this, this.service)); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 删除页面 */ async delete(none, actionOptions) { __1.config.defaultApp?.emit('saving'); try { await page_1.default.removePage({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Page.delete', operationDesc: actionOptions?.actionDesc || `删除页面"${this.name}${this.title ? `(${this.title})` : ''}"`, }, path: { id: this.id, }, }); } catch (err) { await __1.config.defaultApp?.history.load(); throw err; } const index = this.service.pages.indexOf(this); ~index && this.service.pages.splice(index, 1); this.destroy(); this.service.emit('pageTreeChange'); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); } /** * 修改页面 */ async update(none, actionOptions, then) { __1.config.defaultApp?.emit('saving'); const body = this.toPlainJSON(); __1.utils.logger.debug('修改页面', body); const result = await page_1.default.update({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Page.delete', operationDesc: actionOptions?.actionDesc || `修改页面"${this.name}${this.title ? `(${this.title})` : ''}"`, }, body, }); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 设为首页 */ async setAsIndex() { __1.config.defaultApp?.emit('saving'); const tasks = this.service.pages.filter((page) => page.isIndex) .map((page) => { page.assign({ isIndex: false }); return page.update(); }); this.assign({ isIndex: true }); tasks.push(this.update(undefined, { actionDesc: `设置页面"${this.name}"为首页`, })); await Promise.all(tasks); this.service.emit('pageTreeChange'); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); } /** * 设置是否开启权限控制 */ async setAuth(auth) { this.assign({ auth }); await this.update(undefined, { actionDesc: `设置${this.auth ? '开启' : '关闭'}页面"${this.name}"权限控制`, }); } // /** // * 加载子页面详情 // * @requires this.id // * @deprecated // */ // async loadViewTree() { // let rootView = await pageService.loadViewTree({ // query: { // id: this.id, // }, // config: { // mock: config.mock, // }, // }); // traverse((current) => { // if (current.parent) { // const view = current.parent.children[current.index] = new View(current.node); // view.assign({ page: this, parent: current.parent }); // view.parseAll(); // } // }, { node: rootView, parent: null, index: null, nodePath: '' }); // rootView = new View(rootView); // rootView.parseAll(); // this.assign({ rootView }); // return rootView; // } /** * 从后端 JSON 生成规范的 Param 对象 */ static from(source, service) { const page = new Page(source); page.assign({ rootView: __1.View.from(page.rootView, null, page), service, tempPath: page.rootView ? `/${page.rootView.name}` : '', code: page.rootView ? `/ID_${page.rootView.id}` : '', }); return page; } } __decorate([ decorators_1.immutable() ], Page.prototype, "level", void 0); __decorate([ decorators_1.immutable() ], Page.prototype, "id", void 0); __decorate([ decorators_1.immutable() ], Page.prototype, "name", void 0); __decorate([ decorators_1.immutable() ], Page.prototype, "title", void 0); __decorate([ decorators_1.immutable() ], Page.prototype, "auth", void 0); __decorate([ decorators_1.immutable() ], Page.prototype, "isIndex", void 0); __decorate([ decorators_1.immutable() ], Page.prototype, "rootView", void 0); __decorate([ decorators_1.immutable() ], Page.prototype, "serviceId", void 0); __decorate([ decorators_1.circular(), decorators_1.immutable() ], Page.prototype, "service", void 0); __decorate([ decorators_1.excludedInJSON(), decorators_1.immutable() ], Page.prototype, "childrenField", void 0); __decorate([ decorators_1.excludedInJSON(), decorators_1.immutable() ], Page.prototype, "moreChildrenFields", void 0); __decorate([ decorators_1.action('导入页面') ], Page.prototype, "importPage", null); __decorate([ decorators_1.action('添加页面') ], Page.prototype, "create", null); __decorate([ decorators_1.action('删除页面') ], Page.prototype, "delete", null); __decorate([ decorators_1.action('设为首页') ], Page.prototype, "setAsIndex", null); __decorate([ decorators_1.action('设置是否开启权限控制') ], Page.prototype, "setAuth", null); exports.Page = Page; exports.default = Page; //# sourceMappingURL=Page.js.map