common.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import EvenBus from '../eventBus.js'
  2. const types = {
  3. control: {
  4. unload: 'removeControl'
  5. },
  6. layer: {
  7. unload: 'removeTileLayer'
  8. },
  9. overlay: {
  10. unload: 'removeOverlay'
  11. },
  12. contextMenu: {
  13. unload: 'removeContextMenu'
  14. }
  15. }
  16. const getParent = $component => {
  17. return ($component.abstract || $component.name !== 'bm-map') ? getParent($component.$parent) : $component
  18. }
  19. //($component.abstract || $component.$el === $component.$children[0].$el) ? getParent($component.$parent) : $component
  20. function destroyInstance() {
  21. const { unload, renderByParent, $parent, ready } = this
  22. EvenBus.$off('ready', ready);
  23. if (renderByParent) {
  24. $parent.reload()
  25. }
  26. unload()
  27. }
  28. const getMixin = (prop = {}) => {
  29. return {
  30. emits: ['ready'],
  31. computed: {
  32. renderByParent() {
  33. return this.$parent.preventChildrenRender
  34. }
  35. },
  36. created() {
  37. const $parent = getParent(this.$parent)
  38. const map = $parent.map
  39. const { ready } = this
  40. map ? ready() : EvenBus.$on('ready', ready);
  41. map ? this.init({ BMap: $parent.BMap, map }) : EvenBus.$on('init', this.init);
  42. },
  43. mounted() {
  44. const $parent = getParent(this.$parent)
  45. const map = $parent.map
  46. const { mountedReady } = this
  47. map ? mountedReady() : EvenBus.$on('ready', mountedReady);
  48. },
  49. unmounted: destroyInstance,
  50. methods: {
  51. init() { },
  52. ready() {
  53. const $parent = getParent(this.$parent)
  54. const BMap = this.BMap = $parent.BMap
  55. const map = this.map = $parent.map
  56. this.load()
  57. this.$emit('ready', {
  58. BMap,
  59. map
  60. })
  61. },
  62. mountedReady() {
  63. this.mountedLoad()
  64. },
  65. transmitEvent(e) {
  66. this.$emit(e.type.replace(/^on/, ''), e)
  67. },
  68. reload() {
  69. this && this.BMap && this.$nextTick(() => {
  70. this.unload()
  71. this.$nextTick(() => {
  72. this.load();
  73. this.mountedLoad();
  74. })
  75. })
  76. },
  77. unload() {
  78. const { map, originInstance } = this
  79. try {
  80. switch (prop.type) {
  81. case 'search':
  82. return originInstance.clearResults()
  83. case 'autoComplete':
  84. case 'lushu':
  85. return originInstance.dispose()
  86. case 'markerClusterer':
  87. return originInstance.clearMarkers()
  88. default:
  89. map[types[prop.type].unload](originInstance)
  90. }
  91. } catch (e) { }
  92. },
  93. mountedLoad() { }
  94. },
  95. };
  96. }
  97. export default type => getMixin({ type });