fix(cli): site keyboard nav

This commit is contained in:
陈嘉涵 2019-12-09 17:18:41 +08:00
parent 396002495f
commit 3e43a860eb

View File

@ -51,63 +51,40 @@ export default {
} }
}, },
data() {
return {
nav: [],
currentPath: null,
leftNav: null,
rightNav: null
};
},
watch: { watch: {
// eslint-disable-next-line // eslint-disable-next-line
'$route.path'() { '$route.path'() {
this.setNav(); this.setNav();
this.updateNav();
} }
}, },
created() { created() {
this.setNav(); this.setNav();
this.updateNav();
this.keyboardHandler(); this.keyboardHandler();
}, },
methods: { methods: {
setNav() { setNav() {
const { nav } = this.config; const { nav } = this.config;
for (let i = 0; i < nav.length; i++) { const items = nav.reduce((list, item) => list.concat(item.items), []);
const navItem = nav[i]; const currentPath = this.$route.path.split('/').pop();
if (!navItem.groups) {
this.nav.push(nav[i]);
} else {
for (let j = 0; j < navItem.groups.length; j++) {
this.nav = this.nav.concat(navItem.groups[j].list);
}
}
}
},
updateNav() {
let currentIndex; let currentIndex;
this.currentPath = '/' + this.$route.path.split('/').pop(); for (let i = 0, len = items.length; i < len; i++) {
for (let i = 0, len = this.nav.length; i < len; i++) { if (items[i].path === currentPath) {
if (this.nav[i].path === this.currentPath) {
currentIndex = i; currentIndex = i;
break; break;
} }
} }
this.leftNav = this.nav[currentIndex - 1];
this.rightNav = this.nav[currentIndex + 1]; this.leftNav = items[currentIndex - 1];
this.rightNav = items[currentIndex + 1];
}, },
handleNavClick(direction) { keyboardNav(direction) {
const nav = direction === 'prev' ? this.leftNav : this.rightNav; const nav = direction === 'prev' ? this.leftNav : this.rightNav;
if (nav.path) { if (nav.path) {
this.$router.push(this.base + nav.path); this.$router.push(this.base + nav.path);
} else if (nav.link) {
window.location.href = nav.link;
} }
}, },
@ -115,10 +92,10 @@ export default {
window.addEventListener('keyup', event => { window.addEventListener('keyup', event => {
switch (event.keyCode) { switch (event.keyCode) {
case 37: // left case 37: // left
this.handleNavClick('prev'); this.keyboardNav('prev');
break; break;
case 39: // right case 39: // right
this.handleNavClick('next'); this.keyboardNav('next');
break; break;
} }
}); });