app-design.ts 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
import Vue from 'vue';
import { Subject } from 'rxjs';
import AppDesignCompponent from "./app-design.vue";
import { LogUtil } from 'ibiz-core';

export class AppDesign {

    /**
     * 实例对象
     *
     * @private
     * @static
     * @memberof AppDesign
     */
    private static readonly $design = new AppDesign();

    /**
     * 构造方法
     * 
     * @memberof AppDesign
     */
    constructor() {
        if (AppDesign.$design) {
            return AppDesign.$design;
        }
    }

    /**
     * vue 实例
     *
     * @private
     * @type {Vue | null}
     * @memberof AppDesign
     */
    private vueExample: Vue | null = null;

    /**
     * 获取实例对象
     *
     * @static
     * @returns
     * @memberof AppDesign
     */
    public static getInstance() {
        return AppDesign.$design;
    }

    /**
     * 创建 Vue 实例对象
     *
     * @memberof AppDesign
     */
    private createVueExample(params:any): Subject<any> {
        if(this.vueExample){
            this.vueExample.$destroy(); 
            this.vueExample = null;
        }
        try {
            this.vueExample = new Vue({
                render(h) {
                    return h(AppDesignCompponent, {props:params} );
                }
            }).$mount();
            document.body.appendChild(this.vueExample.$el);
            const comp: any = this.vueExample.$children[0];
            return comp.getSubject();
        } catch (error) {
            console.error(error);
            return new Subject<any>();
        }
    }

    /**
     * 打开抽屉
     *
     * @memberof AppDesign
     */
    public openDrawer(params:any): Subject<any> {
        try {
            const subject = this.createVueExample(params);
            return subject;
        } catch (error) {
            LogUtil.log(error);
            return new Subject<any>();
        }
    }

    /**
     * @description 销毁临时vue对象
     * @memberof AppDrawer
     */
    destroyVueExample() {
        if (this.vueExample) {
            this.vueExample.$destroy();
            this.vueExample = null;
        }
    }

}