InfoWindow.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <div v-show="show">
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import commonMixin from '../base/mixins/common.js'
  8. import bindEvents from '../base/bindEvent.js'
  9. import { createPoint, createSize } from '../base/factory.js'
  10. import { deleteEmptyKey } from '../base/util.js'
  11. export default {
  12. name: 'bm-info-window',
  13. mixins: [commonMixin('overlay')],
  14. props: {
  15. show: {
  16. type: Boolean
  17. },
  18. position: {
  19. type: Object
  20. },
  21. title: {
  22. type: String
  23. },
  24. width: {
  25. type: Number
  26. },
  27. height: {
  28. type: Number
  29. },
  30. maxWidth: {
  31. type: Number
  32. },
  33. offset: {
  34. type: Object
  35. },
  36. maximize: {
  37. type: Boolean
  38. },
  39. autoPan: {
  40. type: Boolean
  41. },
  42. closeOnClick: {
  43. type: Boolean,
  44. default: true
  45. },
  46. message: {
  47. type: String
  48. }
  49. },
  50. watch: {
  51. show(val) {
  52. val ? this.openInfoWindow() : this.closeInfoWindow()
  53. },
  54. 'position.lng'(val, oldVal) {
  55. this.reload()
  56. },
  57. 'position.lat'(val, oldVal) {
  58. this.reload()
  59. },
  60. 'offset.width'(val, oldVal) {
  61. this.reload()
  62. },
  63. 'offset.height'(val) {
  64. this.reload()
  65. },
  66. maxWidth() {
  67. this.reload()
  68. },
  69. width(val) {
  70. this.originInstance.setWidth(val)
  71. },
  72. height(val) {
  73. this.originInstance.setHeight(val)
  74. },
  75. title(val) {
  76. this.originInstance.setTitle(val)
  77. },
  78. maximize(val) {
  79. val ? this.originInstance.enableMaximize() : this.originInstance.disableMaximize()
  80. },
  81. autoPan(val) {
  82. val ? this.originInstance.enableAutoPan() : this.originInstance.disableAutoPan()
  83. },
  84. closeOnClick(val) {
  85. val ? this.originInstance.enableCloseOnClick() : this.originInstance.disableCloseOnClick()
  86. }
  87. },
  88. methods: {
  89. redraw() {
  90. this.originInstance.redraw()
  91. },
  92. load() {
  93. const { BMap, map, show, title, width, height, maxWidth, offset, autoPan, closeOnClick, message, maximize, bindObserver, $parent } = this
  94. const $content = this.$el
  95. let infoWindowOption = {
  96. width,
  97. height,
  98. title,
  99. maxWidth,
  100. offset: offset && createSize(BMap, offset),
  101. enableAutoPan: autoPan,
  102. enableCloseOnClick: closeOnClick,
  103. enableMessage: typeof message === 'undefined',
  104. message
  105. };
  106. deleteEmptyKey(infoWindowOption);
  107. const overlay = new BMap.InfoWindow($content, infoWindowOption)
  108. maximize ? overlay.enableMaximize() : overlay.disableMaximize()
  109. bindEvents.call(this, overlay)
  110. this.originInstance = overlay
  111. overlay.redraw()
  112. ;[].forEach.call($content.querySelectorAll('img'), $img => {
  113. $img.onload = () => overlay.redraw()
  114. })
  115. bindObserver()
  116. this.$container = $parent.originInstance && $parent.originInstance.openInfoWindow ? $parent.originInstance : map
  117. show && this.openInfoWindow()
  118. },
  119. bindObserver() {
  120. const MutationObserver = window.MutationObserver
  121. if (!MutationObserver) {
  122. return
  123. }
  124. const { $el, originInstance } = this
  125. this.observer = new MutationObserver(mutations => originInstance.redraw())
  126. this.observer.observe($el, { attributes: true, childList: true, characterData: true, subtree: true })
  127. },
  128. openInfoWindow() {
  129. const { BMap, $container, position, originInstance } = this
  130. $container.openInfoWindow(originInstance, createPoint(BMap, position || this.$parent.position))
  131. },
  132. closeInfoWindow() {
  133. this.$container.closeInfoWindow(this.originInstance)
  134. }
  135. }
  136. }
  137. </script>