Newer
Older
CallCenterFront / src / components / fMap / base / mixins / common.js
TAN YUE on 24 Sep 2020 1 KB 20200924 新建事件添加地图
const types = {
  map: {
    unload: 'clearMap'
  },
  control: {
    unload: 'removeControl'
  },
  layer: {
    unload: 'removeLayer'
  },
  features: {
    unload: 'removeFeatures'
  },
  listener: {
    unload: 'removeListener'
  }
}

const getParent = $component => ($component.abstract || $component.$el === $component.$children[0].$el) ? getParent($component.$parent) : $component

function destroyInstance () {
  const { unload, renderByParent, $parent } = this
  if (renderByParent) {
    $parent.reload()
  }
  unload()
}

class Mixin {
  constructor (prop) {
    this.methods = {
      ready () {
        const $parent = getParent(this.$parent)
        const fmap = this.fmap = $parent.fmap
        this.load()
        this.$emit('ready', {
          fmap
        })
      },
      transmitEvent (e) {
        this.$emit(e.type.replace(/^on/, ''), e)
      },
      reload () {
        this && this.$nextTick(() => {
          this.unload()
          this.$nextTick(this.load)
        })
      },
      unload () {
        const {fmap, originInstance} = this
        try {
          switch (prop.type) {
            case 'search':
              return originInstance.clearResults()
            case 'autoComplete':
            case 'lushu':
              return originInstance.dispose()
            case 'markerClusterer':
              return originInstance.clearMarkers()
            default:
              fmap[types[prop.type].unload](originInstance)
          }
        } catch (e) {}
      }
    }
    this.computed = {
      renderByParent () {
        return this.$parent.preventChildrenRender
      }
    }
    this.mounted = function () {
      const $parent = getParent(this.$parent)
      const fmap = $parent.fmap
      const { ready } = this
      fmap ? ready() : $parent.$on('ready', ready)
    }
    this.destroyed = destroyInstance
    this.beforeDestroy = destroyInstance
  }
}

export default type => new Mixin({ type })