loading-service-base.ts 1.2 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
/**
 * 加载服务基类
 *
 * @export
 * @class LoadingServiceBase
 */
export class LoadingServiceBase {

  /**
   * 是否加载
   *
   * @type {boolean}
   * @memberof LoadingServiceBase
   */
  public isLoading: boolean = false;

  /**
   * 加载结束
   *
   * @public
   * @memberof LoadingServiceBase
   */
  public endLoading(selector: any): void {
    if (!this.isLoading) {
      return
    }
    if (selector) {
      let cover = selector.querySelector('.cover');
      if (selector.contains(cover)) {
        selector.removeChild(cover);
      }
    }
    this.isLoading = false;
  }

  /**
   * 开始加载
   *
   * @public
   * @memberof LoadingServiceBase
   */
  public beginLoading(selector: any): void {
    if (this.isLoading) {
      return;
    }
    this.isLoading = true;
    // 自定义loading元素
    const userEle = document.createElement('div');
    userEle.classList.add('cover');
    const innerDiv = document.createElement('div');
    innerDiv.classList.add('loading');
    for (let i = 0; i < 4; i++) {
      const dot = document.createElement('span');
      innerDiv.appendChild(dot);
    }
    userEle.appendChild(innerDiv);
    // 挂载
    if (selector) {
      selector.appendChild(userEle);
    }

  }
}