allow navigation information to be cached

This commit is contained in:
Weidi Deng 2021-01-06 16:58:29 +08:00
parent 43e0d4a856
commit e73c6e6019
3 changed files with 47 additions and 3 deletions

View File

@ -25,7 +25,12 @@ const state = {
showMessage: null,
showConfirm: null,
previewMode: false,
hash: ''
hash: '',
lastViewed: {
path: '',
clicked: '',
pageOffset: 0
}
}
export default new Vuex.Store({

View File

@ -88,6 +88,7 @@ const mutations = {
state.previewMode = value
},
setHash: (state, value) => (state.hash = value),
setLastViewed: (state, value) => (state.lastViewed = value)
}
export default mutations

View File

@ -63,7 +63,8 @@ export default {
'reload',
'multiple',
'loading',
'show'
'show',
'lastViewed'
]),
isPreview () {
return !this.loading && !this.isListing && !this.isEditor || this.loading && this.$store.state.previewMode
@ -111,7 +112,7 @@ export default {
this.fetchData()
},
watch: {
'$route': 'fetchData',
'$route': 'nav',
'reload': function () {
this.fetchData()
}
@ -129,6 +130,43 @@ export default {
},
methods: {
...mapMutations([ 'setLoading' ]),
async nav() {
if (!this.isPreview && !this.isEditor && clean(`/${this.$route.params.pathMatch}`) !== clean(this.lastViewed.path) && clean(`/${this.$route.params.pathMatch}`).startsWith(clean(this.lastViewed.path))) {
let dirs = clean(this.$route.fullPath).split("/")
this.$store.commit('setLastViewed', {
path: this.req.path,
clicked: decodeURIComponent(dirs[dirs.length - 1]),
pageOffset: window.pageYOffset
})
this.fetchData()
} else if (clean(this.lastViewed.path) === clean(`/${this.$route.params.pathMatch}`)) {
await this.fetchData()
let offset = 1000, oldPageOffset = 0, pageOffset = window.pageYOffset, _this = this
let int = setInterval(function() {
window.scrollTo(0, offset)
oldPageOffset = pageOffset
pageOffset = window.pageYOffset
if (offset >= _this.lastViewed.pageOffset || oldPageOffset === pageOffset) clearInterval(int);
offset += Math.min(1000, _this.lastViewed.pageOffset - offset)
}, 20);
for (let i = 0; i < this.req.items.length; i++) {
if (this.req.items[i].name === this.lastViewed.clicked) {
this.$store.commit('addSelected', i)
break
}
}
} else {
this.$store.commit('setLastViewed', {
path: '',
clicked: '',
pageOffset: 0
})
this.fetchData()
}
},
async fetchData () {
// Reset view information.
this.$store.commit('setReload', false)