import { createVNode, VNode, render as vueRender } from 'vue'; import { IContext, IOpenViewResult, IParam, IViewDetail } from '@/core'; import { AppModal } from '@components/common/modal'; export class ModalHelper { /** * @description 抽屉唯一实例 * @private * @static * @memberof ModalHelper */ private static readonly modalInstance = new ModalHelper(); /** * vue 实例 * * @private * @type {Vue | null} * @memberof ModalHelper */ private vueExample: VNode | null = null; /** * @description 抽屉容器 * @private * @type {(Node | null)} * @memberof ModalHelper */ private container: Node | null = null; /** * 构造方法 * * @memberof ModalHelper */ constructor() { if (ModalHelper.modalInstance) { return ModalHelper.modalInstance; } } /** * @description 获取实例 * @static * @return {*} * @memberof ModalHelper */ static getInstance() { return ModalHelper.modalInstance; } /** * @description 打开模态 * @param {IViewDetail} view * @param {IContext} context * @param {IParam} viewParam * @return {*} {Promise} * @memberof ModalHelper */ public openModal( view: IViewDetail, context: IContext, viewParams: IParam, customParam: IParam = {} ): Promise { return new Promise((resolve, reject) => { try { this.container = document.createElement('div'); document.body.appendChild(this.container); this.vueExample = createVNode(AppModal, { view, context, viewParams, customParam, onModalAction: (result: IOpenViewResult) => { if (result) { resolve(result); } if (this.vueExample) { this.vueExample = null; } if (this.container) { document.body.removeChild(this.container); this.container = null; } }, }); const appInstance = App.getUIAppInstance(); if (appInstance) { this.vueExample.appContext = appInstance.appContext; } vueRender(this.vueExample, this.container as any); } catch (error) { reject(error); } }); } }