<template>
|
<div id="tags-view-container" class="tags-view-container">
|
<scroll-pane ref="scrollPane" class="tags-view-wrapper" @scroll="handleScroll">
|
<div class="hamburger-container" @click="toggleSidebar">
|
<svg-icon icon-class="drag"/>
|
</div>
|
<router-link v-for="tag in visitedViews" :key="tag.path"
|
:to="{ path: tag.path, query: tag.query, fullPath: tag.fullPath }" custom
|
v-slot="{ navigate, isActive, isExactActive }" ref="tag">
|
<span :class="['tags-view-item', isActive && 'router-link-active', isExactActive && 'router-link-exact-active', tag.meta.icon && 'hide-dot']"
|
@click="navigate"
|
@click.middle="!isAffix(tag) ? closeSelectedTag(tag) : ''"
|
@contextmenu.prevent="openMenu(tag, $event)">
|
<svg-icon v-if="tag.meta.icon" :icon-class="tag.meta.icon" />
|
{{ tag.title }}
|
<icon-close v-if="!isAffix(tag)" class="el-icon-close" @click.prevent.stop="closeSelectedTag(tag)" />
|
</span>
|
</router-link>
|
</scroll-pane>
|
<ul v-show="visible" :style="{ left: left + 'px', top: top + 'px' }" class="contextmenu">
|
<li @click="refreshSelectedTag(selectedTag)">刷新</li>
|
<li v-if="!isAffix(selectedTag)" @click="closeSelectedTag(selectedTag)">关闭</li>
|
<li @click="closeOthersTags">关闭其它</li>
|
<li @click="closeAllTags(selectedTag)">关闭全部</li>
|
</ul>
|
</div>
|
</template>
|
|
<script setup name="TagsView">
|
import { defineComponent } from 'vue';
|
import ScrollPane from './ScrollPane';
|
import path from 'path-browserify';
|
import useAppStore from '@/store/app';
|
import useTagsViewStore from '@/store/tagsView';
|
import usePromissionStore from '@/store/permission';
|
import { storeToRefs } from 'pinia';
|
import { Close as IconClose } from '@element-plus/icons-vue';
|
import SvgIcon from '@/components/SvgIcon/index.vue';
|
import { ref, watch, onMounted, nextTick } from 'vue';
|
import { useRoute, useRouter } from 'vue-router';
|
const route = useRoute();
|
const router = useRouter();
|
|
const appStore = useAppStore();
|
const tagsViewStore = useTagsViewStore();
|
const permissionStore = usePromissionStore();
|
const { sidebar } = storeToRefs(appStore);
|
const { visitedViews } = storeToRefs(tagsViewStore);
|
const { routes } = storeToRefs(permissionStore);
|
const { toggleSidebar } = appStore;
|
const { addVisitedView, addView, updateVisitedView, delCachedView, delView, delOthersViews, delAllViews, } = tagsViewStore;
|
|
const visible = ref(false);
|
const top = ref(0);
|
const left = ref(0);
|
const selectedTag = ref({});
|
const affixTags = ref([]);
|
|
const scrollPane = ref(null);
|
const tag = ref(null);
|
|
watch(
|
() => route.path,
|
() => {
|
addTags();
|
moveToCurrentTag();
|
},
|
{
|
immediate: true
|
}
|
);
|
|
watch(
|
() => visible.value,
|
value => {
|
if (value) {
|
document.body.addEventListener('click', closeMenu);
|
} else {
|
document.body.removeEventListener('click', closeMenu);
|
}
|
}
|
);
|
|
|
onMounted(() => {
|
initTags();
|
addTags();
|
});
|
|
function isCurrentRoute(_route) {
|
return route.path === _route.path;
|
}
|
|
function isAffix(tag) {
|
return tag.meta && tag.meta.affix;
|
}
|
|
function filterAffixTags(_routes, basePath = '/') {
|
let tags = [];
|
_routes.forEach(route => {
|
if (route.meta && route.meta.affix) {
|
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;
|
}
|
|
function initTags() {
|
affixTags.value = filterAffixTags(routes.value);
|
for (const tag of affixTags.value) {
|
// Must have tag name
|
if (tag.name) {
|
addVisitedView(tag);
|
}
|
}
|
}
|
|
function addTags() {
|
const { name } = route;
|
if (name) {
|
addView(route);
|
}
|
return false;
|
}
|
|
function moveToCurrentTag() {
|
const tags = tag.value;
|
nextTick(() => {
|
for (const tag of tags) {
|
if (tag.to.path === route.path) {
|
scrollPane.value.moveToTarget(tag);
|
// when query is different then update
|
if (tag.to.fullPath !== route.fullPath) {
|
updateVisitedView(route);
|
}
|
break;
|
}
|
}
|
});
|
}
|
|
function refreshSelectedTag(view) {
|
delCachedView(view);
|
const { fullPath } = view;
|
nextTick(() => {
|
router.replace({
|
path: '/redirect' + fullPath
|
});
|
})
|
}
|
|
function closeSelectedTag(view) {
|
delView(view);
|
if (isActive(view)) {
|
toLastView(visitedViews.value, view);
|
}
|
}
|
|
function closeOthersTags() {
|
router.push(selectedTag.value);
|
delOthersViews(selectedTag.value);
|
moveToCurrentTag();
|
}
|
|
function closeAllTags(view) {
|
delAllViews();
|
if (affixTags.value.some(tag => tag.path === view.path)) {
|
return;
|
}
|
toLastView(visitedViews.value, view);
|
}
|
|
function toLastView(_visitedViews, view) {
|
const latestView = _visitedViews.slice(-1)[0];
|
if (latestView) {
|
router.push(latestView.fullPath);
|
} else {
|
// now the default is to redirect to the home page if there is no tags-view,
|
// you can adjust it according to your needs.
|
if (view.name === 'Dashboard') {
|
// to reload home page
|
router.replace({ path: '/redirect' + view.fullPath });
|
} else {
|
router.push('/');
|
}
|
}
|
}
|
|
function openMenu(tag, e) {
|
const menuMinWidth = 105;
|
const offsetLeft = tag.getBoundingClientRect().left; // container margin left
|
const offsetWidth = tag.offsetWidth; // container width
|
const maxLeft = offsetWidth - menuMinWidth; // left boundary
|
const _left = e.clientX - offsetLeft + 15; // 15: margin right
|
|
if (left > maxLeft) {
|
left.value = maxLeft;
|
} else {
|
left.value = _left;
|
}
|
|
top.value = e.clientY;
|
visible.value = true;
|
selectedTag.value = tag;
|
}
|
|
function closeMenu() {
|
visible.value = false;
|
}
|
|
function handleScroll() {
|
closeMenu();
|
}
|
</script>
|
|
<style lang="less" scoped>
|
.tags-view-container {
|
height: 34px;
|
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);
|
.hamburger-container {
|
display: inline-block;
|
height: 32px;
|
line-height: 32px;
|
padding: 0 16px;
|
border-left: 1px solid var(--light-color);
|
border-right: 1px solid var(--light-color);
|
}
|
.tags-view-wrapper {
|
.tags-view-item {
|
user-select: none;
|
display: inline-block;
|
position: relative;
|
cursor: pointer;
|
height: 26px;
|
line-height: 26px;
|
border: 1px solid #d8dce5;
|
color: #495060;
|
background: #fff;
|
padding: 0 8px;
|
font-size: 12px;
|
margin-left: 5px;
|
margin-top: 4px;
|
|
&:first-of-type {
|
margin-left: 15px;
|
}
|
|
&:last-of-type {
|
margin-right: 15px;
|
}
|
|
&.router-link-exact-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: 2px;
|
}
|
&.hide-dot {
|
&::before {
|
width: 0;
|
height: 0;
|
}
|
}
|
}
|
}
|
}
|
|
.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;
|
}
|
}
|
}
|
}
|
</style>
|
|
<style lang="less">
|
//reset element css of el-icon-close
|
.tags-view-wrapper {
|
.tags-view-item {
|
.link {
|
padding: 0 2px;
|
}
|
.el-icon-close {
|
width: 16px;
|
height: 16px;
|
padding: 4px;
|
margin-bottom: -4px;
|
border-radius: 50%;
|
text-align: center;
|
transition: all .3s cubic-bezier(.645, .045, .355, 1);
|
transform-origin: 100% 50%;
|
|
&:before {
|
transform: scale(.6);
|
display: inline-block;
|
vertical-align: -3px;
|
}
|
|
&:hover {
|
background-color: #b4bccc;
|
color: #fff;
|
}
|
}
|
}
|
}
|
</style>
|