Blame view

node_modules/vue-baidu-map/components/base/mixins/common.js 1.94 KB
290144e9   易尊强   第一次
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
  const types = {
    control: {
      unload: 'removeControl'
    },
    layer: {
      unload: 'removeTileLayer'
    },
    overlay: {
      unload: 'removeOverlay'
    },
    contextMenu: {
      unload: 'removeContextMenu'
    }
  }
  
  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 BMap = this.BMap = $parent.BMap
          const map = this.map = $parent.map
          this.load()
          this.$emit('ready', {
            BMap,
            map
          })
        },
        transmitEvent (e) {
          this.$emit(e.type.replace(/^on/, ''), e)
        },
        reload () {
          this && this.BMap && 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 () {
        const $parent = getParent(this.$parent)
        const map = $parent.map
        const {ready} = this
        map ? ready() : $parent.$on('ready', ready)
      }
      this.destroyed = destroyInstance
      this.beforeDestroy = destroyInstance
    }
  }
  
  export default type => new Mixin({type})