This commit is contained in:
leij1ang 2017-12-04 11:11:29 +00:00 committed by GitHub
commit c1575f61bf
3 changed files with 196 additions and 54 deletions

View File

@ -3,6 +3,10 @@
<div class="scroll-wrapper" ref="scrollWrapper" :style="{left: left + 'px'}"> <div class="scroll-wrapper" ref="scrollWrapper" :style="{left: left + 'px'}">
<slot></slot> <slot></slot>
</div> </div>
<div class="bar-container" v-show="isOverflow" ref="scrollbarContainer">
<div class="scrollbar" ref="scrollbar">
</div>
</div>
</div> </div>
</template> </template>
@ -11,41 +15,83 @@ export default {
name: 'scrollPane', name: 'scrollPane',
data() { data() {
return { return {
left: 0 left: 0,
isOverflow: false,
container: undefined,
containerWidth: undefined,
wrapper: undefined,
wrapperWidth: undefined,
scrollbarContainer: undefined,
scrollbarContainerWidth: undefined,
scrollbar: undefined,
scrollbarWidth: undefined
} }
}, },
mounted() {
this.container = this.$refs.scrollContainer
this.wrapper = this.$refs.scrollWrapper
this.scrollbarContainer = this.$refs.scrollbarContainer
this.scrollbar = this.$refs.scrollbar
this.calcSize()
window.addEventListener('resize', () => {
this.$forceUpdate()
this.calcSize()
}, false)
},
methods: { methods: {
calcPos() {
if (this.isOverflow) {
this.scrollbar.style.left = -(this.scrollbarContainerWidth - this.scrollbarWidth) * this.left / (this.wrapperWidth - this.containerWidth + 100) + 'px'
}
},
calcSize() {
//
this.containerWidth = parseFloat(this.container.offsetWidth, 10)
this.wrapperWidth = parseFloat(this.wrapper.offsetWidth, 10)
this.scrollbarContainerWidth = parseFloat(this.scrollbarContainer.offsetWidth, 10)
this.isOverflow = this.wrapperWidth + 100 > this.containerWidth
if (!this.isOverflow) return false
this.scrollbarWidth = this.scrollbarContainerWidth * this.containerWidth / (this.wrapperWidth + 100)
this.scrollbar.style.width = this.scrollbarWidth + 'px'
this.calcPos()
},
handleScroll(e) { handleScroll(e) {
e.preventDefault() e.preventDefault()
const $container = this.$refs.scrollContainer
const $containerWidth = $container.offsetWidth
const $wrapper = this.$refs.scrollWrapper
const $wrapperWidth = $wrapper.offsetWidth
if (e.wheelDelta > 0) { if (e.wheelDelta > 0) {
//
this.left = Math.min(0, this.left + e.wheelDelta) this.left = Math.min(0, this.left + e.wheelDelta)
} else { } else {
if ($containerWidth - 100 < $wrapperWidth) { //
if (this.left < -($wrapperWidth - $containerWidth + 100)) { // [-(this.wrapperWidth-this.containerWidth+100),0]
this.left = this.left if (this.left >= -(this.wrapperWidth - this.containerWidth + 100)) {
} else { this.left = Math.max(this.left + e.wheelDelta, this.containerWidth - this.wrapperWidth - 100)
this.left = Math.max(this.left + e.wheelDelta, $containerWidth - $wrapperWidth - 100)
}
} else {
this.left = 0
} }
} }
this.calcPos()
} }
} }
} }
</script> </script>
<style rel="stylesheet/scss" lang="scss" scoped> <style rel="stylesheet/scss" lang="scss" scoped>
.scroll-container { .scroll-container {
white-space: nowrap; white-space: nowrap;
position: relative; position: relative;
overflow: hidden; overflow: hidden;
.scroll-wrapper { .scroll-wrapper {
position: absolute; position: absolute;
} }
} .bar-container{
position: absolute;
top: 30px;
width:100%;
height: 5px;
.scrollbar{
position: absolute;
top: 0;
height: 100%;
background: rgba(0,0,0,.8);
}
}
}
</style> </style>

View File

@ -47,6 +47,25 @@ const app = {
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: {
@ -64,6 +83,18 @@ const app = {
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,25 +1,46 @@
<template> <template>
<scroll-pane class='tags-view-container'> <div class="tag-container">
<router-link class="tags-view-item" :class="isActive(tag)?'active':''" v-for="tag in Array.from(visitedViews)" :to="tag.path":key="tag.path"> <scroll-pane class='tags-view-container' ref="pane">
<router-link 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)" v-Clickoutside="closeMenu">
{{$t('route.'+tag.title)}} {{$t('route.'+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="open" :style="{left:left+'px',top:top+'px'}">
<li @click="closePage(0,$event)">关闭</li>
<li @click="closePage(1,$event)">关闭其他</li>
<li @click="closePage(2,$event)">关闭所有</li>
</ul>
</div>
</template> </template>
<script> <script>
import ScrollPane from '@/components/ScrollPane' import ScrollPane from '@/components/ScrollPane'
import Clickoutside from 'element-ui/src/utils/clickoutside'
export default { export default {
components: { ScrollPane }, components: { ScrollPane },
directives: { Clickoutside },
computed: { computed: {
visitedViews() { visitedViews() {
return this.$store.state.app.visitedViews return this.$store.state.app.visitedViews
} }
}, },
data() {
return {
open: false,
isOverflow: false,
top: 0,
left: 0,
isSelect: {}
}
},
mounted() { mounted() {
this.addViewTags() this.addViewTags()
}, },
updated() {
this.$nextTick(this.$refs.pane.calcSize())
},
methods: { methods: {
closeViewTags(view, $event) { closeViewTags(view, $event) {
this.$store.dispatch('delVisitedViews', view).then((views) => { this.$store.dispatch('delVisitedViews', view).then((views) => {
@ -34,6 +55,20 @@ export default {
}) })
$event.preventDefault() $event.preventDefault()
}, },
closePage(flag, $event) {
if (flag === 0) {
this.closeViewTags(this.isSelect, $event)
} else if (flag === 1) {
this.$router.push(this.isSelect.path)
this.$store.dispatch('delOtherViews', this.isSelect)
$event.preventDefault()
} else {
this.$router.push('/')
this.$store.dispatch('delAllViews')
$event.preventDefault()
}
this.open = false
},
generateRoute() { generateRoute() {
if (this.$route.name) { if (this.$route.name) {
return this.$route return this.$route
@ -49,8 +84,16 @@ export default {
}, },
isActive(route) { isActive(route) {
return route.path === this.$route.path || route.name === this.$route.name return route.path === this.$route.path || route.name === this.$route.name
},
openMenu(tag, e) {
this.open = true
this.isSelect = tag
this.left = e.clientX
this.top = e.clientY
},
closeMenu() {
this.open = false
} }
}, },
watch: { watch: {
$route() { $route() {
@ -61,7 +104,27 @@ export default {
</script> </script>
<style rel="stylesheet/scss" lang="scss" scoped> <style rel="stylesheet/scss" lang="scss" scoped>
.tags-view-container { .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 {
background: #fff; background: #fff;
height: 34px; height: 34px;
border-bottom: 1px solid #d8dce5; border-bottom: 1px solid #d8dce5;
@ -97,7 +160,8 @@ export default {
} }
} }
} }
} }
}
</style> </style>
<style rel="stylesheet/scss" lang="scss"> <style rel="stylesheet/scss" lang="scss">
@ -123,4 +187,5 @@ export default {
} }
} }
} }
</style> </style>