Newer
Older
baseResourceFront / src / components / mapWindow / base / mixins / common.js
StephanieGitHub on 30 Jun 2021 1 KB MOD: 重写统计分析,公厕地图
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 map = this.map = $parent.map
        this.load()
        this.$emit('ready', {
          map
        })
      },
      transmitEvent (e) {
        this.$emit(e.type.replace(/^on/, ''), e)
      },
      reload () {
        this && this.$nextTick(() => {
          this.unload()
          this.$nextTick(this.load)
        })
      },
      unload () {
        const { map, originInstance } = this
        try {
          switch (prop.type) {
            case 'search':
              return originInstance.clearResults()
            case 'autoComplete':
            case 'lushu':
              return originInstance.dispose()
            case 'markerClusterer':
              return originInstance.clearMarkers()
            default:
              map[types[prop.type].unload](originInstance)
          }
        } catch (e) {}
      }
    }
    this.computed = {
      renderByParent () {
        return this.$parent.preventChildrenRender
      }
    }
    this.mounted = function () {
      console.log('common mounted')
      const $parent = getParent(this.$parent)
      const map = $parent.map
      const { ready } = this
      console.log(map)
      map ? ready() : $parent.$on('onload', ready)
    }
  }
}

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