Layout.vue 4.9 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6
<template>
  <div
    class="theme-container"
    :class="pageClasses"
    @touchstart="onTouchStart"
    @touchend="onTouchEnd"
D
DCloud_LXH 已提交
7
    @keydown.ctrl="openSearch = true"
D
DCloud_LXH 已提交
8 9 10 11 12 13 14 15 16 17 18
  >
    <Navbar
      v-if="shouldShowNavbar"
      @toggle-sidebar="toggleSidebar"
    />

    <div
      class="sidebar-mask"
      @click="toggleSidebar(false)"
    />

D
DCloud_LXH 已提交
19 20 21 22 23 24 25 26 27 28 29 30
    <Sidebar
      :items="sidebarItems"
      @toggle-sidebar="toggleSidebar"
    >
      <template #top>
        <slot name="sidebar-top" />
      </template>
      <template #bottom>
        <slot name="sidebar-bottom" />
        <SiderBarBottom />
      </template>
    </Sidebar>
D
DCloud_LXH 已提交
31 32 33 34 35 36

    <Home v-if="$page.frontmatter.home" />

    <Page
      v-else
      :sidebar-items="sidebarItems"
37
      :style="pageStyle"
D
DCloud_LXH 已提交
38 39 40
    >
      <template #top>
        <slot name="page-top" />
D
DCloud_LXH 已提交
41
        <TocTop/>
D
DCloud_LXH 已提交
42 43 44
      </template>
      <template #bottom>
        <slot name="page-bottom" />
45
        <Footer />
D
DCloud_LXH 已提交
46 47
      </template>
    </Page>
D
DCloud_LXH 已提交
48 49

    <Toc />
D
DCloud_LXH 已提交
50 51 52 53 54 55 56 57
  </div>
</template>

<script>
import Home from '@theme/components/Home.vue'
import Navbar from '@theme/components/Navbar.vue'
import Page from '@theme/components/Page.vue'
import Sidebar from '@theme/components/Sidebar.vue'
58
import Footer from '@theme/components/Footer.vue';
D
DCloud_LXH 已提交
59
import SiderBarBottom from '../components/SiderBarBottom.vue';
D
DCloud_LXH 已提交
60
import Toc from '../components/Toc';
D
DCloud_LXH 已提交
61
import TocTop from '../components/Toc-top';
D
DCloud_LXH 已提交
62
import { resolveSidebarItems, forbidScroll, BaiduStat } from '../util'
D
DCloud_LXH 已提交
63
import navProvider from '../mixin/navProvider';
64
import toc from '../mixin/toc';
D
DCloud_LXH 已提交
65 66 67

export default {
  name: 'Layout',
68
  mixins: [ navProvider, toc ],
D
DCloud_LXH 已提交
69 70 71 72
  components: {
    Home,
    Page,
    Sidebar,
73
    Navbar,
D
DCloud_LXH 已提交
74
    Footer,
D
DCloud_LXH 已提交
75
    SiderBarBottom,
D
DCloud_LXH 已提交
76 77
    Toc,
    TocTop
D
DCloud_LXH 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  },
  data () {
    return {
      isSidebarOpen: false
    }
  },
  computed: {
    shouldShowNavbar () {
      const { themeConfig } = this.$site
      const { frontmatter } = this.$page
      if (
        frontmatter.navbar === false
        || themeConfig.navbar === false) {
        return false
      }
      return (
        this.$title
        || themeConfig.logo
        || themeConfig.repo
        || themeConfig.nav
        || this.$themeLocaleConfig.nav
      )
    },
    shouldShowSidebar () {
      const { frontmatter } = this.$page
      return (
        !frontmatter.home
        && frontmatter.sidebar !== false
        && this.sidebarItems.length
      )
    },
    sidebarItems () {
      return resolveSidebarItems(
        this.$page,
        this.$page.regularPath,
        this.$site,
        this.$localePath
      )
    },
    pageClasses () {
      const userPageClass = this.$page.frontmatter.pageClass
      return [
        {
          'no-navbar': !this.shouldShowNavbar,
          'sidebar-open': this.isSidebarOpen,
          'no-sidebar': !this.shouldShowSidebar
        },
        userPageClass
      ]
127 128 129 130 131 132 133
    },
    pageStyle () {
      const style = {};

      !this.visible && (style.paddingRight = '0px');

      return style;
D
DCloud_LXH 已提交
134 135 136
    }
  },
  mounted () {
D
DCloud_LXH 已提交
137
    BaiduStat()
D
DCloud_LXH 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    this.$router.afterEach(() => {
      this.isSidebarOpen = false
    })
    forbidScroll(this.isSidebarOpen)
    this.renderNavLinkState()
  },
  methods: {
    toggleSidebar (to) {
      this.isSidebarOpen = typeof to === 'boolean' ? to : !this.isSidebarOpen
      this.$emit('toggle-sidebar', 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)
        }
      }
    },
    renderNavLinkState() {
      this.$nextTick(() => {
        const navs = document.querySelectorAll('nav')
        const navLinks = []
        navs.forEach(nav => {
          nav.querySelectorAll('a').forEach(navLink => {
            if(navLink.className.indexOf('external') === -1) {
              navLinks.push(navLink)
            }
          })
        })
        
        navLinks.forEach((navLink,index) => {
          navLink.classList.remove('router-link-active')
          let href =  navLink.href.split('/')
          href = href[href.length - 2]
          const path = (this.$route.fullPath.match(/\/(\w+)+\//) || [])[1]
          if (path) {
            if (path === href) {
              navLink.classList.add('router-link-active')
            }
          } else {
            // 0 => PC
            // navLinks.length / 2 => mobile
            if( index === 0 || index === navLinks.length / 2) {
              navLink.classList.add('router-link-active')
            }
          }
        })
      })
    }
  },
  watch: {
    isSidebarOpen: forbidScroll,
D
DCloud_LXH 已提交
201
    $route() {
D
DCloud_LXH 已提交
202 203 204 205 206
      this.renderNavLinkState()
    }
  }
}
</script>