common.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const types = {
  2. control: {
  3. unload: 'removeControl'
  4. },
  5. layer: {
  6. unload: 'removeTileLayer'
  7. },
  8. overlay: {
  9. unload: 'removeOverlay'
  10. },
  11. contextMenu: {
  12. unload: 'removeContextMenu'
  13. }
  14. }
  15. const getParent = $component => ($component.abstract || $component.$el === $component.$children[0].$el) ? getParent($component.$parent) : $component
  16. function destroyInstance () {
  17. const {unload, renderByParent, $parent} = this
  18. if (renderByParent) {
  19. $parent.reload()
  20. }
  21. unload()
  22. }
  23. class Mixin {
  24. constructor (prop) {
  25. this.methods = {
  26. ready () {
  27. const $parent = getParent(this.$parent)
  28. const BMap = this.BMap = $parent.BMap
  29. const map = this.map = $parent.map
  30. this.load()
  31. this.$emit('ready', {
  32. BMap,
  33. map
  34. })
  35. },
  36. transmitEvent (e) {
  37. this.$emit(e.type.replace(/^on/, ''), e)
  38. },
  39. reload () {
  40. this && this.BMap && this.$nextTick(() => {
  41. this.unload()
  42. this.$nextTick(this.load)
  43. })
  44. },
  45. unload () {
  46. const {map, originInstance} = this
  47. try {
  48. switch (prop.type) {
  49. case 'search':
  50. return originInstance.clearResults()
  51. case 'autoComplete':
  52. case 'lushu':
  53. return originInstance.dispose()
  54. case 'markerClusterer':
  55. return originInstance.clearMarkers()
  56. default:
  57. map[types[prop.type].unload](originInstance)
  58. }
  59. } catch (e) {}
  60. }
  61. }
  62. this.computed = {
  63. renderByParent () {
  64. return this.$parent.preventChildrenRender
  65. }
  66. }
  67. this.mounted = function () {
  68. const $parent = getParent(this.$parent)
  69. const map = $parent.map
  70. const {ready} = this
  71. map ? ready() : $parent.$on('ready', ready)
  72. }
  73. this.destroyed = destroyInstance
  74. this.beforeDestroy = destroyInstance
  75. }
  76. }
  77. export default type => new Mixin({type})