index.js 3.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import {
  isFn,
  isPlainObject,
  supportsPassive
} from 'uni-shared'

import {
  NAVBAR_HEIGHT,
  TABBAR_HEIGHT
} from 'uni-helpers/constants'

import {
  pageScrollTo,
  disableScroll,
  createScrollListener
} from './scroll'

import requestComponentInfo from './request-component-info'

20 21
import { requestComponentObserver, destroyComponentObserver } from './request-component-observer'

fxy060608's avatar
fxy060608 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
const passiveOptions = supportsPassive ? {
  passive: false
} : false

function updateCssVar (vm) {
  if (uni.canIUse('css.var')) {
    const pageVm = vm.$parent.$parent
    const windowTop = pageVm.showNavigationBar && pageVm.navigationBar.type !== 'transparent' ? (NAVBAR_HEIGHT + 'px')
      : '0px'
    const windowBottom = getApp().$children[0].showTabBar ? (TABBAR_HEIGHT + 'px') : '0px'
    const style = document.documentElement.style
    style.setProperty('--window-top', windowTop)
    style.setProperty('--window-bottom', windowBottom)
    console.debug(`${vm.$page.route}[${vm.$page.id}]:--window-top=${windowTop}`)
    console.debug(`${vm.$page.route}[${vm.$page.id}]:--window-bottom=${windowBottom}`)
  }
}

export default function initSubscribe (subscribe) {
  subscribe('requestComponentInfo', requestComponentInfo)

  subscribe('pageScrollTo', pageScrollTo)

45 46 47
  subscribe('requestComponentObserver', requestComponentObserver)
  subscribe('destroyComponentObserver', destroyComponentObserver)

fxy060608's avatar
fxy060608 已提交
48 49 50 51 52 53 54 55
  if (__PLATFORM__ === 'h5') {
    let scrollListener = false
    let disableScrollListener = false

    subscribe('onPageLoad', vm => { // 用户 onLoad 之前 update
      updateCssVar(vm)
    })

56
    subscribe('onPageShow', vm => {
fxy060608's avatar
fxy060608 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      const pageVm = vm.$parent.$parent

      if (vm._isMounted) { // 非首次 show 才 update(首次 show 的时候在 onPageLoad 中触发了)
        updateCssVar(vm)
      }

      if (disableScrollListener) {
        document.removeEventListener('touchmove', disableScrollListener, passiveOptions)
      }

      if (pageVm.disableScroll) {
        disableScrollListener = disableScroll
        document.addEventListener('touchmove', disableScrollListener, passiveOptions)
      }

      const enablePageScroll = isFn(vm.$options.onPageScroll)
      const enablePageReachBottom = isFn(vm.$options.onReachBottom)
      const onReachBottomDistance = pageVm.onReachBottomDistance

      const enableTransparentTitleNView = isPlainObject(pageVm.titleNView) && pageVm.titleNView.type === 'transparent'

      if (scrollListener) {
        document.removeEventListener('scroll', scrollListener)
      }

      if (enableTransparentTitleNView || enablePageScroll || enablePageReachBottom) { // 初始化 scroll 监听
        scrollListener = createScrollListener(vm.$page.id, {
          enablePageScroll,
          enablePageReachBottom,
          onReachBottomDistance,
          enableTransparentTitleNView
        })
        setTimeout(function () { // 避免监听太早,直接触发了 scroll
          document.addEventListener('scroll', scrollListener)
        }, 10)
      }
    })
  }
95
}