Navbar.vue 8.7 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5
<template>
  <header class="navbar" :class="{ 'navbar-fixed': fixedNavbar }">
    <div class="main-navbar">
      <!-- <SidebarButton @toggle-sidebar="$emit('toggle-sidebar')" /> -->

D
DCloud_LXH 已提交
6
      <NavbarLogo />
D
DCloud_LXH 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

      <div class="main-navbar-links can-hide">
        <template v-for="(item, index) in customNavBar">
          <div :class="mainNavLinkClass(index)" :key="item.text">
            <MainNavbarLink v-if="item.type" :item='item' />
            <a v-else href="javascript:;" @click="changeUserNav(index)">{{item.text}}</a>
          </div>
        </template>
      </div>

      <div class="mobile-main-navbar">
        <div class="mobile-links_mobile">
          <a href="javascript:;" class="mobile-links__btn" @click="toggleMobilePanel">{{mainNavBarText}}</a>
        </div>
        <div class="mobile-links__panel" :class="{open: showMobilePanel}">
          <template v-for="(item, index) in customNavBar">
            <div :class="mainNavLinkClass(index)" :key="item.text">
              <MainNavbarLink v-if="item.type" :item='item' @click.native="toggleMobilePanel"/>
              <a v-else href="javascript:;" @click="changeUserNav(index),toggleMobilePanel()">{{item.text}}</a>
            </div>
          </template>
        </div>
      </div>

      <div
        class="links"
        :style="linksWrapMaxWidth ? {
          'max-width': linksWrapMaxWidth + 'px'
        } : {}"
      >
D
DCloud_LXH 已提交
37
        <!-- <a class="switch-version" href="javascript:void(0)">回到旧版</a> -->
D
DCloud_LXH 已提交
38 39
        <DcloudSearchPage v-if="isAlgoliaSearch" ref="dcloudSearchPage" :options="algolia"/>
        <AlgoliaSearchBox v-if="isAlgoliaSearch" />
D
DCloud_LXH 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        <SearchBox v-else-if="$site.themeConfig.search !== false && $page.frontmatter.search !== false" />
      </div>
    </div>

    <div class="sub-navbar">
      <NavLinks class="can-hide" />
      <div class="mobile-sub-navbar">
        <div class="subnavbar__item" @click="$emit('toggle-sidebar')">
          <a href="javascript:;">{{subNavBarText}}</a>
        </div>
      </div>
    </div>
  </header>
</template>

<script>
D
DCloud_LXH 已提交
56
import AlgoliaSearchBox from './AlgoliaSearchBox'
D
DCloud_LXH 已提交
57
import SearchBox from './SearchBox'
D
DCloud_LXH 已提交
58 59 60
import SidebarButton from '@theme/components/SidebarButton.vue'
import NavLinks from '@theme/components/NavLinks.vue'
import MainNavbarLink from './MainNavbarLink.vue';
D
DCloud_LXH 已提交
61
import NavbarLogo from './NavbarLogo.vue';
D
DCloud_LXH 已提交
62
import DcloudSearchPage from './DcloudSearchPage';
D
DCloud_LXH 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
import navInject from '../mixin/navInject';
import { forbidScroll, os } from '../util';

export default {
  name: 'Navbar',

  mixins: [ navInject ],

  components: {
    SidebarButton,
    NavLinks,
    MainNavbarLink,
    SearchBox,
D
DCloud_LXH 已提交
76
    AlgoliaSearchBox,
D
DCloud_LXH 已提交
77 78
    NavbarLogo,
    DcloudSearchPage
D
DCloud_LXH 已提交
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
  },

  data () {
    return {
      linksWrapMaxWidth: null,
      showMobilePanel: false,
      fixedNavbar: false
    }
  },

  computed: {
    algolia () {
      return this.$themeLocaleConfig.algolia || this.$site.themeConfig.algolia || {}
    },

    isAlgoliaSearch () {
      return this.algolia && this.algolia.apiKey && this.algolia.indexName
    }
  },

  mounted () {
    const MOBILE_DESKTOP_BREAKPOINT = 719 // refer to config.styl
    const NAVBAR_VERTICAL_PADDING = parseInt(css(this.$el, 'paddingLeft')) + parseInt(css(this.$el, 'paddingRight'))
    const handleLinksWrapWidth = () => {
      if (document.documentElement.clientWidth < MOBILE_DESKTOP_BREAKPOINT) {
        this.linksWrapMaxWidth = null
      } else {
        this.linksWrapMaxWidth = this.$el.offsetWidth - NAVBAR_VERTICAL_PADDING
          - (this.$refs.siteName && this.$refs.siteName.offsetWidth || 0)
      }
      this.$nextTick(this.initNavBar)
    }
    handleLinksWrapWidth()
    window.addEventListener('resize', handleLinksWrapWidth, false)
    this.initNavBar()
  },

  methods: {
    initNavBar () {
      os.init()
      this.sideBar = document.querySelector('.sidebar')
      this.navbar = document.querySelector('.navbar')
      this.mainNavBar = document.querySelector('.main-navbar')
      this.subNavBar = document.querySelector('.sub-navbar')
      this.pageContainer = document.querySelector('.page')
D
DCloud_LXH 已提交
124
      this.vuepressToc = document.querySelector('.vuepress-toc')
D
DCloud_LXH 已提交
125 126 127
      this.navbarHeight = this.navbar.clientHeight
      this.subNavBarHeight = this.subNavBar.clientHeight
      this.mainNavBarHeight = this.mainNavBar.clientHeight
128 129 130
      this.scrollBehavior()
    },
    scrollBehavior () {
D
DCloud_LXH 已提交
131
      this.removeWindowScroll()
132 133 134 135 136 137 138 139
      this.onWindowScroll()
      if(this.showSubNavBar) {
        this.addWindowScroll()
      } else {
        this.fixedNavbar = true
      }
    },
    addWindowScroll () {
D
DCloud_LXH 已提交
140 141 142 143 144 145 146 147
      if (os.pc) {
        window.addEventListener('scroll', this.onWindowScroll, false)
      }
    },
    removeWindowScroll () {
      window.removeEventListener('scroll', this.onWindowScroll)
      this.fixedNavbar = false
      this.sideBar && this.sideBar.removeAttribute('style')
D
DCloud_LXH 已提交
148
      this.vuepressToc && this.vuepressToc.removeAttribute('style')
D
DCloud_LXH 已提交
149
      this.navbar && this.navbar.removeAttribute('style')
150 151 152
      if (this.pageContainer) {
        this.pageContainer.style.marginTop = this.showSubNavBar ? 'auto' : `${this.navbarHeight}px`
      }
D
DCloud_LXH 已提交
153 154
    },
    onWindowScroll () {
155
      const scrollTop = !this.showSubNavBar ? 0 : document.documentElement.scrollTop || document.body.scrollTop;
D
DCloud_LXH 已提交
156

157
      if (!this.fixedNavbar) {
D
DCloud_LXH 已提交
158 159
        let sideTop = this.navbarHeight - scrollTop
        sideTop <= this.subNavBarHeight && (sideTop = this.subNavBarHeight)
160
        this.sideBar && (this.sideBar.style.top = `${sideTop + 1}px`)
D
DCloud_LXH 已提交
161
        this.vuepressToc && (this.vuepressToc.style.top = `${sideTop + 1}px`)
D
DCloud_LXH 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174
      }

      if (scrollTop >= this.mainNavBarHeight) {
        if (!this.fixedNavbar) {
          this.fixedNavbar = true
          this.navbar.style.top = `-${this.mainNavBarHeight}px`
          this.$nextTick(() => {
            this.pageContainer && (this.pageContainer.style.marginTop = `${this.navbarHeight}px`)
          })
        }
      } else if (scrollTop < this.mainNavBarHeight) {
        if (this.fixedNavbar) {
          this.fixedNavbar = false
175
          this.pageContainer && (this.pageContainer.style.marginTop = 'auto')
D
DCloud_LXH 已提交
176 177 178
        }
      }
    },
179 180
    fixedSideBarHeight () {
      if(!os.pc) return
181
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
182 183
      this.navbarHeight = this.navbar.clientHeight
      this.subNavBarHeight = this.subNavBar.clientHeight
184
      const setHeight = !!scrollTop || this.fixedNavbar ? this.subNavBarHeight : this.navbarHeight
185 186 187
      this.sideBar.style.top = `${setHeight + 1}px`
      this.vuepressToc.style.top = `${setHeight + 1}px`
    },
D
DCloud_LXH 已提交
188 189 190 191 192 193
    mainNavLinkClass (index) {
      return ['main-navbar-item', this.navConfig.userNavIndex === index ? 'active' : '']
    },
    toggleMobilePanel () {
      this.showMobilePanel = !this.showMobilePanel
      forbidScroll(this.showMobilePanel)
D
DCloud_LXH 已提交
194 195
    },
    switchVersion () {
D
DCloud_LXH 已提交
196 197
      document.cookie = encodeURIComponent('__old_version') + "=__old_version; path=/"
      // document.cookie = encodeURIComponent('__new_version') + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/"
198
      location.replace(location.origin + '?v=' + Date.now())
D
DCloud_LXH 已提交
199 200 201 202
    }
  },

  watch: {
203 204
    fixedNavbar () {
      this.fixedSideBarHeight()
205
      this.scrollBehavior()
206
    },
D
DCloud_LXH 已提交
207 208
    'navConfig.userNavIndex' () {
      this.$nextTick(()=>{
209
        this.fixedSideBarHeight()
210
        this.scrollBehavior()
D
DCloud_LXH 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
      })
    }
  }
}

function css (el, property) {
  // NOTE: Known bug, will return 'auto' if style value is 'auto'
  const win = el.ownerDocument.defaultView
  // null means not to return pseudo styles
  return win.getComputedStyle(el, null)[property]
}
</script>

<style lang="stylus">
$navbar-vertical-padding = 0.7rem
$navbar-horizontal-padding = 1.5rem
@import '../styles/navbar'

.navbar
  // padding $navbar-vertical-padding $navbar-horizontal-padding
  line-height $navbar-main-navbar-height // $navbarHeight - 1.4rem
  a, span, img
    display inline-block
  .title-logo
  .logo
    height $navbar-logo-height // $navbarHeight - 1.4rem
    min-width $navbar-main-navbar-height // $navbarHeight - 1.4rem
    margin-right 0.8rem
    vertical-align top
  .site-name
    font-size 1.3rem
    font-weight 600
    color $textColor
    position relative
  .links
D
DCloud_LXH 已提交
246
    height 100%
D
DCloud_LXH 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    padding-left 1.5rem
    box-sizing border-box
    background-color white
    white-space nowrap
    font-size 0.9rem
    position absolute
    right $navbar-horizontal-padding
    top 0 //$navbar-vertical-padding
    display flex
    .search-box
      flex: 0 0 auto
      vertical-align top

@media (max-width: $MQMobile)
  .navbar
    // padding-left 4rem
    .can-hide
      display none !important
    .links
      padding-left 0rem // 1.5rem
    .site-name
      width calc(100vw - 9.4rem)
      overflow hidden
      white-space nowrap
      text-overflow ellipsis
</style>