Layout.vue 4.0 KB
Newer Older
E
Evan You 已提交
1
<template>
E
Evan You 已提交
2
  <div class="theme-container"
3
    :class="pageClasses"
E
Evan You 已提交
4 5
    @touchstart="onTouchStart"
    @touchend="onTouchEnd">
E
tweaks  
Evan You 已提交
6
    <Navbar v-if="shouldShowNavbar" @toggle-sidebar="toggleSidebar"/>
E
Evan You 已提交
7
    <Sidebar :items="sidebarItems" @toggle-sidebar="toggleSidebar"/>
E
Evan You 已提交
8 9 10 11
    <div class="custom-layout" v-if="$page.frontmatter.layout">
      <component :is="$page.frontmatter.layout"/>
    </div>
    <Home v-else-if="$page.frontmatter.home"/>
E
Evan You 已提交
12
    <Page v-else :sidebar-items="sidebarItems"/>
E
Evan You 已提交
13 14 15 16
  </div>
</template>

<script>
17
import Vue from 'vue'
E
Evan You 已提交
18
import nprogress from 'nprogress'
E
Evan You 已提交
19
import Home from './Home.vue'
E
Evan You 已提交
20
import Navbar from './Navbar.vue'
E
Evan You 已提交
21
import Page from './Page.vue'
E
Evan You 已提交
22
import Sidebar from './Sidebar.vue'
23
import { pathToComponentName, getTitle, getLang } from '@app/util'
E
Evan You 已提交
24
import { resolveSidebarItems } from './util'
E
Evan You 已提交
25

E
Evan You 已提交
26
export default {
E
Evan You 已提交
27
  components: { Home, Page, Sidebar, Navbar },
E
Evan You 已提交
28 29 30 31 32
  data () {
    return {
      isSidebarOpen: false
    }
  },
E
Evan You 已提交
33

E
tweaks  
Evan You 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47
  computed: {
    shouldShowNavbar () {
      const { themeConfig } = this.$site
      return (
        this.$site.title ||
        themeConfig.logo ||
        themeConfig.repo ||
        themeConfig.nav
      )
    },
    shouldShowSidebar () {
      const { themeConfig } = this.$site
      const { frontmatter } = this.$page
      return (
E
Evan You 已提交
48 49 50 51 52
        !frontmatter.layout &&
        !frontmatter.home &&
        frontmatter.sidebar !== false && (
          frontmatter.sidebar === 'auto' ||
          themeConfig.sidebar
E
Evan You 已提交
53
        )
E
tweaks  
Evan You 已提交
54
      )
E
Evan You 已提交
55 56 57 58 59 60 61
    },
    sidebarItems () {
      return resolveSidebarItems(
        this.$page,
        this.$route,
        this.$site
      )
62 63 64 65 66 67 68 69 70 71 72
    },
    pageClasses() {
      const userPageClass = this.$page.frontmatter.pageClass
      return [
        {
          'no-navbar': !this.shouldShowNavbar,
          'sidebar-open': this.isSidebarOpen,
          'no-sidebar': !this.shouldShowSidebar,
        },
        userPageClass
      ]
E
tweaks  
Evan You 已提交
73 74 75
    }
  },

E
Evan You 已提交
76 77
  created () {
    if (this.$ssrContext) {
E
Evan You 已提交
78
      this.$ssrContext.title = getTitle(this.$site, this.$page)
E
Evan You 已提交
79 80 81 82
      this.$ssrContext.lang = getLang(this.$page)
    }
  },

E
Evan You 已提交
83
  mounted () {
E
Evan You 已提交
84 85 86
    // update title / meta tags
    this.currentMetaTags = []
    const updateMeta = () => {
E
Evan You 已提交
87
      document.title = getTitle(this.$site, this.$page)
E
Evan You 已提交
88 89 90 91 92 93 94
      document.documentElement.lang = getLang(this.$page)
      this.currentMetaTags = updateMetaTags(this.$page, this.currentMetaTags)
    }
    this.$watch('$page', updateMeta)
    updateMeta()

    // configure progress bar
E
Evan You 已提交
95 96 97
    nprogress.configure({ showSpinner: false })

    this.$router.beforeEach((to, from, next) => {
98
      if (to.path !== from.path && !Vue.component(pathToComponentName(to.path))) {
E
Evan You 已提交
99 100
        nprogress.start()
      }
E
Evan You 已提交
101 102 103 104 105
      next()
    })

    this.$router.afterEach(() => {
      nprogress.done()
E
Evan You 已提交
106
      this.isSidebarOpen = false
E
Evan You 已提交
107
    })
E
Evan You 已提交
108
  },
E
Evan You 已提交
109 110 111 112 113

  beforeDestroy () {
    updateMetaTags(null, this.currentMetaTags)
  },

E
Evan You 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  methods: {
    toggleSidebar (to) {
      this.isSidebarOpen = typeof to === 'boolean' ? to : !this.isSidebarOpen
    },
    // side swipe
    onTouchStart (e) {
      this.touchStart = {
        x: e.changedTouches[0].clientX,
        y: e.changedTouches[0].clientY
      }
    },
    onTouchEnd (e) {
      const dx = e.changedTouches[0].clientX - this.touchStart.x
      const dy = e.changedTouches[0].clientY - this.touchStart.y
      if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 40) {
        if (dx > 0 && this.touchStart.x <= 80) {
          this.toggleSidebar(true)
        } else {
          this.toggleSidebar(false)
        }
      }
    }
E
Evan You 已提交
136 137
  }
}
E
Evan You 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

function updateMetaTags (page, current) {
  if (current) {
    current.forEach(c => {
      document.head.removeChild(c)
    })
  }
  const data = page && page.frontmatter.meta
  if (data) {
    return data.map(m => {
      const tag = document.createElement('meta')
      Object.keys(m).forEach(key => {
        tag.setAttribute(key, m[key])
      })
      document.head.appendChild(tag)
      return tag
    })
  }
}
E
Evan You 已提交
157 158
</script>

E
Evan You 已提交
159
<style src="prismjs/themes/prism-tomorrow.css"></style>
E
Evan You 已提交
160
<style src="./styles/theme.styl" lang="stylus"></style>