Skip to content

Commit 0410756

Browse files
committed
fearure[TagsView]: add affix option (#1577)
1 parent d955fe8 commit 0410756

File tree

3 files changed

+69
-25
lines changed

3 files changed

+69
-25
lines changed

src/router/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import nestedRouter from './modules/nested'
2929
icon: 'svg-name' the icon show in the sidebar
3030
noCache: true if true, the page will no be cached(default is false)
3131
breadcrumb: false if false, the item will hidden in breadcrumb(default is true)
32+
affix: true if true, the tag will affix in the tags-view
3233
}
3334
**/
3435
export const constantRouterMap = [
@@ -72,7 +73,7 @@ export const constantRouterMap = [
7273
path: 'dashboard',
7374
component: () => import('@/views/dashboard/index'),
7475
name: 'Dashboard',
75-
meta: { title: 'dashboard', icon: 'dashboard', noCache: true }
76+
meta: { title: 'dashboard', icon: 'dashboard', noCache: true, affix: true }
7677
}
7778
]
7879
},
@@ -85,7 +86,7 @@ export const constantRouterMap = [
8586
path: 'index',
8687
component: () => import('@/views/documentation/index'),
8788
name: 'Documentation',
88-
meta: { title: 'documentation', icon: 'documentation', noCache: true }
89+
meta: { title: 'documentation', icon: 'documentation', affix: true }
8990
}
9091
]
9192
},

src/store/modules/tagsView.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,9 @@ const tagsView = {
3838
},
3939

4040
DEL_OTHERS_VISITED_VIEWS: (state, view) => {
41-
for (const [i, v] of state.visitedViews.entries()) {
42-
if (v.path === view.path) {
43-
state.visitedViews = state.visitedViews.slice(i, i + 1)
44-
break
45-
}
46-
}
41+
state.visitedViews = state.visitedViews.filter(v => {
42+
return v.meta.affix || v.path === view.path
43+
})
4744
},
4845
DEL_OTHERS_CACHED_VIEWS: (state, view) => {
4946
for (const i of state.cachedViews) {
@@ -56,7 +53,9 @@ const tagsView = {
5653
},
5754

5855
DEL_ALL_VISITED_VIEWS: state => {
59-
state.visitedViews = []
56+
// keep affix tags
57+
const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
58+
state.visitedViews = affixTags
6059
},
6160
DEL_ALL_CACHED_VIEWS: state => {
6261
state.cachedViews = []

src/views/layout/components/TagsView.vue

+60-16
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@
1212
@click.middle.native="closeSelectedTag(tag)"
1313
@contextmenu.prevent.native="openMenu(tag,$event)">
1414
{{ generateTitle(tag.title) }}
15-
<span class="el-icon-close" @click.prevent.stop="closeSelectedTag(tag)" />
15+
<span v-if="!tag.meta.affix" class="el-icon-close" @click.prevent.stop="closeSelectedTag(tag)" />
1616
</router-link>
1717
</scroll-pane>
1818
<ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
1919
<li @click="refreshSelectedTag(selectedTag)">{{ $t('tagsView.refresh') }}</li>
20-
<li @click="closeSelectedTag(selectedTag)">{{ $t('tagsView.close') }}</li>
20+
<li v-if="!(selectedTag.meta&&selectedTag.meta.affix)" @click="closeSelectedTag(selectedTag)">{{
21+
$t('tagsView.close') }}</li>
2122
<li @click="closeOthersTags">{{ $t('tagsView.closeOthers') }}</li>
22-
<li @click="closeAllTags">{{ $t('tagsView.closeAll') }}</li>
23+
<li @click="closeAllTags(selectedTag)">{{ $t('tagsView.closeAll') }}</li>
2324
</ul>
2425
</div>
2526
</template>
2627

2728
<script>
2829
import ScrollPane from '@/components/ScrollPane'
2930
import { generateTitle } from '@/utils/i18n'
31+
import path from 'path'
3032
3133
export default {
3234
components: { ScrollPane },
@@ -35,17 +37,21 @@ export default {
3537
visible: false,
3638
top: 0,
3739
left: 0,
38-
selectedTag: {}
40+
selectedTag: {},
41+
affixTags: []
3942
}
4043
},
4144
computed: {
4245
visitedViews() {
4346
return this.$store.state.tagsView.visitedViews
47+
},
48+
routers() {
49+
return this.$store.state.permission.routers
4450
}
4551
},
4652
watch: {
4753
$route() {
48-
this.addViewTags()
54+
this.addTags()
4955
this.moveToCurrentTag()
5056
},
5157
visible(value) {
@@ -57,14 +63,44 @@ export default {
5763
}
5864
},
5965
mounted() {
60-
this.addViewTags()
66+
this.initTags()
67+
this.addTags()
6168
},
6269
methods: {
6370
generateTitle, // generateTitle by vue-i18n
6471
isActive(route) {
6572
return route.path === this.$route.path
6673
},
67-
addViewTags() {
74+
filterAffixTags(routes, basePath = '/') {
75+
let tags = []
76+
routes.forEach(route => {
77+
if (route.meta && route.meta.affix) {
78+
tags.push({
79+
path: path.resolve(basePath, route.path),
80+
name: route.name,
81+
meta: { ...route.meta }
82+
})
83+
}
84+
if (route.children) {
85+
const tempTags = this.filterAffixTags(route.children, route.path)
86+
if (tempTags.length >= 1) {
87+
tags = [...tags, ...tempTags]
88+
}
89+
}
90+
})
91+
92+
return tags
93+
},
94+
initTags() {
95+
const affixTags = this.affixTags = this.filterAffixTags(this.routers)
96+
for (const tag of affixTags) {
97+
// Must have tag name
98+
if (tag.name) {
99+
this.$store.dispatch('addVisitedView', tag)
100+
}
101+
}
102+
},
103+
addTags() {
68104
const { name } = this.$route
69105
if (name) {
70106
this.$store.dispatch('addView', this.$route)
@@ -101,12 +137,7 @@ export default {
101137
closeSelectedTag(view) {
102138
this.$store.dispatch('delView', view).then(({ visitedViews }) => {
103139
if (this.isActive(view)) {
104-
const latestView = visitedViews.slice(-1)[0]
105-
if (latestView) {
106-
this.$router.push(latestView)
107-
} else {
108-
this.$router.push('/')
109-
}
140+
this.toLastView(visitedViews)
110141
}
111142
})
112143
},
@@ -116,9 +147,22 @@ export default {
116147
this.moveToCurrentTag()
117148
})
118149
},
119-
closeAllTags() {
120-
this.$store.dispatch('delAllViews')
121-
this.$router.push('/')
150+
closeAllTags(view) {
151+
this.$store.dispatch('delAllViews').then(({ visitedViews }) => {
152+
if (this.affixTags.some(tag => tag.path === view.path)) {
153+
return
154+
}
155+
this.toLastView(visitedViews)
156+
})
157+
},
158+
toLastView(visitedViews) {
159+
const latestView = visitedViews.slice(-1)[0]
160+
if (latestView) {
161+
this.$router.push(latestView)
162+
} else {
163+
// You can set another route
164+
this.$router.push('/')
165+
}
122166
},
123167
openMenu(tag, e) {
124168
const menuMinWidth = 105

0 commit comments

Comments
 (0)