TopMenuBar.vue 6.3 KB

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