RouteNavigation.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <right-click-menu
  3. :mouse-position="mousePosition"
  4. :config="opt"/>
  5. <div class="route_navigation_main"
  6. ref="tagRef">
  7. <div v-for="(item,index) in menuList"
  8. :class="index === currentIndex ? 'active_text item' : 'item'"
  9. :key="index"
  10. @contextmenu.prevent="contextmenuItem(item,index,$event)"
  11. @click="handleRouterPush(item)">
  12. {{ item?.title }}
  13. <el-icon @click.stop.prevent="closeRouter(index,item.path)">
  14. <Close/>
  15. </el-icon>
  16. </div>
  17. <div class="active_box" :style="selectedStyle"/>
  18. </div>
  19. </template>
  20. <script setup>
  21. import {ref} from "vue";
  22. import tabsHook from "@/layout/HeaderV2/tabs-hook";
  23. import RightClickMenu from "@/components/menu-item/RightClickMenu";
  24. import router from '@/router'
  25. const tagRef = ref(null)
  26. const opt = [
  27. {
  28. name: '关闭', click: (data, index) => {
  29. closeRouter(index, data.path)
  30. }
  31. },
  32. {
  33. name: '关闭其他选项卡', click: (data, index) => {
  34. router.push(data.path)
  35. setLabel([data])
  36. }
  37. },
  38. {
  39. name: '关闭所有选项卡', click: (data, index) => {
  40. menuList.value = []
  41. menuKey.value = []
  42. backToTheHomepage()
  43. }
  44. },
  45. {
  46. name: '关闭左侧选项卡', click: (data, index) => {
  47. if (index === 0) return
  48. if (index > currentIndex.value) {
  49. router.push(data.path)
  50. }
  51. let temp = []
  52. for (let i = index; i < menuList.value.length; i++) {
  53. temp.push(menuList.value[i])
  54. }
  55. setLabel(temp)
  56. }
  57. },
  58. {
  59. name: '关闭右侧选项卡', click: (data, index) => {
  60. if (index + 1 === menuList.value.length) return
  61. if (index < currentIndex.value) {
  62. router.push(data.path)
  63. }
  64. let temp = []
  65. for (let i = 0; i < index + 1; i++) {
  66. temp.push(menuList.value[i])
  67. }
  68. setLabel(temp)
  69. }
  70. }
  71. ]
  72. const mousePosition = ref()
  73. const contextmenuItem = async (item, index, event) => {
  74. mousePosition.value = {
  75. event,
  76. data: item,
  77. index
  78. }
  79. }
  80. const menuList = ref(tabsHook.getItem())
  81. const menuKey = ref(tabsHook.getItemKey())
  82. const currentPath = ref('')
  83. const currentIndex = ref(-1)
  84. const closeRouter = (index, path) => {
  85. menuList.value.splice(index, 1)
  86. menuKey.value.splice(index, 1)
  87. if (menuList.value.length === 0) {
  88. let data = {
  89. path: '/dashboard',
  90. title: '首页',
  91. }
  92. menuList.value.push(data)
  93. menuKey.value.push(JSON.stringify(data))
  94. router.push('/dashboard')
  95. } else {
  96. if (router.currentRoute.value.path === path || router.currentRoute.value.meta.activeMenu === path) {
  97. let temp = index === menuList.value.length ? menuList.value.length - 1 : index
  98. router.push(menuList.value[temp]?.path)
  99. }
  100. }
  101. }
  102. // 设置标签
  103. const setLabel = (data) => {
  104. menuList.value = data
  105. menuKey.value.value = []
  106. for (let i = 0, len = menuList.value.length; i < len; i++) {
  107. menuKey.value.value.push(JSON.stringify(menuList.value[i]))
  108. }
  109. }
  110. // 回到首页
  111. const backToTheHomepage = () => {
  112. router.push('/dashboard')
  113. }
  114. // 设置滚动条位置
  115. const handleScrolling = async (str) => {
  116. currentIndex.value = menuList.value.findIndex((item) => {
  117. return item.name === str.name
  118. })
  119. if (currentIndex.value === -1) return
  120. let itemData = tagRef.value.querySelectorAll('.item')
  121. let scroll = 0;
  122. for (let i = 0; i < currentIndex.value; i++) {
  123. let item = itemData[i]
  124. scroll += item.scrollWidth
  125. }
  126. selectedStyle.value = {
  127. width: itemData[currentIndex.value].clientWidth + 'px',
  128. transform: `translateX(${scroll}px)`
  129. }
  130. tagRef.value.scrollTo({top: 0, left: scroll, behavior: 'smooth'})
  131. }
  132. const selectedStyle = ref({})
  133. function handleRouterPush(item) {
  134. router.push({
  135. name: item.name,
  136. query: item.query
  137. })
  138. }
  139. watch(() => menuList.value, () => {
  140. tabsHook.setItem(menuList.value)
  141. routingChanges(false)
  142. }, {deep: true})
  143. const createLabels = (str, data) => {
  144. if (menuKey.value.indexOf(str) > -1) return
  145. menuList.value.push(data)
  146. tabsHook.setItem(menuList.value)
  147. menuKey.value.push(str)
  148. }
  149. const routingChanges = (createTabs = true) => {
  150. const currentRoute = router.currentRoute.value
  151. currentPath.value = currentRoute.path
  152. const data = {
  153. path: currentRoute.path,
  154. title: currentRoute.meta.title,
  155. name: currentRoute.name,
  156. query: currentRoute.query
  157. }
  158. const routerInfoToStr = JSON.stringify(data)
  159. nextTick(() => {
  160. handleScrolling(data)
  161. })
  162. // 不需要创建
  163. if (currentRoute.meta.hideTabs) return;
  164. if (createTabs) {
  165. createLabels(routerInfoToStr, data)
  166. }
  167. }
  168. watch(() => router.currentRoute.value, () => {
  169. routingChanges()
  170. }, {immediate: true})
  171. </script>
  172. <style scoped lang="scss">
  173. $item-height: 40px;
  174. .route_navigation_main {
  175. flex: 1;
  176. display: flex;
  177. height: 100%;
  178. width: 100%;
  179. position: relative;
  180. overflow-x: auto;
  181. overflow-y: hidden;
  182. margin-right: 16px;
  183. }
  184. .active_text {
  185. color: var(--xc-header-active-text-color);
  186. opacity: 1 !important;
  187. }
  188. .active_box {
  189. position: absolute;
  190. height: $item-height;
  191. border-radius: 4px;
  192. background-color: var(--xc-header-active-bg-color);
  193. box-shadow: var(--el-box-shadow-light);
  194. transition: all .2s;
  195. -webkit-transition: all .2s;
  196. }
  197. .route_navigation_main::-webkit-scrollbar {
  198. height: 4px;
  199. background-color: #eaeaea;
  200. }
  201. .route_navigation_main:hover ::-webkit-scrollbar-track-piece {
  202. /*鼠标移动上去再显示滚动条*/
  203. background-color: #fff;
  204. /* 滚动条的背景颜色 */
  205. border-radius: 6px;
  206. /* 滚动条的圆角宽度 */
  207. }
  208. .route_navigation_main:hover::-webkit-scrollbar-thumb { /*滚动条里面小方块*/
  209. border-radius: 10px;
  210. -webkit-box-shadow: inset 0 0 5px rgba(28, 28, 28, 0.2);
  211. background: #919191;
  212. }
  213. .route_navigation_main::-webkit-scrollbar-track {
  214. background: white;
  215. }
  216. .item {
  217. display: flex;
  218. align-items: center;
  219. justify-content: center;
  220. padding: 0 20px;
  221. cursor: pointer;
  222. z-index: 1;
  223. height: $item-height;
  224. user-select: none;
  225. transition: all .2s;
  226. white-space: nowrap;
  227. -webkit-transition: all .2s;
  228. opacity: .7;
  229. &:hover {
  230. opacity: 1;
  231. }
  232. i {
  233. padding: 2px;
  234. font-size: 12px;
  235. margin-left: 5px;
  236. color: rgb(0, 0, 0);
  237. &:hover {
  238. background-color: #0a84fd;
  239. color: white;
  240. border-radius: 50%;
  241. }
  242. }
  243. }
  244. </style>