TopMenuBar.vue 6.2 KB

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