popup 点位弹窗


export default class popup {
  constructor(vue) {
    // 弹窗的ID
    this.id = undefined;
    // 弹窗对应的事件处理函数
    this.fun = undefined;
    // Vue实例
    this.vue = vue;
    // 保存弹窗类型和ID的映射关系
    this.popTypeMap = new Map();
  }

  /**
   * 创建弹窗
   * @param {String} type 弹框类型
   * @param {Vue} popup 弹框组件
   * @param {Object} options 携带参数
   * @returns
   */
  createPopup(type, popup, options, popupheight, popupLeft, gensui = true) {
    // 如果已经存在相同ID的弹窗,则直接返回
    if (document.getElementById(options.id)) return;

    if (this.id) {
      // 如果当前存在弹窗,则先移除
      if (document.getElementById(this.id)) {
        this.removePopup();
      }
    }
    // 使用Vue.extend方法创建弹窗的Vue实例
    let windowVm = this.vue.extend(popup);
    // 创建弹窗的Vue实例,并传入propsData
    this.vmInstance = new windowVm({
      propsData: {
        info: {
          ...options,
        },
      },
    });
    // 将store对象设置为弹窗实例的$store属性
    this.vmInstance.$store = options.store;
    // 手动挂载Vue
    this.vmInstance.$mount();
    // 弹窗关闭事件的处理函数
    this.vmInstance.closeEvent = (e) => {
      // XGisMap.YBLS.create.entity.point.pointFlash("")
      // 移除弹窗
      this.removePopup();
    };

    // 将弹窗实例的根元素添加到DOM中
    document.getElementById('app').appendChild(this.vmInstance.$el);
    // 如果gensui参数为true,则添加回调函数,用于实现弹窗的跟随效果
    if (gensui) {
      this.fun = earth.czm.viewer.scene.postRender.addEventListener(() => {
        if (!document.getElementById(options.id)) return;
        const scratch = new Cesium.Cartesian2();

        let winpos = earth.czm.viewer.scene.cartesianToCanvasCoordinates(
          options.position,
          scratch
        );
        if (Cesium.defined(winpos)) {
          this.vmInstance.$el.style.top =
            winpos.y -
            this.vmInstance.$el.clientHeight -
            (popupheight || 80) +
            'px';
          this.vmInstance.$el.style.left =
            winpos.x -
            this.vmInstance.$el.clientWidth / 2 -
            (popupLeft || 0) +
            'px';
        }
      });
    }
    // 设置当前弹窗的ID
    this.id = options.id;
    // 将弹窗类型和ID添加到映射关系中
    this.popTypeMap.set(type, this.id);
    // 返回弹窗实例
    return this.vmInstance;
  }
  /**
   * @description: 销毁弹窗
   * @return {*}
   */
  removePopup() {
    // 判断是否存在当前弹窗元素
    if (document.getElementById(this.id)) {
      // 从DOM中移除弹窗元素
      document
        .getElementById('app')
        .removeChild(document.getElementById(this.id));
      // 销毁弹窗实例
      this.vmInstance.$destroy();
      if (this.fun) {
        this.fun(); // 移除回调函数
      }
    }
  }
  /**
   * @description: 根据类型删除弹窗
   * @param {*} type 弹窗类型
   * @return {*}
   */
  removePopupByType(type) {
    if (this.popTypeMap.has(type)) {
      let id = this.popTypeMap.get(type);
      if (document.getElementById(id)) {
        document.getElementById('app').removeChild(document.getElementById(id));
      }
      // XGisMap.YBLS.create.entity.point.pointFlash("")
      this.vmInstance.$destroy(); // 销毁弹窗实例
      if (this.fun) {
        this.fun(); // 移除回调函数
      }

      this.popTypeMap.delete(type); // 移除映射关系中对应的弹窗类型和ID
    }
  }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值