ContextMenu.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <n-dropdown
  3. :show="show"
  4. :options="options"
  5. :x="x"
  6. :y="y"
  7. placement="bottom-start"
  8. @clickoutside="handleHideDropdown"
  9. @select="handleSelect"
  10. />
  11. </template>
  12. <script setup>
  13. import { useTabStore } from '@/store'
  14. const props = defineProps({
  15. show: {
  16. type: Boolean,
  17. default: false,
  18. },
  19. currentPath: {
  20. type: String,
  21. default: '',
  22. },
  23. x: {
  24. type: Number,
  25. default: 0,
  26. },
  27. y: {
  28. type: Number,
  29. default: 0,
  30. },
  31. })
  32. const emit = defineEmits(['update:show'])
  33. const tabStore = useTabStore()
  34. const options = computed(() => [
  35. {
  36. label: '重新加载',
  37. key: 'reload',
  38. disabled: props.currentPath !== tabStore.activeTab,
  39. icon: () => h('i', { class: 'i-mdi:refresh text-14' }),
  40. },
  41. {
  42. label: '关闭',
  43. key: 'close',
  44. disabled: tabStore.tabs.length <= 1,
  45. icon: () => h('i', { class: 'i-mdi:close text-14' }),
  46. },
  47. {
  48. label: '关闭其他',
  49. key: 'close-other',
  50. disabled: tabStore.tabs.length <= 1,
  51. icon: () => h('i', { class: 'i-mdi:arrow-expand-horizontal text-14' }),
  52. },
  53. {
  54. label: '关闭左侧',
  55. key: 'close-left',
  56. disabled: tabStore.tabs.length <= 1 || props.currentPath === tabStore.tabs[0].path,
  57. icon: () => h('i', { class: 'i-mdi:arrow-expand-left text-14' }),
  58. },
  59. {
  60. label: '关闭右侧',
  61. key: 'close-right',
  62. disabled:
  63. tabStore.tabs.length <= 1
  64. || props.currentPath === tabStore.tabs[tabStore.tabs.length - 1].path,
  65. icon: () => h('i', { class: 'i-mdi:arrow-expand-right text-14' }),
  66. },
  67. ])
  68. const route = useRoute()
  69. const actionMap = new Map([
  70. [
  71. 'reload',
  72. () => {
  73. tabStore.reloadTab(route.fullPath, route.meta?.keepAlive)
  74. },
  75. ],
  76. [
  77. 'close',
  78. () => {
  79. tabStore.removeTab(props.currentPath)
  80. },
  81. ],
  82. [
  83. 'close-other',
  84. () => {
  85. tabStore.removeOther(props.currentPath)
  86. },
  87. ],
  88. [
  89. 'close-left',
  90. () => {
  91. tabStore.removeLeft(props.currentPath)
  92. },
  93. ],
  94. [
  95. 'close-right',
  96. () => {
  97. tabStore.removeRight(props.currentPath)
  98. },
  99. ],
  100. ])
  101. function handleHideDropdown() {
  102. emit('update:show', false)
  103. }
  104. function handleSelect(key) {
  105. const actionFn = actionMap.get(key)
  106. if (typeof actionFn === 'function')
  107. actionFn()
  108. handleHideDropdown()
  109. }
  110. </script>