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

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

D
DCloud_LXH 已提交
18 19 20 21 22 23 24 25 26 27 28 29
    <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 已提交
30 31 32 33 34 35 36 37 38 39 40 41

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

    <Page
      v-else
      :sidebar-items="sidebarItems"
    >
      <template #top>
        <slot name="page-top" />
      </template>
      <template #bottom>
        <slot name="page-bottom" />
42
        <Footer />
D
DCloud_LXH 已提交
43 44
      </template>
    </Page>
D
DCloud_LXH 已提交
45 46

    <DcloudSearchPage :options="algolia"/>
D
DCloud_LXH 已提交
47 48 49 50 51 52 53 54
  </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'
55
import Footer from '@theme/components/Footer.vue';
D
DCloud_LXH 已提交
56
import SiderBarBottom from '../components/SiderBarBottom.vue';
D
DCloud_LXH 已提交
57
import DcloudSearchPage from '../components/DcloudSearchPage';
D
DCloud_LXH 已提交
58
import { resolveSidebarItems, forbidScroll, BaiduStat } from '../util'
D
DCloud_LXH 已提交
59 60 61 62 63 64 65 66 67
import navProvider from '../mixin/navProvider';

export default {
  name: 'Layout',
  mixins: [ navProvider ],
  components: {
    Home,
    Page,
    Sidebar,
68
    Navbar,
D
DCloud_LXH 已提交
69
    Footer,
D
DCloud_LXH 已提交
70 71
    SiderBarBottom,
    DcloudSearchPage
D
DCloud_LXH 已提交
72 73 74 75 76 77 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
  },
  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
      ]
D
DCloud_LXH 已提交
121 122 123
    },
    algolia () {
      return this.$themeLocaleConfig.algolia || this.$site.themeConfig.algolia || {}
D
DCloud_LXH 已提交
124 125 126
    }
  },
  mounted () {
D
DCloud_LXH 已提交
127
    BaiduStat()
D
DCloud_LXH 已提交
128 129 130 131 132 133 134 135 136 137 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
    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 已提交
191
    $route() {
D
DCloud_LXH 已提交
192 193 194 195 196
      this.renderNavLinkState()
    }
  }
}
</script>