callout.js 5.0 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7
import {
  MapType,
  getMapInfo
} from '../../../../helpers/location'
const mapInfo = getMapInfo()
const ISAMAP = mapInfo.type === MapType.AMAP

Q
qiang 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20
export function createCallout (maps) {
  function onAdd () {
    const div = this.div
    const panes = this.getPanes()
    panes.floatPane.appendChild(div)
  }
  function onRemove () {
    const parentNode = this.div.parentNode
    if (parentNode) {
      parentNode.removeChild(this.div)
    }
  }

DCloud-WZF's avatar
DCloud-WZF 已提交
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
  function createAMapText () {
    const option = this.option
    this.Text = new maps.Text({
      text: option.content,
      anchor: 'bottom-center', // 设置文本标记锚点
      offset: new maps.Pixel(0, option.offsetY),
      style: {
        'margin-bottom': '1rem',
        padding: (option.padding || 8) + 'px',
        'line-height': (option.fontSize || 14) + 'px',
        'border-radius': (option.borderRadius || 0) + 'px',
        'border-color': `${option.bgColor || '#fff'} transparent transparent`,
        'background-color': option.bgColor || '#fff',
        'box-shadow': '0 2px 6px 0 rgba(114, 124, 245, .5)',
        'text-align': 'center',
        'font-size': (option.fontSize || 14) + 'px',
        color: option.color || '#000'
      },
      position: option.position
    })

    maps.event.addListener(this.Text, 'click', () => {
      this.callback(this.parent)
    })

    this.Text.setMap(option.map)
  }

  function removeAMapText () {
50 51 52
    if (this.Text) {
      this.option.map.remove(this.Text)
    }
DCloud-WZF's avatar
DCloud-WZF 已提交
53 54
  }

Q
qiang 已提交
55 56 57 58 59 60 61 62
  class Callout {
    option
    position
    index
    visible
    alwaysVisible
    div
    triangle
DCloud-WZF's avatar
DCloud-WZF 已提交
63 64 65
    callback
    parent
    Text
Q
qiang 已提交
66 67 68 69 70 71 72 73 74

    set onclick (callback) {
      this.div.onclick = callback
    }

    get onclick () {
      return this.div.onclick
    }

DCloud-WZF's avatar
DCloud-WZF 已提交
75
    constructor (option = {}, callback, parent) {
Q
qiang 已提交
76
      this.option = option || {}
DCloud-WZF's avatar
DCloud-WZF 已提交
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
      this.visible = this.alwaysVisible = option.display === 'ALWAYS'
      if (ISAMAP) {
        this.callback = callback
        this.parent = parent
        if (this.visible) {
          this.createAMapText()
        }
      } else {
        const map = option.map
        this.position = option.position
        this.index = 1
        const div = (this.div = document.createElement('div'))
        const divStyle = div.style
        divStyle.position = 'absolute'
        divStyle.whiteSpace = 'nowrap'
        divStyle.transform = 'translateX(-50%) translateY(-100%)'
        divStyle.zIndex = '1'
        divStyle.boxShadow = option.boxShadow || 'none'
        divStyle.display = this.visible ? 'block' : 'none'
        const triangle = (this.triangle = document.createElement('div'))
        triangle.setAttribute(
          'style',
          'position: absolute;white-space: nowrap;border-width: 4px;border-style: solid;border-color: #fff transparent transparent;border-image: initial;font-size: 12px;padding: 0px;background-color: transparent;width: 0px;height: 0px;transform: translate(-50%, 100%);left: 50%;bottom: 0;'
        )
        this.setStyle(option)
        div.appendChild(triangle)
        if (map) {
          this.setMap(map)
        }
Q
qiang 已提交
106 107 108
      }
    }

DCloud-WZF's avatar
DCloud-WZF 已提交
109 110 111
    createAMapText = createAMapText
    removeAMapText = removeAMapText

Q
qiang 已提交
112 113 114 115 116 117 118 119 120
    onAdd = onAdd
    construct = onAdd
    setOption (option) {
      this.option = option
      if (option.display === 'ALWAYS') {
        this.alwaysVisible = this.visible = true
      } else {
        this.alwaysVisible = false
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
121 122 123 124 125 126 127 128
      if (ISAMAP) {
        if (this.visible) {
          this.createAMapText()
        }
      } else {
        this.setPosition(option.position)
        this.setStyle(option)
      }
Q
qiang 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    }

    setStyle (option) {
      const div = this.div
      const divStyle = div.style
      div.innerText = option.content || ''
      divStyle.lineHeight = (option.fontSize || 14) + 'px'
      divStyle.fontSize = (option.fontSize || 14) + 'px'
      divStyle.padding = (option.padding || 8) + 'px'
      divStyle.color = option.color || '#000'
      divStyle.borderRadius = (option.borderRadius || 0) + 'px'
      divStyle.backgroundColor = option.bgColor || '#fff'
      divStyle.marginTop = '-' + ((option.top || 0) + 5) + 'px'
      this.triangle.style.borderColor = `${option.bgColor || '#fff'} transparent transparent`
    }

    setPosition (position) {
      this.position = position
      this.draw()
    }

    draw () {
      const overlayProjection = this.getProjection()
      if (!this.position || !this.div || !overlayProjection) {
        return
      }
      const pixel = overlayProjection.fromLatLngToDivPixel(this.position)
      const divStyle = this.div.style
      divStyle.left = pixel.x + 'px'
      divStyle.top = pixel.y + 'px'
    }

    changed () {
      const divStyle = this.div.style
      divStyle.display = this.visible ? 'block' : 'none'
    }

    onRemove = onRemove

    destroy = onRemove
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
170 171 172 173 174 175 176
  if (!ISAMAP) {
    const prototype = Callout.prototype
    const overlay = new (maps.OverlayView || maps.Overlay)()
    for (const key in overlay) {
      if (!(key in prototype)) {
        prototype[key] = overlay[key]
      }
177 178
    }
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
179

Q
qiang 已提交
180 181
  return Callout
}