Layout.vue 3.8 KB
Newer Older
E
Evan You 已提交
1
<template>
E
Evan You 已提交
2
  <div class="theme-container"
E
Evan You 已提交
3
    :class="{
E
tweaks  
Evan You 已提交
4
      'no-navbar': !shouldShowNavbar,
E
Evan You 已提交
5
      'sidebar-open': isSidebarOpen,
E
tweaks  
Evan You 已提交
6
      'no-sidebar': !shouldShowSidebar
E
Evan You 已提交
7
    }"
E
Evan You 已提交
8 9
    @touchstart="onTouchStart"
    @touchend="onTouchEnd">
E
tweaks  
Evan You 已提交
10
    <Navbar v-if="shouldShowNavbar" @toggle-sidebar="toggleSidebar"/>
E
Evan You 已提交
11
    <Sidebar :items="sidebarItems" @toggle-sidebar="toggleSidebar"/>
E
Evan You 已提交
12 13 14 15
    <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 已提交
16
    <Page v-else :sidebar-items="sidebarItems"/>
E
Evan You 已提交
17 18 19 20
  </div>
</template>

<script>
21
import Vue from 'vue'
E
Evan You 已提交
22
import nprogress from 'nprogress'
E
Evan You 已提交
23
import Home from './Home.vue'
E
Evan You 已提交
24
import Navbar from './Navbar.vue'
E
Evan You 已提交
25
import Page from './Page.vue'
E
Evan You 已提交
26
import Sidebar from './Sidebar.vue'
E
Evan You 已提交
27
import { pathToComponentName, getTitle, getLang } from '../app/util'
E
Evan You 已提交
28
import { resolveSidebarItems } from './util'
E
Evan You 已提交
29

E
Evan You 已提交
30
export default {
E
Evan You 已提交
31
  components: { Home, Page, Sidebar, Navbar },
E
Evan You 已提交
32 33 34 35 36
  data () {
    return {
      isSidebarOpen: false
    }
  },
E
Evan You 已提交
37

E
tweaks  
Evan You 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51
  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 已提交
52 53 54 55 56
        !frontmatter.layout &&
        !frontmatter.home &&
        frontmatter.sidebar !== false && (
          frontmatter.sidebar === 'auto' ||
          themeConfig.sidebar
E
Evan You 已提交
57
        )
E
tweaks  
Evan You 已提交
58
      )
E
Evan You 已提交
59 60 61 62 63 64 65
    },
    sidebarItems () {
      return resolveSidebarItems(
        this.$page,
        this.$route,
        this.$site
      )
E
tweaks  
Evan You 已提交
66 67 68
    }
  },

E
Evan You 已提交
69 70
  created () {
    if (this.$ssrContext) {
E
Evan You 已提交
71
      this.$ssrContext.title = getTitle(this.$site, this.$page)
E
Evan You 已提交
72 73 74 75
      this.$ssrContext.lang = getLang(this.$page)
    }
  },

E
Evan You 已提交
76
  mounted () {
E
Evan You 已提交
77 78 79
    // update title / meta tags
    this.currentMetaTags = []
    const updateMeta = () => {
E
Evan You 已提交
80
      document.title = getTitle(this.$site, this.$page)
E
Evan You 已提交
81 82 83 84 85 86 87
      document.documentElement.lang = getLang(this.$page)
      this.currentMetaTags = updateMetaTags(this.$page, this.currentMetaTags)
    }
    this.$watch('$page', updateMeta)
    updateMeta()

    // configure progress bar
E
Evan You 已提交
88 89 90
    nprogress.configure({ showSpinner: false })

    this.$router.beforeEach((to, from, next) => {
91
      if (to.path !== from.path && !Vue.component(pathToComponentName(to.path))) {
E
Evan You 已提交
92 93
        nprogress.start()
      }
E
Evan You 已提交
94 95 96 97 98
      next()
    })

    this.$router.afterEach(() => {
      nprogress.done()
E
Evan You 已提交
99
      this.isSidebarOpen = false
E
Evan You 已提交
100
    })
E
Evan You 已提交
101
  },
E
Evan You 已提交
102 103 104 105 106

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

E
Evan You 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  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 已提交
129 130
  }
}
E
Evan You 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

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 已提交
150 151
</script>

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