<template>
|
<div id="tags-view-container" class="tags-view-container">
|
<scroll-pane
|
ref="scrollPane"
|
class="tags-view-wrapper"
|
@scroll="handleScroll"
|
>
|
<tag-item
|
v-for="tag in visitedViews"
|
ref="tag"
|
:key="tag.path"
|
:class="isActive(tag) ? 'active' : ''"
|
:to="{
|
...tag,
|
}"
|
tag="span"
|
class="tags-view-item"
|
@click.native.prevent="toCurrentTag(tag)"
|
@click.middle.native="closeSelectedTag(tag)"
|
@contextmenu.prevent.native="openMenu(tag, $event)"
|
>
|
{{ tag.title }}
|
<span
|
v-if="!isOnlyOne"
|
class="el-icon-close"
|
@click.prevent.stop="closeSelectedTag(tag)"
|
/>
|
</tag-item>
|
</scroll-pane>
|
<ul
|
v-show="visible"
|
:style="{ left: left + 'px', top: top + 'px' }"
|
class="contextmenu"
|
>
|
<li
|
:class="{ 'is-disabled': isOnlyOne }"
|
@click="closeSelectedTag(selectedTag)"
|
>
|
关闭
|
</li>
|
<li :class="{ 'is-disabled': isOnlyOne }" @click="closeOthersTags">
|
关闭其他标签页
|
</li>
|
<li
|
:class="{ 'is-disabled': isLastView }"
|
@click="closeRight(selectedTag)"
|
>
|
关闭右侧标签页
|
</li>
|
</ul>
|
</div>
|
</template>
|
|
<script>
|
import { mapState } from 'vuex'
|
import ScrollPane from './scroll-pane'
|
import TagItem from './tag-item'
|
|
export default {
|
components: { ScrollPane, TagItem },
|
data() {
|
return {
|
visible: false,
|
top: 0,
|
left: 0,
|
selectedTag: {},
|
}
|
},
|
computed: {
|
...mapState({
|
visitedViews: (state) => state.tagsView.visitedViews,
|
}),
|
isLastView() {
|
const latestView = this.visitedViews.slice(-1)[0]
|
if (latestView?.name && latestView.name === this.selectedTag.name) {
|
return true
|
}
|
return false
|
},
|
isOnlyOne() {
|
return this.visitedViews.length <= 1
|
},
|
},
|
watch: {
|
$route() {
|
const timer = setTimeout(() => {
|
this.addTags()
|
this.moveToCurrentTag()
|
clearTimeout(timer)
|
}, 500)
|
},
|
visible(value) {
|
if (value) {
|
document.body.addEventListener('click', this.closeMenu)
|
} else {
|
document.body.removeEventListener('click', this.closeMenu)
|
}
|
},
|
},
|
mounted() {
|
this.addTags()
|
},
|
methods: {
|
isActive(route) {
|
return route.name === this.$route.name
|
},
|
toCurrentTag(tag) {
|
if (!this.isActive(tag)) {
|
this.$router.push(tag.fullPath)
|
}
|
},
|
addTags() {
|
const { name } = this.$route
|
if (name) {
|
this.$store.dispatch('tagsView/addVisitedView', this.$route)
|
}
|
return false
|
},
|
moveToCurrentTag() {
|
const tags = this.$refs.tag
|
if (tags) {
|
this.$nextTick(() => {
|
for (const tag of tags) {
|
if (tag.to.name === this.$route.name) {
|
this.$refs.scrollPane.moveToTarget(tag, this.visitedViews)
|
break
|
}
|
}
|
})
|
}
|
},
|
closeSelectedTag(view) {
|
if (!this.isOnlyOne) {
|
this.$store
|
.dispatch('tagsView/delVisitedView', view)
|
.then((visitedViews) => {
|
if (this.isActive(view)) {
|
this.toLastView(visitedViews)
|
// this.moveToCurrentTag()
|
}
|
})
|
}
|
},
|
closeOthersTags() {
|
if (!this.isOnlyOne) {
|
this.$router.push(this.selectedTag)
|
this.$store
|
.dispatch('tagsView/delOthersVisitedViews', this.selectedTag)
|
.then(() => {
|
// this.moveToCurrentTag()
|
})
|
}
|
},
|
closeRight() {
|
if (!this.isLastView) {
|
this.$store
|
.dispatch('tagsView/delRightVisitedViews', this.selectedTag)
|
.then((visitedViews) => {
|
if (!visitedViews.find((item) => item.name === this.$route.name)) {
|
if (
|
this.$route.matched.slice(-1)[0]?.instances?.default?.$metaInfo
|
?.title
|
) {
|
this.toLastView(visitedViews)
|
}
|
}
|
})
|
}
|
},
|
toLastView(visitedViews) {
|
const latestView = visitedViews.slice(-1)[0]
|
if (latestView) {
|
this.$router.push(latestView.fullPath)
|
}
|
},
|
openMenu(tag, e) {
|
const menuMinWidth = 105
|
const offsetLeft = this.$el.getBoundingClientRect().left // container margin left
|
const offsetWidth = this.$el.offsetWidth // container width
|
const maxLeft = offsetWidth - menuMinWidth // left boundary
|
const left = e.clientX - offsetLeft + 15 // 15: margin right
|
|
if (left > maxLeft) {
|
this.left = maxLeft
|
} else {
|
this.left = left
|
}
|
|
this.top = e.clientY
|
this.visible = true
|
this.selectedTag = tag
|
},
|
closeMenu() {
|
this.visible = false
|
},
|
handleScroll() {
|
this.closeMenu()
|
},
|
},
|
}
|
</script>
|
|
<style lang="scss" 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, 0.12), 0 0 3px 0 rgba(0, 0, 0, 0.04);
|
.tags-view-wrapper {
|
.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 8px;
|
font-size: 12px;
|
margin-left: 5px;
|
margin-top: 4px;
|
&:first-of-type {
|
margin-left: 15px;
|
}
|
&:last-of-type {
|
margin-right: 15px;
|
}
|
&.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;
|
}
|
}
|
}
|
}
|
.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, 0.3);
|
li {
|
margin: 0;
|
padding: 7px 16px;
|
cursor: pointer;
|
&.is-disabled {
|
color: #999;
|
cursor: not-allowed;
|
}
|
&:not(.is-disabled):hover {
|
background: #eee;
|
}
|
}
|
}
|
}
|
</style>
|
|
<style lang="scss">
|
//reset element css of el-icon-close
|
.tags-view-wrapper {
|
.tags-view-item {
|
.el-icon-close {
|
width: 16px;
|
height: 16px;
|
vertical-align: 2px;
|
border-radius: 50%;
|
text-align: center;
|
transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
|
transform-origin: 100% 50%;
|
&:before {
|
transform: scale(0.6);
|
display: inline-block;
|
vertical-align: -3px;
|
}
|
&:hover {
|
background-color: #b4bccc;
|
color: #fff;
|
}
|
}
|
}
|
}
|
</style>
|