feat[tags-view]: tags-view add contextmenu (#343)

* add the menu by right-clicking the tags

* bug fixed

* refine
This commit is contained in:
leij1ang 2017-12-13 17:43:03 +08:00 committed by 花裤衩
parent a68413cb8f
commit b6d97f1806
2 changed files with 202 additions and 111 deletions

View File

@ -29,6 +29,25 @@ const tagsView = {
break break
} }
} }
},
DEL_OTHER_VIEWS: (state, view) => {
for (const [i, v] of state.visitedViews.entries()) {
if (v.path === view.path) {
state.visitedViews = [].concat(state.visitedViews.slice(i, i + 1))
break
}
}
for (const i of state.cachedViews) {
if (i === view.name) {
const index = state.cachedViews.indexOf(i)
state.cachedViews = [].concat(state.cachedViews.slice(index, i + 1))
break
}
}
},
DEL_ALL_VIEWS: (state) => {
state.visitedViews = []
state.cachedViews = []
} }
}, },
actions: { actions: {
@ -40,6 +59,18 @@ const tagsView = {
commit('DEL_VISITED_VIEWS', view) commit('DEL_VISITED_VIEWS', view)
resolve([...state.visitedViews]) resolve([...state.visitedViews])
}) })
},
delOtherViews({ commit, state }, view) {
return new Promise((resolve) => {
commit('DEL_OTHER_VIEWS', view)
resolve([...state.visitedViews])
})
},
delAllViews({ commit, state }) {
return new Promise((resolve) => {
commit('DEL_ALL_VIEWS')
resolve([...state.visitedViews])
})
} }
} }
} }

View File

@ -1,10 +1,17 @@
<template> <template>
<div class="tag-container">
<scroll-pane class='tags-view-container' ref='scrollPane'> <scroll-pane class='tags-view-container' ref='scrollPane'>
<router-link ref='tag' class="tags-view-item" :class="isActive(tag)?'active':''" v-for="tag in Array.from(visitedViews)" :to="tag.path":key="tag.path"> <router-link ref='tag' class="tags-view-item" :class="isActive(tag)?'active':''" v-for="tag in Array.from(visitedViews)" :to="tag.path":key="tag.path" @contextmenu.prevent.native="openMenu(tag,$event)">
{{generateTitle(tag.title)}} {{generateTitle(tag.title)}}
<span class='el-icon-close' @click='closeViewTags(tag,$event)'></span> <span class='el-icon-close' @click='closeViewTags(tag,$event)'></span>
</router-link> </router-link>
</scroll-pane> </scroll-pane>
<ul class='contextmenu' v-show="visible" :style="{left:left+'px',top:top+'px'}">
<li @click="closeViewTags(selectedTag, $event)">关闭</li>
<li @click="closeOtherTags">关闭其他</li>
<li @click="closeAllTags">关闭所有</li>
</ul>
</div>
</template> </template>
<script> <script>
@ -13,6 +20,14 @@ import { generateTitle } from '@/utils/i18n'
export default { export default {
components: { ScrollPane }, components: { ScrollPane },
data() {
return {
visible: false,
top: 0,
left: 0,
selectedTag: {}
}
},
computed: { computed: {
visitedViews() { visitedViews() {
return this.$store.state.tagsView.visitedViews return this.$store.state.tagsView.visitedViews
@ -36,6 +51,14 @@ export default {
}) })
$event.preventDefault() $event.preventDefault()
}, },
closeOtherTags() {
this.$router.push(this.selectedTag.path)
this.$store.dispatch('delOtherViews', this.selectedTag)
},
closeAllTags() {
this.$store.dispatch('delAllViews')
this.$router.push('/')
},
generateRoute() { generateRoute() {
if (this.$route.name) { if (this.$route.name) {
return this.$route return this.$route
@ -62,18 +85,54 @@ export default {
} }
} }
}) })
},
openMenu(tag, e) {
this.visible = true
this.selectedTag = tag
this.left = e.clientX
this.top = e.clientY
},
closeMenu() {
this.visible = false
} }
}, },
watch: { watch: {
$route() { $route() {
this.addViewTags() this.addViewTags()
this.moveToCurrentTag() this.moveToCurrentTag()
},
visible(v) {
if (v) {
window.addEventListener('click', this.closeMenu, false)
} else {
window.removeEventListener('click', this.closeMenu, false)
}
} }
} }
} }
</script> </script>
<style rel="stylesheet/scss" lang="scss" scoped> <style rel="stylesheet/scss" lang="scss" scoped>
.tag-container {
.contextmenu {
margin: 0;
background: #fff;
z-index: 99999;
position: absolute;
list-style-type: none;
padding-left: 0;
border: 1px solid rgba(0, 0, 0, 0.4);
font-size: 0.8rem;
box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .5);
li {
margin: 0;
padding: 0.2rem 1.5rem 0.3rem 0.8rem;
&:hover {
background: #eee;
cursor: default;
}
}
}
.tags-view-container { .tags-view-container {
background: #fff; background: #fff;
height: 34px; height: 34px;
@ -111,6 +170,7 @@ export default {
} }
} }
} }
}
</style> </style>
<style rel="stylesheet/scss" lang="scss"> <style rel="stylesheet/scss" lang="scss">