common copy.js 2.7 KB

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