vuepress-theme-reco/Layout.vue

185 lines
4.5 KiB
Vue
Raw Normal View History

2018-09-14 21:41:31 +08:00
<template>
<div
class="theme-container"
:class="pageClasses"
@touchstart="onTouchStart"
2018-12-03 22:19:15 +08:00
@touchend="onTouchEnd">
2019-04-04 18:24:25 +08:00
<Password v-if="!isHasKey"></Password>
2018-12-03 22:19:15 +08:00
2019-04-04 18:24:25 +08:00
<div v-else>
<Navbar v-if="shouldShowNavbar" @toggle-sidebar="toggleSidebar"/>
2018-12-03 22:19:15 +08:00
2019-04-04 18:24:25 +08:00
<div class="sidebar-mask" @click="toggleSidebar(false)"></div>
2018-09-14 21:41:31 +08:00
2019-04-04 18:24:25 +08:00
<Sidebar :items="sidebarItems" @toggle-sidebar="toggleSidebar">
<slot name="sidebar-top" slot="top"/>
<slot name="sidebar-bottom" slot="bottom"/>
</Sidebar>
<div class="custom-layout" v-if="$page.frontmatter.layout">
<component :is="$page.frontmatter.layout"/>
</div>
2018-09-14 21:41:31 +08:00
2019-04-04 18:24:25 +08:00
<Home v-else-if="$page.frontmatter.home"/>
2018-09-14 21:41:31 +08:00
2019-04-04 18:24:25 +08:00
<Page v-else :sidebar-items="sidebarItems" @tagChange="tagChange">
<slot name="page-top" slot="top"/>
<slot name="page-bottom" slot="bottom"/>
</Page>
2018-09-14 21:41:31 +08:00
2019-04-04 18:24:25 +08:00
<Valine :valineRefresh="valineRefresh"></Valine>
2019-01-06 03:06:06 +08:00
2019-04-04 18:24:25 +08:00
<router-view></router-view>
<SWUpdatePopup :updateEvent="swUpdateEvent"/>
<BackToTop></BackToTop>
</div>
2018-09-14 21:41:31 +08:00
</div>
</template>
<script>
2018-12-03 22:19:15 +08:00
import Vue from "vue";
import nprogress from "nprogress";
2019-01-06 03:06:06 +08:00
import Home from "./pages/Home/";
2018-12-03 22:19:15 +08:00
import Navbar from "./components/Navbar/";
2019-01-06 03:06:06 +08:00
import Page from "./pages/Page/";
2018-12-03 22:19:15 +08:00
import Sidebar from "./components/Sidebar/";
import SWUpdatePopup from "./components/SWUpdatePopup/";
2019-01-08 11:49:19 +08:00
import { resolveSidebarItems } from "./util/"
2019-01-29 13:57:19 +08:00
import BackToTop from "./components/BackToTop/"
import Valine from './components/Valine/'
2019-04-04 18:24:25 +08:00
import Password from './components/Password/'
import key from './util/handleKey'
2018-09-14 21:41:31 +08:00
export default {
2019-04-04 18:24:25 +08:00
components: { Home, Page, Sidebar, Navbar, SWUpdatePopup, BackToTop, Valine, Password },
2018-09-14 21:41:31 +08:00
2018-12-03 22:19:15 +08:00
data() {
2018-09-14 21:41:31 +08:00
return {
isSidebarOpen: false,
2019-01-29 13:57:19 +08:00
swUpdateEvent: null,
valineRefresh: false
2018-12-03 22:19:15 +08:00
};
2018-09-14 21:41:31 +08:00
},
2018-12-03 22:19:15 +08:00
2018-09-14 21:41:31 +08:00
computed: {
2018-12-03 22:19:15 +08:00
shouldShowNavbar() {
const { themeConfig } = this.$site;
const { frontmatter } = this.$page;
if (frontmatter.navbar === false || themeConfig.navbar === false) {
return false;
2018-09-14 21:41:31 +08:00
}
return (
this.$title ||
themeConfig.logo ||
themeConfig.repo ||
themeConfig.nav ||
this.$themeLocaleConfig.nav
2018-12-03 22:19:15 +08:00
);
2018-09-14 21:41:31 +08:00
},
2018-12-03 22:19:15 +08:00
shouldShowSidebar() {
const { frontmatter } = this.$page;
2018-09-14 21:41:31 +08:00
return (
!frontmatter.layout &&
!frontmatter.home &&
frontmatter.sidebar !== false &&
this.sidebarItems.length
2018-12-03 22:19:15 +08:00
);
2018-09-14 21:41:31 +08:00
},
2018-12-03 22:19:15 +08:00
sidebarItems() {
2018-09-14 21:41:31 +08:00
return resolveSidebarItems(
this.$page,
this.$route,
this.$site,
this.$localePath
2018-12-03 22:19:15 +08:00
);
2018-09-14 21:41:31 +08:00
},
2018-12-03 22:19:15 +08:00
pageClasses() {
const userPageClass = this.$page.frontmatter.pageClass;
2018-09-14 21:41:31 +08:00
return [
{
2018-12-03 22:19:15 +08:00
"no-navbar": !this.shouldShowNavbar,
"sidebar-open": this.isSidebarOpen,
"no-sidebar": !this.shouldShowSidebar
2018-09-14 21:41:31 +08:00
},
userPageClass
2018-12-03 22:19:15 +08:00
];
2019-04-04 18:24:25 +08:00
},
isHasKey () {
const keyPage = this.$site.themeConfig.keyPage
if (!keyPage) {
return true
}
const {keys} = keyPage
return key.isHasKey(keys)
2018-09-14 21:41:31 +08:00
}
},
2018-12-03 22:19:15 +08:00
mounted() {
window.addEventListener("scroll", this.onScroll);
2018-09-14 21:41:31 +08:00
// configure progress bar
2018-12-03 22:19:15 +08:00
nprogress.configure({ showSpinner: false });
2018-09-14 21:41:31 +08:00
this.$router.beforeEach((to, from, next) => {
if (to.path !== from.path && !Vue.component(to.name)) {
2018-12-03 22:19:15 +08:00
nprogress.start();
2018-09-14 21:41:31 +08:00
}
2018-12-03 22:19:15 +08:00
next();
});
2018-09-14 21:41:31 +08:00
this.$router.afterEach(() => {
2018-12-03 22:19:15 +08:00
nprogress.done();
this.isSidebarOpen = false;
});
2018-09-14 21:41:31 +08:00
2018-12-03 22:19:15 +08:00
this.$on("sw-updated", this.onSWUpdated);
2018-09-14 21:41:31 +08:00
},
methods: {
2019-01-29 13:57:19 +08:00
tagChange () {
this.valineRefresh = true
setTimeout(() => {
this.valineRefresh = false
}, 300)
},
2018-12-03 22:19:15 +08:00
toggleSidebar(to) {
this.isSidebarOpen = typeof to === "boolean" ? to : !this.isSidebarOpen;
2018-09-14 21:41:31 +08:00
},
// side swipe
2018-12-03 22:19:15 +08:00
onTouchStart(e) {
2018-09-14 21:41:31 +08:00
this.touchStart = {
x: e.changedTouches[0].clientX,
y: e.changedTouches[0].clientY
2018-12-03 22:19:15 +08:00
};
2018-09-14 21:41:31 +08:00
},
2018-12-03 22:19:15 +08:00
onTouchEnd(e) {
const dx = e.changedTouches[0].clientX - this.touchStart.x;
const dy = e.changedTouches[0].clientY - this.touchStart.y;
2018-09-14 21:41:31 +08:00
if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 40) {
if (dx > 0 && this.touchStart.x <= 80) {
2018-12-03 22:19:15 +08:00
this.toggleSidebar(true);
2018-09-14 21:41:31 +08:00
} else {
2018-12-03 22:19:15 +08:00
this.toggleSidebar(false);
2018-09-14 21:41:31 +08:00
}
}
},
2018-12-03 22:19:15 +08:00
onSWUpdated(e) {
this.swUpdateEvent = e;
2018-09-14 21:41:31 +08:00
}
}
2018-12-03 22:19:15 +08:00
};
2018-09-14 21:41:31 +08:00
</script>
<style src="prismjs/themes/prism-tomorrow.css"></style>
2019-01-06 03:06:06 +08:00
<style src="./styles/theme.styl" lang="stylus"></style>
2019-01-29 13:57:19 +08:00