|
| 1 | +<template> |
| 2 | + <div |
| 3 | + class="theme-container" |
| 4 | + :class="pageClasses" |
| 5 | + @touchstart="onTouchStart" |
| 6 | + @touchend="onTouchEnd" |
| 7 | + > |
| 8 | + <!-- Google Tag Manager (noscript) --> |
| 9 | + <noscript> |
| 10 | + <iframe |
| 11 | + :src="`https://www.googletagmanager.com/ns.html?id=${ $site.themeConfig.GTM_TAG }`" |
| 12 | + height="0" |
| 13 | + width="0" |
| 14 | + style="display:none;visibility:hidden"> |
| 15 | + </iframe> |
| 16 | + </noscript> |
| 17 | + <!-- End Google Tag Manager (noscript) --> |
| 18 | + |
| 19 | + <Navbar |
| 20 | + v-if="shouldShowNavbar" |
| 21 | + @toggle-sidebar="toggleSidebar" |
| 22 | + /> |
| 23 | + |
| 24 | + <div |
| 25 | + class="sidebar-mask" |
| 26 | + @click="toggleSidebar(false)" |
| 27 | + /> |
| 28 | + |
| 29 | + <Sidebar |
| 30 | + :items="sidebarItems" |
| 31 | + @toggle-sidebar="toggleSidebar" |
| 32 | + > |
| 33 | + <template #top> |
| 34 | + <slot name="sidebar-top" /> |
| 35 | + </template> |
| 36 | + <template #bottom> |
| 37 | + <slot name="sidebar-bottom" /> |
| 38 | + </template> |
| 39 | + </Sidebar> |
| 40 | + |
| 41 | + <Home v-if="$page.frontmatter.home" /> |
| 42 | + |
| 43 | + <Page |
| 44 | + v-else |
| 45 | + :sidebar-items="sidebarItems" |
| 46 | + > |
| 47 | + <template #top> |
| 48 | + <slot name="page-top" /> |
| 49 | + </template> |
| 50 | + <template #bottom> |
| 51 | + <slot name="page-bottom" /> |
| 52 | + </template> |
| 53 | + </Page> |
| 54 | + </div> |
| 55 | +</template> |
| 56 | + |
| 57 | +<script> |
| 58 | +import Home from '@theme/components/Home.vue' |
| 59 | +import Navbar from '@theme/components/Navbar.vue' |
| 60 | +import Page from '@theme/components/Page.vue' |
| 61 | +import Sidebar from '@theme/components/Sidebar.vue' |
| 62 | +import { resolveSidebarItems } from '@vuepress/theme-default/util' |
| 63 | +export default { |
| 64 | + name: 'Layout', |
| 65 | + components: { |
| 66 | + Home, |
| 67 | + Page, |
| 68 | + Sidebar, |
| 69 | + Navbar |
| 70 | + }, |
| 71 | + data () { |
| 72 | + return { |
| 73 | + isSidebarOpen: false |
| 74 | + } |
| 75 | + }, |
| 76 | + computed: { |
| 77 | + shouldShowNavbar () { |
| 78 | + const { themeConfig } = this.$site |
| 79 | + const { frontmatter } = this.$page |
| 80 | + if ( |
| 81 | + frontmatter.navbar === false |
| 82 | + || themeConfig.navbar === false) { |
| 83 | + return false |
| 84 | + } |
| 85 | + return ( |
| 86 | + this.$title |
| 87 | + || themeConfig.logo |
| 88 | + || themeConfig.repo |
| 89 | + || themeConfig.nav |
| 90 | + || this.$themeLocaleConfig.nav |
| 91 | + ) |
| 92 | + }, |
| 93 | + shouldShowSidebar () { |
| 94 | + const { frontmatter } = this.$page |
| 95 | + return ( |
| 96 | + !frontmatter.home |
| 97 | + && frontmatter.sidebar !== false |
| 98 | + && this.sidebarItems.length |
| 99 | + ) |
| 100 | + }, |
| 101 | + sidebarItems () { |
| 102 | + return resolveSidebarItems( |
| 103 | + this.$page, |
| 104 | + this.$page.regularPath, |
| 105 | + this.$site, |
| 106 | + this.$localePath |
| 107 | + ) |
| 108 | + }, |
| 109 | + pageClasses () { |
| 110 | + const userPageClass = this.$page.frontmatter.pageClass |
| 111 | + return [ |
| 112 | + { |
| 113 | + 'no-navbar': !this.shouldShowNavbar, |
| 114 | + 'sidebar-open': this.isSidebarOpen, |
| 115 | + 'no-sidebar': !this.shouldShowSidebar |
| 116 | + }, |
| 117 | + userPageClass |
| 118 | + ] |
| 119 | + } |
| 120 | + }, |
| 121 | + mounted () { |
| 122 | + this.$router.afterEach(() => { |
| 123 | + this.isSidebarOpen = false |
| 124 | + }) |
| 125 | + }, |
| 126 | + methods: { |
| 127 | + toggleSidebar (to) { |
| 128 | + this.isSidebarOpen = typeof to === 'boolean' ? to : !this.isSidebarOpen |
| 129 | + this.$emit('toggle-sidebar', this.isSidebarOpen) |
| 130 | + }, |
| 131 | + // side swipe |
| 132 | + onTouchStart (e) { |
| 133 | + this.touchStart = { |
| 134 | + x: e.changedTouches[0].clientX, |
| 135 | + y: e.changedTouches[0].clientY |
| 136 | + } |
| 137 | + }, |
| 138 | + onTouchEnd (e) { |
| 139 | + const dx = e.changedTouches[0].clientX - this.touchStart.x |
| 140 | + const dy = e.changedTouches[0].clientY - this.touchStart.y |
| 141 | + if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 40) { |
| 142 | + if (dx > 0 && this.touchStart.x <= 80) { |
| 143 | + this.toggleSidebar(true) |
| 144 | + } else { |
| 145 | + this.toggleSidebar(false) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | +</script> |
0 commit comments