446 lines
11 KiB
Vue
446 lines
11 KiB
Vue
<template>
|
||
<div class="tags-view-container">
|
||
<div class="navbar-wrapper">
|
||
<navbar />
|
||
</div>
|
||
<div class="shortcut-key" v-show="shortcutExpanded">
|
||
<component
|
||
:is="isExternal(shortcut.path) ? 'a' : 'router-link'"
|
||
v-for="shortcut in shortcuts"
|
||
:key="shortcut.path"
|
||
v-bind="isExternal(shortcut.path) ? { href: shortcut.path, target: '_blank' } : { to: shortcut.path }"
|
||
class="shortcut-item"
|
||
>
|
||
<el-tooltip :content="generateTitle(shortcut.title)" placement="bottom" :show-after="500">
|
||
<svg-icon v-if="shortcut.icon" :icon-class="shortcut.icon" />
|
||
<span :style="{ marginLeft: shortcut.icon ? '5px' : '0' }">{{ generateTitle(shortcut.title) }}</span>
|
||
</el-tooltip>
|
||
</component>
|
||
</div>
|
||
<div class="shortcut-toggle" @click="toggleShortcut">
|
||
<el-icon><ArrowUp v-if="shortcutExpanded" /><ArrowDown v-else /></el-icon>
|
||
</div>
|
||
<scroll-pane ref="scrollPane" class="tags-view-wrapper">
|
||
<router-link
|
||
v-for="tag in visitedViews"
|
||
ref="tagsRef"
|
||
:key="tag.path"
|
||
:class="isActive(tag) ? 'active' : ''"
|
||
:to="{ path: tag.path, query: tag.query }"
|
||
class="tags-view-item"
|
||
@click.middle="!isAffix(tag) && closeSelectedTag(tag)"
|
||
@contextmenu.prevent="openMenu(tag, $event)"
|
||
>
|
||
{{ generateTitle(tag.name || tag.meta?.title) }}
|
||
<span
|
||
v-if="!isAffix(tag)"
|
||
class="close-icon"
|
||
@click.prevent.stop="closeSelectedTag(tag)"
|
||
>x</span>
|
||
</router-link>
|
||
</scroll-pane>
|
||
<ul v-show="visible" :style="{ left: left + 'px', top: top + 'px' }" class="contextmenu">
|
||
<li @click="refreshSelectedTag(selectedTag)">{{ $t('common.refresh') || '刷新' }}</li>
|
||
<li v-if="!isAffix(selectedTag)" @click="closeSelectedTag(selectedTag)">{{ $t('common.Close') || '关闭' }}</li>
|
||
<li @click="closeOthersTags">{{ $t('common.Close Other') || '关闭其他' }}</li>
|
||
<li @click="closeAllTags(selectedTag)">{{ $t('common.Close All') || '全部关闭' }}</li>
|
||
</ul>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, computed, watch, onMounted, nextTick, onBeforeUpdate } from 'vue'
|
||
import { useRoute, useRouter } from 'vue-router'
|
||
import { useTagsViewStore } from '@/stores/modules/tagsView'
|
||
import { usePermissionStore } from '@/stores/modules/permission'
|
||
import { useShortcutsStore } from '@/stores/modules/shortcuts'
|
||
import ScrollPane from './ScrollPane.vue'
|
||
import Navbar from '../Navbar.vue'
|
||
import path from 'path-browserify'
|
||
import { useI18n } from 'vue-i18n'
|
||
import { isExternal } from '@/utils/validate'
|
||
import { ArrowUp, ArrowDown } from '@element-plus/icons-vue'
|
||
import { useSettingsStore } from '@/stores/modules/settings'
|
||
|
||
const { t, te } = useI18n()
|
||
|
||
const visible = ref(false)
|
||
const top = ref(0)
|
||
const left = ref(0)
|
||
const selectedTag = ref<any>({})
|
||
const affixTags = ref<any[]>([])
|
||
const tagsRef = ref<any[]>([])
|
||
|
||
const tagsViewStore = useTagsViewStore()
|
||
const permissionStore = usePermissionStore()
|
||
const shortcutsStore = useShortcutsStore()
|
||
const settingsStore = useSettingsStore()
|
||
const route = useRoute()
|
||
const router = useRouter()
|
||
const scrollPane = ref()
|
||
|
||
const visitedViews = computed(() => tagsViewStore.visitedViews)
|
||
const routes = computed(() => permissionStore.routes)
|
||
const shortcuts = computed(() => shortcutsStore.$state?.shortcuts ?? [])
|
||
const shortcutExpanded = computed(() => settingsStore.shortcutExpanded)
|
||
|
||
const toggleShortcut = () => {
|
||
settingsStore.changeSetting({ key: 'shortcutExpanded', value: !shortcutExpanded.value })
|
||
}
|
||
|
||
onBeforeUpdate(() => {
|
||
tagsRef.value = []
|
||
})
|
||
|
||
const isActive = (tag: any) => {
|
||
return tag.path === route.path
|
||
}
|
||
|
||
const isAffix = (tag: any) => {
|
||
return tag.meta && tag.meta.affix
|
||
}
|
||
|
||
const generateTitle = (title: any) => {
|
||
const titleKey = 'menu.' + title
|
||
const hasKey = te(titleKey)
|
||
if (hasKey) {
|
||
return t(titleKey)
|
||
}
|
||
return title || 'no-name'
|
||
}
|
||
|
||
const filterAffixTags = (routes: any[], basePath = '/') => {
|
||
let tags: any[] = []
|
||
routes.forEach(route => {
|
||
if (route.meta && route.meta.affix) {
|
||
// Simple path resolve
|
||
const tagPath = path.resolve(basePath, route.path)
|
||
tags.push({
|
||
fullPath: tagPath,
|
||
path: tagPath,
|
||
name: route.name,
|
||
meta: { ...route.meta }
|
||
})
|
||
}
|
||
if (route.children) {
|
||
const tempTags = filterAffixTags(route.children, route.path)
|
||
if (tempTags.length >= 1) {
|
||
tags = [...tags, ...tempTags]
|
||
}
|
||
}
|
||
})
|
||
return tags
|
||
}
|
||
|
||
const initTags = () => {
|
||
const affixTagsArr = affixTags.value = filterAffixTags(routes.value)
|
||
for (const tag of affixTagsArr) {
|
||
if (tag.name) {
|
||
tagsViewStore.addVisitedView(tag)
|
||
}
|
||
}
|
||
}
|
||
|
||
const addTags = () => {
|
||
const { name } = route
|
||
if (name) {
|
||
tagsViewStore.addView(route)
|
||
}
|
||
return false
|
||
}
|
||
|
||
const moveToCurrentTag = () => {
|
||
nextTick(() => {
|
||
for (const [i, tag] of visitedViews.value.entries()) {
|
||
if (tag.path === route.path) {
|
||
const tagRef = tagsRef.value[i]
|
||
if (tagRef) {
|
||
// When using router-link with scoped slot or component, ref might be component instance
|
||
// router-link renders an 'a' tag by default, but in Vue 3 it's a component.
|
||
// Accessing $el gives the DOM element.
|
||
scrollPane.value?.moveToTarget(tagRef.$el)
|
||
}
|
||
|
||
if (tag.fullPath !== route.fullPath) {
|
||
tagsViewStore.updateVisitedView(route)
|
||
}
|
||
break
|
||
}
|
||
}
|
||
})
|
||
}
|
||
|
||
const refreshSelectedTag = (view: any) => {
|
||
tagsViewStore.delCachedView(view).then(() => {
|
||
const { fullPath } = view
|
||
nextTick(() => {
|
||
router.replace({
|
||
path: '/redirect' + fullPath
|
||
})
|
||
})
|
||
})
|
||
}
|
||
|
||
const closeSelectedTag = (view: any) => {
|
||
tagsViewStore.delView(view).then(({ visitedViews }: any) => {
|
||
if (isActive(view)) {
|
||
toLastView(visitedViews, view)
|
||
}
|
||
})
|
||
}
|
||
|
||
const closeOthersTags = () => {
|
||
router.push(selectedTag.value)
|
||
tagsViewStore.delOthersViews(selectedTag.value).then(() => {
|
||
moveToCurrentTag()
|
||
})
|
||
}
|
||
|
||
const closeAllTags = (view: any) => {
|
||
tagsViewStore.delAllViews().then(({ visitedViews }: any) => {
|
||
if (affixTags.value.some(tag => tag.path === view.path)) {
|
||
return
|
||
}
|
||
toLastView(visitedViews, view)
|
||
})
|
||
}
|
||
|
||
const toLastView = (visitedViews: any[], view: any) => {
|
||
const latestView = visitedViews.slice(-1)[0]
|
||
if (latestView) {
|
||
router.push(latestView.fullPath)
|
||
} else {
|
||
if (view.name === 'Dashboard') {
|
||
router.replace({ path: '/redirect' + view.fullPath })
|
||
} else {
|
||
router.push('/')
|
||
}
|
||
}
|
||
}
|
||
|
||
const openMenu = (tag: any, e: MouseEvent) => {
|
||
const menuMinWidth = 105
|
||
const offsetLeft = scrollPane.value?.$el.getBoundingClientRect().left // container margin left
|
||
const offsetWidth = scrollPane.value?.$el.offsetWidth // container width
|
||
const maxLeft = offsetWidth - menuMinWidth // left boundary
|
||
const leftPos = e.clientX - offsetLeft + 15 // 15: margin right
|
||
|
||
if (leftPos > maxLeft) {
|
||
left.value = maxLeft
|
||
} else {
|
||
left.value = leftPos
|
||
}
|
||
top.value = e.clientY
|
||
visible.value = true
|
||
selectedTag.value = tag
|
||
}
|
||
|
||
const closeMenu = () => {
|
||
visible.value = false
|
||
}
|
||
|
||
watch(route, () => {
|
||
addTags()
|
||
moveToCurrentTag()
|
||
})
|
||
|
||
watch(visible, (value) => {
|
||
if (value) {
|
||
document.body.addEventListener('click', closeMenu)
|
||
} else {
|
||
document.body.removeEventListener('click', closeMenu)
|
||
}
|
||
})
|
||
|
||
onMounted(() => {
|
||
initTags()
|
||
addTags()
|
||
moveToCurrentTag() // Initial scroll to current tag
|
||
})
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.tags-view-container {
|
||
height: auto;
|
||
width: 100%;
|
||
background: #fff;
|
||
// border-bottom: 1px solid #d8dce5;
|
||
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .12), 0 0 3px 0 rgba(0, 0, 0, .04);
|
||
|
||
.shortcut-toggle {
|
||
height: 7px;
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
background: #d8d7d7;
|
||
border-bottom: 1px solid #f0f2f5;
|
||
cursor: pointer;
|
||
font-size: 14px;
|
||
color: #909399;
|
||
transition: all 0.3s;
|
||
|
||
&:hover {
|
||
background: #f5f7fa;
|
||
color: #409EFF;
|
||
}
|
||
}
|
||
|
||
.tags-view-wrapper {
|
||
height: 35px;
|
||
width: 100%;
|
||
background: #f0f2f5;
|
||
padding-top: 2px;
|
||
// border-bottom: 1px solid #d8dce5;
|
||
|
||
.tags-view-item {
|
||
display: inline-block;
|
||
position: relative;
|
||
cursor: pointer;
|
||
height: 26px;
|
||
line-height: 26px;
|
||
border: 1px solid #d8dce5;
|
||
color: #495060;
|
||
background: #fff;
|
||
padding: 0 12px;
|
||
font-size: 12px;
|
||
margin-left: 6px;
|
||
// margin-top: 5px; // 移除 margin-top,改用 flex 居中
|
||
border-radius: 3px;
|
||
text-decoration: none;
|
||
transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
|
||
|
||
&:first-of-type {
|
||
margin-left: 15px;
|
||
}
|
||
&:last-of-type {
|
||
margin-right: 15px;
|
||
}
|
||
|
||
&:hover {
|
||
background-color: #f6f6f6;
|
||
border-color: #b4bccc;
|
||
}
|
||
|
||
&.active {
|
||
background-color: #42b983;
|
||
color: #fff;
|
||
border-color: #42b983;
|
||
&::before {
|
||
content: "";
|
||
background: #fff;
|
||
display: inline-block;
|
||
width: 8px;
|
||
height: 8px;
|
||
border-radius: 50%;
|
||
position: relative;
|
||
margin-right: 4px;
|
||
}
|
||
}
|
||
.close-icon {
|
||
width: 16px;
|
||
height: 16px;
|
||
line-height: 14px;
|
||
vertical-align: middle;
|
||
// border-radius: 50%;
|
||
text-align: center;
|
||
transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
|
||
transform-origin: 100% 50%;
|
||
// margin-left: 5px;
|
||
font-size: 12px;
|
||
|
||
&:hover {
|
||
background-color: #b4bccc;
|
||
color: #fff;
|
||
}
|
||
}
|
||
|
||
.close-icon {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
width: 16px;
|
||
height: 16px;
|
||
line-height: 16px;
|
||
vertical-align: middle;
|
||
// border-radius: 50%;
|
||
text-align: center;
|
||
transition: all 0.18s ease;
|
||
transform-origin: 100% 50%;
|
||
// margin-left: 5px;
|
||
font-size: 12px;
|
||
cursor: pointer;
|
||
box-sizing: border-box;
|
||
|
||
&:hover {
|
||
background-color: #c7cedb;
|
||
color: #fff;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
.navbar-wrapper {
|
||
min-height: 45px;
|
||
height: auto;
|
||
}
|
||
|
||
|
||
|
||
.contextmenu {
|
||
margin: 0;
|
||
background: #fff;
|
||
z-index: 3000;
|
||
position: absolute;
|
||
list-style-type: none;
|
||
padding: 5px 0;
|
||
border-radius: 4px;
|
||
font-size: 12px;
|
||
font-weight: 400;
|
||
color: #333;
|
||
box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3);
|
||
li {
|
||
margin: 0;
|
||
padding: 7px 16px;
|
||
cursor: pointer;
|
||
&:hover {
|
||
background: #eee;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.shortcut-key {
|
||
height: 30px;
|
||
margin: 0 15px;
|
||
display: flex;
|
||
align-items: center;
|
||
// padding-top: 7px;
|
||
gap: 8px;
|
||
background-color: #ffffff;
|
||
|
||
.shortcut-item {
|
||
min-width: 70px;
|
||
padding: 0px 10px;
|
||
height: 24px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
border-radius: 4px;
|
||
background-color: #dbdada;
|
||
color: #606266;
|
||
font-size: 13px;
|
||
transition: all 0.3s;
|
||
|
||
&:hover {
|
||
background-color: #e6e8eb;
|
||
color: #409EFF;
|
||
}
|
||
|
||
&.router-link-active {
|
||
color: #409EFF;
|
||
background-color: #ecf5ff;
|
||
}
|
||
}
|
||
}
|
||
|
||
</style>
|