Blame view

node_modules/vue-baidu-map/components/overlays/InfoWindow.vue 3.35 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  <template>
  <div v-show="show">
    <slot></slot>
  </div>
  </template>
  
  <script>
  import commonMixin from '../base/mixins/common.js'
  import bindEvents from '../base/bindEvent.js'
  import {createPoint, createSize} from '../base/factory.js'
  
  export default {
    name: 'bm-info-window',
    mixins: [commonMixin('overlay')],
    props: {
      show: {
        type: Boolean
      },
      position: {
        type: Object
      },
      title: {
        type: String
      },
      width: {
        type: Number
      },
      height: {
        type: Number
      },
      maxWidth: {
        type: Number
      },
      offset: {
        type: Object
      },
      maximize: {
        type: Boolean
      },
      autoPan: {
        type: Boolean
      },
      closeOnClick: {
        type: Boolean,
        default: true
      },
      message: {
        type: String
      }
    },
    watch: {
      show (val) {
        val ? this.openInfoWindow() : this.closeInfoWindow()
      },
      'position.lng' (val, oldVal) {
        this.reload()
      },
      'position.lat' (val, oldVal) {
        this.reload()
      },
      'offset.width' (val, oldVal) {
        this.reload()
      },
      'offset.height' (val) {
        this.reload()
      },
      maxWidth () {
        this.reload()
      },
      width (val) {
        this.originInstance.setWidth(val)
      },
      height (val) {
        this.originInstance.setHeight(val)
      },
      title (val) {
        this.originInstance.setTitle(val)
      },
      maximize (val) {
        val ? this.originInstance.enableMaximize() : this.originInstance.disableMaximize()
      },
      autoPan (val) {
        val ? this.originInstance.enableAutoPan() : this.originInstance.disableAutoPan()
      },
      closeOnClick (val) {
        val ? this.originInstance.enableCloseOnClick() : this.originInstance.disableCloseOnClick()
      }
    },
    methods: {
      redraw () {
        this.originInstance.redraw()
      },
      load () {
        const {BMap, map, show, title, width, height, maxWidth, offset, autoPan, closeOnClick, message, maximize, bindObserver, $parent} = this
        const $content = this.$el
        const overlay = new BMap.InfoWindow($content, {
          width,
          height,
          title,
          maxWidth,
          offset: createSize(BMap, offset),
          enableAutoPan: autoPan,
          enableCloseOnClick: closeOnClick,
          enableMessage: typeof message === 'undefined',
          message
        })
  
        maximize ? overlay.enableMaximize() : overlay.disableMaximize()
        bindEvents.call(this, overlay)
        this.originInstance = overlay
        overlay.redraw()
        ;[].forEach.call($content.querySelectorAll('img'), $img => {
          $img.onload = () => overlay.redraw()
        })
        bindObserver()
        this.$container = $parent.originInstance && $parent.originInstance.openInfoWindow ? $parent.originInstance : map
        show && this.openInfoWindow()
      },
      bindObserver () {
        const MutationObserver = global.MutationObserver
        if (!MutationObserver) {
          return
        }
        const {$el, originInstance} = this
        this.observer = new MutationObserver(mutations => originInstance.redraw())
        this.observer.observe($el, {attributes: true, childList: true, characterData: true, subtree: true})
      },
      openInfoWindow () {
        const {BMap, $container, position, originInstance} = this
        $container.openInfoWindow(originInstance, createPoint(BMap, position))
      },
      closeInfoWindow () {
        this.$container.closeInfoWindow(this.originInstance)
      }
    }
  }
  </script>