TopMenuBar.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <div class="hc-top-menu-bar">
  3. <el-scrollbar always>
  4. <div class="bar-menu-content">
  5. <div v-for="(item, index) in barMenuData" class="bar-menu-btn" :class="item.key === barRoutes.key?'cur':''"
  6. @click="barMenuClick(item)" @contextmenu.prevent="barMenuContextMenu($event, item, index)">
  7. <span>{{item.title}}</span>
  8. <div class="bar-close-icon" @click.stop="barMenuCloseClick(item, index)">
  9. <HcIcon name="close"/>
  10. </div>
  11. </div>
  12. </div>
  13. </el-scrollbar>
  14. <!--右键菜单-->
  15. <HcContextMenu ref="contextMenuRef" :datas="menusData" @item-click="handleMenuSelect"/>
  16. </div>
  17. </template>
  18. <script setup>
  19. import {onMounted, ref, watch} from "vue";
  20. import {useAppStore} from "~src/store";
  21. import {useRouter, useRoute} from 'vue-router'
  22. import {getStoreData, setStoreData} from '~src/utils/storage'
  23. //初始组合式
  24. const router = useRouter()
  25. const useRoutes = useRoute()
  26. const useAppState = useAppStore()
  27. //初始变量
  28. const barMenuData = ref(getStoreData('bar-menu-datas') || []);
  29. const barRoutes = ref({key: '', path: '', title: ''});
  30. //渲染完成
  31. onMounted(() => {
  32. const {name, path, meta} = useRoutes
  33. barRoutes.value = {path, key: name, title: meta?.title}
  34. setBarMenuData()
  35. })
  36. //监听
  37. watch(() => [
  38. useRoutes?.name,
  39. useRoutes?.path,
  40. useRoutes?.meta?.title,
  41. ], ([key, path, title]) => {
  42. barRoutes.value = {path, key, title}
  43. setBarMenuData()
  44. })
  45. //设置菜单数据
  46. const setBarMenuData = () => {
  47. const {key, path, title} = barRoutes.value
  48. if (key !== 'home-index') {
  49. const index = barMenuData.value.findIndex(item => item.key === key)
  50. if (index === -1) {
  51. barMenuData.value.push({path, key: key, title: title})
  52. }
  53. setStoreData('bar-menu-datas', barMenuData.value)
  54. }
  55. }
  56. //菜单被点击
  57. const barMenuClick = (item) => {
  58. const { key } = barRoutes.value
  59. if (key !== item.key) {
  60. router.push({name: item.key});
  61. }
  62. }
  63. //鼠标右键菜单
  64. const contextMenuRef = ref(null);
  65. const barItem = ref({});
  66. const barItemIndex = ref(0);
  67. const menusData = ref([
  68. {label: '关闭当前', key: "close"},
  69. {label: '关闭所有', key: "all"},
  70. ]);
  71. const barMenuContextMenu = (event, item, index) => {
  72. event.preventDefault();
  73. barItem.value = item;
  74. barItemIndex.value = index;
  75. contextMenuRef.value?.showMenu(event);
  76. }
  77. //鼠标右键菜单被点击
  78. const handleMenuSelect = ({key}) => {
  79. if (key === 'close') {
  80. barMenuCloseClick(barItem.value, barItemIndex.value)
  81. } else if (key === 'all') {
  82. barMenuData.value = []
  83. setStoreData('bar-menu-datas', [])
  84. useAppState.menuBarShow = false;
  85. router.push({name: 'home-index'});
  86. }
  87. }
  88. //菜单关闭被点击
  89. const barMenuCloseClick = (item, index) => {
  90. const total = barMenuData.value.length -1;
  91. const { key } = barRoutes.value
  92. barMenuData.value.splice(index, 1)
  93. if (key === item.key) {
  94. let items = {};
  95. const indexs = barMenuData.value.length -1;
  96. if(total > index) {
  97. items = barMenuData.value[index]
  98. } else if(indexs >= 0) {
  99. items = barMenuData.value[indexs]
  100. }
  101. if (indexs < 0) {
  102. setStoreData('bar-menu-datas', barMenuData.value)
  103. useAppState.menuBarShow = false;
  104. router.push({name: 'home-index'});
  105. } else {
  106. barRoutes.value = items
  107. setStoreData('bar-menu-datas', barMenuData.value)
  108. router.push({name: items.key});
  109. }
  110. } else {
  111. setStoreData('bar-menu-datas', barMenuData.value)
  112. }
  113. }
  114. </script>
  115. <style lang="scss">
  116. .hc-top-menu-bar {
  117. position: relative;
  118. padding: 0 24px 20px;
  119. margin-top: -8px;
  120. .bar-menu-content {
  121. display: flex;
  122. position: relative;
  123. .bar-menu-btn {
  124. position: relative;
  125. color: #b3b3b3;
  126. padding-left: 10px;
  127. padding-right: 6px;
  128. height: 32px;
  129. font-size: 14px;
  130. display: flex;
  131. align-items: center;
  132. justify-content: center;
  133. background: #ffffff;
  134. border: 1px solid #ffffff;
  135. border-radius: 4px;
  136. user-select: none;
  137. cursor: pointer;
  138. white-space: nowrap;
  139. transition: background 0.3s, color 0.3s;
  140. &:hover:not([class*='cur']) {
  141. background: var(--el-color-primary-light-9);
  142. color: #838791;
  143. }
  144. &:active:not([class*='cur']) {
  145. background: var(--el-color-primary-light-8);
  146. color: #838791;
  147. }
  148. &.cur {
  149. color: #ffffff;
  150. cursor: default;
  151. background: linear-gradient(to right, var(--el-color-primary-light-5), var(--el-color-primary), var(--el-color-primary-dark-2));
  152. background-size: 200%;
  153. transition: background-position 0.5s;
  154. }
  155. .bar-close-icon {
  156. height: 30px;
  157. width: 18px;
  158. display: flex;
  159. align-items: center;
  160. justify-content: center;
  161. margin-left: 6px;
  162. font-size: 16px;
  163. cursor: pointer;
  164. transition: color 0.3s;
  165. &:hover {
  166. color: var(--el-color-primary);
  167. }
  168. }
  169. }
  170. .bar-menu-btn.cur .bar-close-icon:hover {
  171. color: #000000;
  172. }
  173. .bar-menu-btn + .bar-menu-btn {
  174. margin-left: 10px;
  175. }
  176. }
  177. .el-scrollbar__bar.is-horizontal {
  178. bottom: -10px;
  179. }
  180. .el-scrollbar__bar.is-vertical {
  181. display: none;
  182. }
  183. }
  184. </style>