Menu.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <template>
  2. <div>
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import commonMixin from '../base/mixins/common.js'
  8. export default {
  9. name: 'bm-context-menu',
  10. props: {
  11. width: {
  12. type: Number
  13. }
  14. },
  15. mixins: [commonMixin('contextMenu')],
  16. methods: {
  17. load() {
  18. const { width, BMap, map, $parent } = this
  19. const parent = this.parent = $parent.originInstance || map
  20. if (this.originInstance) {
  21. parent.removeContextMenu(this.originInstance)
  22. }
  23. const menu = this.originInstance = new BMap.ContextMenu()
  24. if (this.$slots.default) {
  25. for (const item of this.$slots.default() || []) {
  26. const props = item.props;
  27. if (props.seperator) {
  28. menu.addSeparator()
  29. continue
  30. }
  31. const menuItem = new BMap.MenuItem(props.text, function (point, pixel) {
  32. props.callback && props.callback({
  33. point,
  34. pixel,
  35. BMap,
  36. map,
  37. target: parent
  38. })
  39. }, {
  40. width,
  41. id: props.id,
  42. iconUrl: props.iconUrl
  43. })
  44. props.disabled ? menuItem.disable() : menuItem.enable()
  45. props.originInstance = menuItem
  46. menu.addItem(menuItem)
  47. }
  48. }
  49. parent.addContextMenu(menu)
  50. }
  51. }
  52. }
  53. </script>