loading.ts 1.9 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 100
import { AppServiceBase } from 'ibiz-core';
import Vue from 'vue';
import AppLoading from "./app-loading.vue";
/**
 * 全局加载动画工具类
 *
 * @export
 * @class Loading
 */
export class Loading {

    /**
     * i18n对象
     *
     * @private
     * @memberof AppModal
     */
    private i18n: any;

    /**
     * 加载个数计数
     *
     * @protected
     * @type {number}
     * @memberof DragDesignBase
     */
    protected static loadingCount: number = 0;

    /**
     * vue 实例
     *
     * @private
     * @type {Vue}
     * @memberof Loading
     */
    private static vueExample?: Vue;

    /**
     * 构造方法
     * 
     * @memberof AppDrawer
     */
    constructor() {
      this.initBasicData();
    }

    /**
     * 初始化基础数据
     * 
     * @memberof AppDrawer
     */
    private initBasicData(){
        const appService = AppServiceBase.getInstance();
        this.i18n = appService.getI18n();
    }

    /**
     * 显示加载动画
     *
     * @static
     * @memberof Loading
     */
    public static show(): void {
      if (this.loadingCount === 0) {
        let component = AppLoading;
        let vm: any = new Vue({
            // i18n: this.i18n,
            render(h) {
                return h(component);
            },
        }).$mount();
        this.vueExample = vm;
        let app =  document.getElementById("app");
        if(app){
            app.appendChild(vm.$el);
        }
      }
      this.loadingCount++;
  }

  /**
   * 隐藏加载动画
   *
   * @memberof DragDesignBase
   */
  public static hidden(): void {
      this.loadingCount--;
      if (this.loadingCount < 0) {
          this.loadingCount = 0;
      }
      if (this.loadingCount === 0) {
        let app =  document.getElementById("app");
        if(app){
          if(this.vueExample){
            app.removeChild(this.vueExample.$el);
          }
      }
      }
  }
}