Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -1,84 +1,97 @@
|
|||||||
<template>
|
<template>
|
||||||
<el-scrollbar ref="scrollContainer" :vertical="false" class="scroll-container" @wheel.native.prevent="handleScroll">
|
<el-scrollbar
|
||||||
|
ref="scrollContainerRef"
|
||||||
|
:vertical="false"
|
||||||
|
class="scroll-container"
|
||||||
|
@wheel.prevent="handleScroll">
|
||||||
<slot/>
|
<slot/>
|
||||||
</el-scrollbar>
|
</el-scrollbar>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script setup lang="ts">
|
||||||
import {defineComponent, reactive, ref, toRefs, computed, onMounted, onBeforeUnmount, getCurrentInstance} from "vue";
|
import {ref, computed, onMounted, onBeforeUnmount, getCurrentInstance} from "vue";
|
||||||
|
import {tagsViewStoreHook} from "@store/modules/tagsView"
|
||||||
|
|
||||||
export default defineComponent({
|
|
||||||
emits: ['scroll'],
|
|
||||||
setup(_, context) {
|
|
||||||
const scrollContainer = ref(null)
|
|
||||||
const scrollWrapper = computed(() => {
|
|
||||||
return (scrollContainer.value as any).$refs.wrap as HTMLElement
|
|
||||||
})
|
|
||||||
const {ctx} = getCurrentInstance() as any
|
|
||||||
const tagAndTagSpacing = 4
|
|
||||||
|
|
||||||
const state = reactive({
|
const tagAndTagSpacing = ref(4)
|
||||||
handleScroll: (e: WheelEvent) => {
|
const scrollContainerRef = ref(null)
|
||||||
const eventDelta = (e as any).wheelDelta || -e.deltaY * 40
|
|
||||||
scrollWrapper.value.scrollLeft = scrollWrapper.value.scrollLeft + eventDelta / 4
|
|
||||||
},
|
|
||||||
moveToCurrentTag: (currentTag: HTMLElement) => {
|
|
||||||
const container = (scrollContainer.value as any).$el as HTMLElement
|
|
||||||
const containerWidth = container.offsetWidth
|
|
||||||
const tagList = ctx.$parent.$refs.tag as any[]
|
|
||||||
let firstTag = null
|
|
||||||
let lastTag = null
|
|
||||||
|
|
||||||
// find first tag and last tag
|
const emits = defineEmits()
|
||||||
if (tagList.length > 0) {
|
const emitScroll = () => {
|
||||||
firstTag = tagList[0]
|
emits('scroll')
|
||||||
lastTag = tagList[tagList.length - 1]
|
}
|
||||||
|
|
||||||
|
const {ctx} = getCurrentInstance() as any
|
||||||
|
const scrollWrapper = computed(() => {
|
||||||
|
return (scrollContainerRef.value as any).$refs.wrap as HTMLElement
|
||||||
|
})
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
console.log('scrollWrapper', scrollWrapper.value)
|
||||||
|
//scrollWrapper.value.addEventListener('scroll', emitScroll, true);
|
||||||
|
})
|
||||||
|
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
// scrollWrapper.value.removeEventListener('scroll', emitScroll);
|
||||||
|
})
|
||||||
|
|
||||||
|
function handleScroll(e: WheelEvent) {
|
||||||
|
const eventDelta = (e as any).wheelDelta || -e.deltaY * 40
|
||||||
|
scrollWrapper.value.scrollLeft = scrollWrapper.value.scrollLeft + eventDelta / 4
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveToTarget(currentTag) {
|
||||||
|
const $container = ctx.$refs.scrollContainer.$el
|
||||||
|
const $containerWidth = $container.offsetWidth
|
||||||
|
const $scrollWrapper = scrollWrapper.value;
|
||||||
|
|
||||||
|
let firstTag = null
|
||||||
|
let lastTag = null
|
||||||
|
|
||||||
|
// find first tag and last tag
|
||||||
|
if (visitedViews.value.length > 0) {
|
||||||
|
firstTag = visitedViews.value[0]
|
||||||
|
lastTag = visitedViews.value[visitedViews.value.length - 1]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (firstTag === currentTag) {
|
||||||
|
$scrollWrapper.scrollLeft = 0
|
||||||
|
} else if (lastTag === currentTag) {
|
||||||
|
$scrollWrapper.scrollLeft = $scrollWrapper.scrollWidth - $containerWidth
|
||||||
|
} else {
|
||||||
|
const tagListDom = document.getElementsByClassName('tags-view-item');
|
||||||
|
const currentIndex = visitedViews.value.findIndex(item => item === currentTag)
|
||||||
|
let prevTag = null
|
||||||
|
let nextTag = null
|
||||||
|
for (const k in tagListDom) {
|
||||||
|
if (k !== 'length' && Object.hasOwnProperty.call(tagListDom, k)) {
|
||||||
|
if (tagListDom[k].dataset.path === visitedViews.value[currentIndex - 1].path) {
|
||||||
|
prevTag = tagListDom[k];
|
||||||
}
|
}
|
||||||
|
if (tagListDom[k].dataset.path === visitedViews.value[currentIndex + 1].path) {
|
||||||
if (firstTag === currentTag) {
|
nextTag = tagListDom[k];
|
||||||
scrollWrapper.value.scrollLeft = 0
|
|
||||||
} else if (lastTag === currentTag) {
|
|
||||||
scrollWrapper.value.scrollLeft = scrollWrapper.value.scrollWidth - containerWidth
|
|
||||||
} else {
|
|
||||||
// find preTag and nextTag
|
|
||||||
const currentIndex = tagList.findIndex(item => item === currentTag)
|
|
||||||
const prevTag = tagList[currentIndex - 1]
|
|
||||||
const nextTag = tagList[currentIndex + 1]
|
|
||||||
// the tag's offsetLeft after of nextTag
|
|
||||||
const afterNextTagOffsetLeft = nextTag.$el.offsetLeft + nextTag.$el.offsetWidth + tagAndTagSpacing
|
|
||||||
// the tag's offsetLeft before of prevTag
|
|
||||||
const beforePrevTagOffsetLeft = prevTag.$el.offsetLeft - tagAndTagSpacing
|
|
||||||
|
|
||||||
if (afterNextTagOffsetLeft > scrollWrapper.value.scrollLeft + containerWidth) {
|
|
||||||
scrollWrapper.value.scrollLeft = afterNextTagOffsetLeft - containerWidth
|
|
||||||
} else if (beforePrevTagOffsetLeft < scrollWrapper.value.scrollLeft) {
|
|
||||||
scrollWrapper.value.scrollLeft = beforePrevTagOffsetLeft
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
const emitScroll = () => {
|
|
||||||
context.emit('scroll')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
// the tag's offsetLeft after of nextTag
|
||||||
//scrollWrapper.value.addEventListener('scroll', emitScroll, true)
|
const afterNextTagOffsetLeft = nextTag.offsetLeft + nextTag.offsetWidth + tagAndTagSpacing.value
|
||||||
})
|
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
// the tag's offsetLeft before of prevTag
|
||||||
//scrollWrapper.value.removeEventListener('scroll', emitScroll)
|
const beforePrevTagOffsetLeft = prevTag.offsetLeft - tagAndTagSpacing.value
|
||||||
})
|
if (afterNextTagOffsetLeft > $scrollWrapper.scrollLeft + $containerWidth) {
|
||||||
|
$scrollWrapper.scrollLeft = afterNextTagOffsetLeft - $containerWidth
|
||||||
return {
|
} else if (beforePrevTagOffsetLeft < $scrollWrapper.scrollLeft) {
|
||||||
scrollContainer,
|
$scrollWrapper.scrollLeft = beforePrevTagOffsetLeft
|
||||||
...toRefs(state)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
defineExpose({
|
||||||
|
moveToTarget
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
.scroll-container {
|
.scroll-container {
|
||||||
.el-scrollbar__bar {
|
.el-scrollbar__bar {
|
||||||
|
|||||||
@@ -1,40 +1,48 @@
|
|||||||
<template>
|
<template>
|
||||||
<div id="tags-view-container" class="tags-view-container">
|
<div id="tags-view-container" class="tags-view-container">
|
||||||
<scroll-pane ref="scrollPane" class="tags-view-wrapper" @scroll="handleScroll">
|
<scroll-pane ref="scrollPaneRef" class="tags-view-wrapper" @scroll="handleScroll">
|
||||||
<router-link
|
<router-link
|
||||||
v-for="tag in visitedViews"
|
v-for="tag in visitedViews"
|
||||||
ref="tag"
|
|
||||||
:key="tag.path"
|
:key="tag.path"
|
||||||
:class="isActive(tag)?'active':''"
|
:class="isActive(tag)?'active':''"
|
||||||
:to="{ path: tag.path, query: tag.query, fullPath: tag.fullPath }"
|
:to="{ path: tag.path, query: tag.query, fullPath: tag.fullPath }"
|
||||||
tag="span"
|
|
||||||
class="tags-view-item"
|
class="tags-view-item"
|
||||||
@click.middle.native="!isAffix(tag)?closeSelectedTag(tag):''"
|
@click.middle="!isAffix(tag)?closeSelectedTag(tag):''"
|
||||||
@contextmenu.prevent.native="openMenu(tag,$event)"
|
@contextmenu.prevent="openMenu(tag,$event)"
|
||||||
>
|
>
|
||||||
{{ tag.meta.title }}
|
{{ tag.meta.title }}
|
||||||
<span v-if="!isAffix(tag)" @click.prevent.stop="closeSelectedTag(tag)">
|
<span v-if="!isAffix(tag)" class="el-icon-close" @click.prevent.stop="closeSelectedTag(tag)">
|
||||||
<el-icon :size="8">
|
<close class="el-icon-close" style="width: 1em; height: 1em;vertical-align: middle;"/>
|
||||||
<close />
|
|
||||||
</el-icon>
|
|
||||||
</span>
|
</span>
|
||||||
</router-link>
|
</router-link>
|
||||||
</scroll-pane>
|
</scroll-pane>
|
||||||
<ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
|
<ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
|
||||||
<li @click="refreshSelectedTag(selectedTag)">刷新</li>
|
<li @click="refreshSelectedTag(selectedTag)">
|
||||||
<li v-if="!isAffix(selectedTag)" @click="closeSelectedTag(selectedTag)">关闭</li>
|
<refresh-right style="width: 1em; height: 1em;"/> 刷新
|
||||||
<li @click="closeOthersTags">关闭其它</li>
|
</li>
|
||||||
<li @click="closeAllTags(selectedTag)">关闭所有</li>
|
<li v-if="!isAffix(selectedTag)" @click="closeSelectedTag(selectedTag)">
|
||||||
|
<close style="width: 1em; height: 1em;"/>关闭
|
||||||
|
</li>
|
||||||
|
<li @click="closeOthersTags">
|
||||||
|
<circle-close style="width: 1em; height: 1em;"/>关闭其它
|
||||||
|
</li>
|
||||||
|
<li v-if="!isFirstView()" @click="closeLeftTags">
|
||||||
|
<back style="width: 1em; height: 1em;"/>关闭左侧
|
||||||
|
</li>
|
||||||
|
<li v-if="!isLastView()" @click="closeRightTags">
|
||||||
|
<right style="width: 1em; height: 1em;"/>关闭右侧
|
||||||
|
</li>
|
||||||
|
<li @click="closeAllTags(selectedTag)">
|
||||||
|
<circle-close style="width: 1em; height: 1em;"/>关闭所有
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts" setup>
|
||||||
|
|
||||||
import {Close} from '@element-plus/icons'
|
|
||||||
import {tagsViewStoreHook} from '@/store/modules/tagsView'
|
import {tagsViewStoreHook} from '@/store/modules/tagsView'
|
||||||
import {usePermissionStoreHook} from '@/store/modules/Permission'
|
import {usePermissionStoreHook} from '@/store/modules/Permission'
|
||||||
import ScrollPane from './ScrollPane.vue'
|
|
||||||
import path from 'path-browserify'
|
import path from 'path-browserify'
|
||||||
import {
|
import {
|
||||||
defineComponent,
|
defineComponent,
|
||||||
@@ -45,201 +53,218 @@ import {
|
|||||||
reactive,
|
reactive,
|
||||||
ref,
|
ref,
|
||||||
toRefs,
|
toRefs,
|
||||||
watch
|
watch,
|
||||||
|
onMounted
|
||||||
} from "vue";
|
} from "vue";
|
||||||
import {RouteRecordRaw, useRoute, useRouter} from 'vue-router'
|
import {RouteRecordRaw, useRoute, useRouter} from 'vue-router'
|
||||||
import {TagView} from "@store/interface";
|
import {TagView} from "@store/interface";
|
||||||
|
|
||||||
export default defineComponent({
|
import ScrollPane from './ScrollPane.vue'
|
||||||
components: {ScrollPane,Close},
|
import {Close} from '@element-plus/icons'
|
||||||
setup() {
|
|
||||||
const router = useRouter()
|
|
||||||
const instance = getCurrentInstance()
|
|
||||||
const currentRoute = useRoute()
|
|
||||||
const scrollPaneRef = ref(null)
|
|
||||||
const {ctx} = instance as any
|
|
||||||
|
|
||||||
const toLastView=(visitedViews:TagView[],view:TagView)=>{
|
const {ctx} = getCurrentInstance() as any
|
||||||
const latestView = visitedViews.slice(-1)[0]
|
const router = useRouter()
|
||||||
if (latestView && latestView.fullPath) {
|
const route = useRoute();
|
||||||
router.push(latestView.fullPath)
|
|
||||||
} else {
|
|
||||||
if (view.name === 'Dashboard') {
|
|
||||||
router.push({path: '/redirect' + view.fullPath})
|
|
||||||
} else {
|
|
||||||
router.push('/')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const state =reactive({
|
const visitedViews = computed(() => tagsViewStoreHook().visitedViews)
|
||||||
visible: false,
|
const routes = computed(() => usePermissionStoreHook().routes)
|
||||||
top: 0,
|
|
||||||
left: 0,
|
|
||||||
selectedTag: {} as TagView,
|
|
||||||
affixTags: [] as TagView[],
|
|
||||||
isActive: (route:TagView) => {
|
|
||||||
return route.path === currentRoute.path
|
|
||||||
},
|
|
||||||
isAffix: (tag:TagView) => {
|
|
||||||
return tag.meta && tag.meta.affix
|
|
||||||
},
|
|
||||||
refreshSelectedTag: (view: TagView) => {
|
|
||||||
tagsViewStoreHook().delCachedView(view)
|
|
||||||
const { fullPath } = view
|
|
||||||
nextTick(() => {
|
|
||||||
router.replace({ path: '/redirect' + fullPath }).catch(err => {
|
|
||||||
console.warn(err)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
|
||||||
closeSelectedTag: (view: TagView) => {
|
|
||||||
tagsViewStoreHook().delView(view);
|
|
||||||
if (state.isActive(view)) {
|
|
||||||
|
|
||||||
toLastView(tagsViewStoreHook().visitedViews, view)
|
const affixTags = ref([]);
|
||||||
}
|
const visible = ref(false);
|
||||||
},
|
const selectedTag = ref({});
|
||||||
closeOthersTags: () => {
|
const scrollPaneRef = ref(null);
|
||||||
if (state.selectedTag.fullPath !== currentRoute.path && state.selectedTag.fullPath !== undefined) {
|
const left = ref(0);
|
||||||
router.push(state.selectedTag.fullPath)
|
const top = ref(0);
|
||||||
}
|
|
||||||
tagsViewStoreHook().delOthersViews(state.selectedTag as TagView)
|
|
||||||
},
|
|
||||||
closeAllTags: (view: TagView) => {
|
|
||||||
tagsViewStoreHook().delAllViews(undefined);
|
|
||||||
if (state.affixTags.some(tag => tag.path === currentRoute.path)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
toLastView(tagsViewStoreHook().visitedViews, view)
|
|
||||||
},
|
|
||||||
openMenu: (tag: TagView, e: MouseEvent) => {
|
|
||||||
const menuMinWidth = 105
|
|
||||||
const offsetLeft = ctx.$el.getBoundingClientRect().left // container margin left
|
|
||||||
const offsetWidth = ctx.$el.offsetWidth // container width
|
|
||||||
const maxLeft = offsetWidth - menuMinWidth // left boundary
|
|
||||||
const left = e.clientX - offsetLeft + 15 // 15: margin right
|
|
||||||
if (left > maxLeft) {
|
|
||||||
state.left = maxLeft
|
|
||||||
} else {
|
|
||||||
state.left = left
|
|
||||||
}
|
|
||||||
state.top = e.clientY
|
|
||||||
state.visible = true
|
|
||||||
state.selectedTag = tag
|
|
||||||
},
|
|
||||||
closeMenu: () => {
|
|
||||||
state.visible = false
|
|
||||||
},
|
|
||||||
handleScroll: () => {
|
|
||||||
state.closeMenu()
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
watch(route, () => {
|
||||||
|
addTags()
|
||||||
|
moveToCurrentTag()
|
||||||
|
})
|
||||||
|
|
||||||
const visitedViews = computed(() => {
|
watch(visible, (value) => {
|
||||||
return tagsViewStoreHook().visitedViews
|
if (value) {
|
||||||
})
|
document.body.addEventListener('click', closeMenu)
|
||||||
const routes = computed(() =>usePermissionStoreHook().routes)
|
} else {
|
||||||
|
document.body.removeEventListener('click', closeMenu)
|
||||||
const filterAffixTags = (routes: RouteRecordRaw[], basePath = '/') => {
|
|
||||||
let tags: TagView[] = []
|
|
||||||
|
|
||||||
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 childTags = filterAffixTags(route.children, route.path)
|
|
||||||
if (childTags.length >= 1) {
|
|
||||||
tags = tags.concat(childTags)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return tags
|
|
||||||
}
|
|
||||||
|
|
||||||
const initTags = () => {
|
|
||||||
state.affixTags = filterAffixTags(routes.value)
|
|
||||||
for (const tag of state.affixTags) {
|
|
||||||
// Must have tag name
|
|
||||||
if (tag.name) {
|
|
||||||
tagsViewStoreHook().addVisitedView(tag as TagView)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const addTags = () => {
|
|
||||||
if (currentRoute.name) {
|
|
||||||
tagsViewStoreHook().addView(currentRoute)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const moveToCurrentTag = () => {
|
|
||||||
const tags = instance?.refs.tag as any[]
|
|
||||||
nextTick(() => {
|
|
||||||
if (tags === null || tags === undefined || !Array.isArray(tags)) { return }
|
|
||||||
for (const tag of tags) {
|
|
||||||
if ((tag.to as TagView).path === currentRoute.path) {
|
|
||||||
(scrollPaneRef.value as any).moveToCurrentTag(tag)
|
|
||||||
// When query is different then update
|
|
||||||
if ((tag.to as TagView).fullPath !== currentRoute.fullPath) {
|
|
||||||
tagsViewStoreHook().updateVisitedView(currentRoute)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(() => currentRoute.name, () => {
|
|
||||||
if (currentRoute.name !== 'Login') {
|
|
||||||
addTags()
|
|
||||||
moveToCurrentTag()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
watch(() => state.visible, (value) => {
|
|
||||||
if (value) {
|
|
||||||
document.body.addEventListener('click', state.closeMenu)
|
|
||||||
} else {
|
|
||||||
document.body.removeEventListener('click', state.closeMenu)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// life cricle
|
|
||||||
onBeforeMount(() => {
|
|
||||||
initTags()
|
|
||||||
addTags()
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
|
||||||
visitedViews,
|
|
||||||
routes,
|
|
||||||
scrollPaneRef,
|
|
||||||
...toRefs(state)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function filterAffixTags(routes: RouteRecordRaw[], basePath = '/') {
|
||||||
|
let tags: TagView[] = []
|
||||||
|
|
||||||
|
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 childTags = filterAffixTags(route.children, route.path)
|
||||||
|
if (childTags.length >= 1) {
|
||||||
|
tags = tags.concat(childTags)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return tags
|
||||||
|
}
|
||||||
|
|
||||||
|
function initTags() {
|
||||||
|
const res = filterAffixTags(routes.value)
|
||||||
|
affixTags.value = res
|
||||||
|
for (const tag of res) {
|
||||||
|
// Must have tag name
|
||||||
|
if (tag.name) {
|
||||||
|
tagsViewStoreHook().addVisitedView(tag as TagView)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function addTags() {
|
||||||
|
if (route.name) {
|
||||||
|
tagsViewStoreHook().addView(route)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveToCurrentTag() {
|
||||||
|
const tags = getCurrentInstance()?.refs.tag as any[]
|
||||||
|
nextTick(() => {
|
||||||
|
if (tags === null || tags === undefined || !Array.isArray(tags)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for (const tag of tags) {
|
||||||
|
if ((tag.to as TagView).path === route.path) {
|
||||||
|
(scrollPaneRef.value as any).value.moveToTarget(tag)
|
||||||
|
// when query is different then update
|
||||||
|
if ((tag.to as TagView).fullPath !== route.fullPath) {
|
||||||
|
tagsViewStoreHook().updateVisitedView(route)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function isActive(tag: TagView) {
|
||||||
|
return tag.path === route.path
|
||||||
|
}
|
||||||
|
|
||||||
|
function isAffix(tag: TagView) {
|
||||||
|
return tag.meta && tag.meta.affix
|
||||||
|
}
|
||||||
|
|
||||||
|
function isFirstView() {
|
||||||
|
try {
|
||||||
|
return selectedTag.value.fullPath === visitedViews.value[1].fullPath || selectedTag.value.fullPath === '/index'
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isLastView() {
|
||||||
|
try {
|
||||||
|
return selectedTag.value.fullPath === visitedViews.value[visitedViews.value.length - 1].fullPath
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function refreshSelectedTag(view: TagView) {
|
||||||
|
tagsViewStoreHook().delCachedView(view)
|
||||||
|
const {fullPath} = view
|
||||||
|
nextTick(() => {
|
||||||
|
|
||||||
|
console.log('fullPath',fullPath)
|
||||||
|
router.replace({path: '/redirect' + fullPath}).catch(err => {
|
||||||
|
console.warn(err)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function toLastView(visitedViews: TagView[], view: TagView) {
|
||||||
|
const latestView = visitedViews.slice(-1)[0]
|
||||||
|
if (latestView && latestView.fullPath) {
|
||||||
|
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 closeSelectedTag(view: TagView) {
|
||||||
|
tagsViewStoreHook().delView(view).then(({visitedViews}) => {
|
||||||
|
if (isActive(view)) {
|
||||||
|
toLastView(visitedViews, view)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeLeftTags() {
|
||||||
|
ctx.$tab.closeLeftPage(selectedTag.value).then(visitedViews => {
|
||||||
|
if (!visitedViews.find(i => i.fullPath === route.fullPath)) {
|
||||||
|
toLastView(visitedViews)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeRightTags() {
|
||||||
|
ctx.$tab.closeRightPage(selectedTag.value).then(visitedViews => {
|
||||||
|
if (!visitedViews.find(i => i.fullPath === route.fullPath)) {
|
||||||
|
toLastView(visitedViews)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function openMenu(tag: TagView, e: MouseEvent) {
|
||||||
|
const menuMinWidth = 105
|
||||||
|
const offsetLeft = ctx.$el.getBoundingClientRect().left // container margin left
|
||||||
|
const offsetWidth = ctx.$el.offsetWidth // container width
|
||||||
|
const maxLeft = offsetWidth - menuMinWidth // left boundary
|
||||||
|
const l = e.clientX - offsetLeft + 15 // 15: margin right
|
||||||
|
|
||||||
|
if (l > maxLeft) {
|
||||||
|
left.value = maxLeft
|
||||||
|
} else {
|
||||||
|
left.value = l
|
||||||
|
}
|
||||||
|
|
||||||
|
top.value = e.clientY
|
||||||
|
visible.value = true
|
||||||
|
selectedTag.value = tag
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeMenu() {
|
||||||
|
visible.value = false
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleScroll() {
|
||||||
|
closeMenu()
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
initTags()
|
||||||
|
addTags()
|
||||||
|
})
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang='scss' scoped>
|
||||||
.tags-view-container {
|
.tags-view-container {
|
||||||
height: 34px;
|
height: 34px;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
border-bottom: 1px solid #d8dce5;
|
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);
|
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 0 3px 0 rgba(0, 0, 0, 0.04);
|
||||||
|
|
||||||
.tags-view-wrapper {
|
.tags-view-wrapper {
|
||||||
.tags-view-item {
|
.tags-view-item {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
@@ -254,22 +279,18 @@ export default defineComponent({
|
|||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
margin-left: 5px;
|
margin-left: 5px;
|
||||||
margin-top: 4px;
|
margin-top: 4px;
|
||||||
|
|
||||||
&:first-of-type {
|
&:first-of-type {
|
||||||
margin-left: 15px;
|
margin-left: 15px;
|
||||||
}
|
}
|
||||||
|
|
||||||
&:last-of-type {
|
&:last-of-type {
|
||||||
margin-right: 15px;
|
margin-right: 15px;
|
||||||
}
|
}
|
||||||
|
|
||||||
&.active {
|
&.active {
|
||||||
background-color: #42b983;
|
background-color: #42b983;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
border-color: #42b983;
|
border-color: #42b983;
|
||||||
|
|
||||||
&::before {
|
&::before {
|
||||||
content: '';
|
content: "";
|
||||||
background: #fff;
|
background: #fff;
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
width: 8px;
|
width: 8px;
|
||||||
@@ -281,7 +302,6 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.contextmenu {
|
.contextmenu {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
@@ -293,13 +313,11 @@ export default defineComponent({
|
|||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
font-weight: 400;
|
font-weight: 400;
|
||||||
color: #333;
|
color: #333;
|
||||||
box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3);
|
box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
|
||||||
|
|
||||||
li {
|
li {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 7px 16px;
|
padding: 7px 16px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background: #eee;
|
background: #eee;
|
||||||
}
|
}
|
||||||
@@ -315,23 +333,23 @@ export default defineComponent({
|
|||||||
.el-icon-close {
|
.el-icon-close {
|
||||||
width: 16px;
|
width: 16px;
|
||||||
height: 16px;
|
height: 16px;
|
||||||
vertical-align: 2px;
|
vertical-align: 0px;
|
||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
transition: all .3s cubic-bezier(.645, .045, .355, 1);
|
transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
|
||||||
transform-origin: 100% 50%;
|
transform-origin: 100% 50%;
|
||||||
|
|
||||||
&:before {
|
&:before {
|
||||||
transform: scale(.6);
|
transform: scale(0.6);
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
vertical-align: -3px;
|
vertical-align: -3px;
|
||||||
}
|
}
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background-color: #b4bccc;
|
background-color: #b4bccc;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
|
width: 12px !important;
|
||||||
|
height: 12px !important;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
@@ -1,16 +1,15 @@
|
|||||||
|
import {defineStore} from "pinia";
|
||||||
import { defineStore } from "pinia";
|
import {store} from "@/store";
|
||||||
import { store } from "@/store";
|
|
||||||
import {TagsViewState} from "@store/interface";
|
import {TagsViewState} from "@store/interface";
|
||||||
|
|
||||||
const tagsViewStore=defineStore({
|
const tagsViewStore = defineStore({
|
||||||
id:"tagsView",
|
id: "tagsView",
|
||||||
state:():TagsViewState=>( {
|
state: (): TagsViewState => ({
|
||||||
visitedViews: [],
|
visitedViews: [],
|
||||||
cachedViews: []
|
cachedViews: []
|
||||||
}),
|
}),
|
||||||
actions: {
|
actions: {
|
||||||
addVisitedView ( view:any) {
|
addVisitedView(view: any) {
|
||||||
if (this.visitedViews.some(v => v.path === view.path)) return
|
if (this.visitedViews.some(v => v.path === view.path)) return
|
||||||
this.visitedViews.push(
|
this.visitedViews.push(
|
||||||
Object.assign({}, view, {
|
Object.assign({}, view, {
|
||||||
@@ -18,58 +17,58 @@ const tagsViewStore=defineStore({
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
addCachedView(view:any) {
|
addCachedView(view: any) {
|
||||||
if (this.cachedViews.includes(view.name)) return
|
if (this.cachedViews.includes(view.name)) return
|
||||||
if (!view.meta.noCache) {
|
if (!view.meta.noCache) {
|
||||||
this.cachedViews.push(view.name)
|
this.cachedViews.push(view.name)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
delVisitedView( view:any) {
|
delVisitedView(view: any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
for (const [i, v] of this.visitedViews.entries()) {
|
for (const [i, v] of this.visitedViews.entries()) {
|
||||||
if (v.path === view.path) {
|
if (v.path === view.path) {
|
||||||
this.visitedViews.splice(i, 1)
|
this.visitedViews.splice(i, 1)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resolve([...this.visitedViews])
|
resolve([...this.visitedViews])
|
||||||
})
|
})
|
||||||
|
|
||||||
},
|
},
|
||||||
delCachedView ( view:any) {
|
delCachedView(view: any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const index = this.cachedViews.indexOf(view.name)
|
const index = this.cachedViews.indexOf(view.name)
|
||||||
index > -1 && this.cachedViews.splice(index, 1)
|
index > -1 && this.cachedViews.splice(index, 1)
|
||||||
resolve([...this.cachedViews])
|
resolve([...this.cachedViews])
|
||||||
})
|
})
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
delOthersVisitedViews (view:any) {
|
delOthersVisitedViews(view: any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
this.visitedViews = this.visitedViews.filter(v => {
|
this.visitedViews = this.visitedViews.filter(v => {
|
||||||
return v.meta?.affix || v.path === view.path
|
return v.meta?.affix || v.path === view.path
|
||||||
})
|
})
|
||||||
resolve([...this.visitedViews])
|
resolve([...this.visitedViews])
|
||||||
})
|
})
|
||||||
|
|
||||||
},
|
},
|
||||||
delOthersCachedViews( view:any) {
|
delOthersCachedViews(view: any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const index = this.cachedViews.indexOf(view.name)
|
const index = this.cachedViews.indexOf(view.name)
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
this.cachedViews = this.cachedViews.slice(index, index + 1)
|
this.cachedViews = this.cachedViews.slice(index, index + 1)
|
||||||
} else {
|
} else {
|
||||||
// if index = -1, there is no cached tags
|
// if index = -1, there is no cached tags
|
||||||
this.cachedViews = []
|
this.cachedViews = []
|
||||||
}
|
}
|
||||||
resolve([...this.cachedViews])
|
resolve([...this.cachedViews])
|
||||||
})
|
})
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
updateVisitedView (view:any) {
|
updateVisitedView(view: any) {
|
||||||
for (let v of this.visitedViews) {
|
for (let v of this.visitedViews) {
|
||||||
if (v.path === view.path) {
|
if (v.path === view.path) {
|
||||||
v = Object.assign(v, view)
|
v = Object.assign(v, view)
|
||||||
@@ -77,31 +76,42 @@ const tagsViewStore=defineStore({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
addView( view:any) {
|
addView(view: any) {
|
||||||
this.addVisitedView( view)
|
this.addVisitedView(view)
|
||||||
this.addCachedView(view)
|
this.addCachedView(view)
|
||||||
},
|
},
|
||||||
delView( view:any) {
|
delView(view: any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
this.delVisitedView(view)
|
this.delVisitedView(view)
|
||||||
this.delCachedView( view)
|
this.delCachedView(view)
|
||||||
resolve({
|
resolve({
|
||||||
visitedViews: [...this.visitedViews],
|
visitedViews: [...this.visitedViews],
|
||||||
cachedViews: [...this.cachedViews]
|
cachedViews: [...this.cachedViews]
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
delOthersViews( view:any) {
|
delOthersViews(view: any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
this.delOthersVisitedViews(view)
|
this.delOthersVisitedViews(view)
|
||||||
this.delOthersCachedViews(view)
|
this.delOthersCachedViews(view)
|
||||||
resolve({
|
resolve({
|
||||||
visitedViews: [...this.visitedViews],
|
visitedViews: [...this.visitedViews],
|
||||||
cachedViews: [...this.cachedViews]
|
cachedViews: [...this.cachedViews]
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
delAllViews( view:any) {
|
delLeftViews(view: any) {
|
||||||
|
const currIndex = this.visitedViews.findIndex(v => v.path === view.path)
|
||||||
|
if (currIndex === -1) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
this.visitedViews = this.visitedViews.filter((item, index) => {
|
||||||
|
if(index>=currIndex||(item.meta&&item.meta.affix)){
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
delAllViews(view: any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const affixTags = this.visitedViews.filter(tag => tag.meta?.affix)
|
const affixTags = this.visitedViews.filter(tag => tag.meta?.affix)
|
||||||
this.visitedViews = affixTags
|
this.visitedViews = affixTags
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
<script lang="ts">
|
<template>
|
||||||
import {defineComponent} from "vue";
|
<div/>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
import {useRoute, useRouter} from "vue-router";
|
import {useRoute, useRouter} from "vue-router";
|
||||||
|
|
||||||
export default defineComponent({
|
const route = useRoute();
|
||||||
created() {
|
const router = useRouter();
|
||||||
const {params, query} = useRoute()
|
|
||||||
const {path} = params
|
const { params, query } = route
|
||||||
useRouter().replace({path: '/' + path, query})
|
const { path } = params
|
||||||
},
|
|
||||||
render() {
|
router.replace({ path: '/' + path, query })
|
||||||
}
|
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user