webview-style-parser.js 1.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
import {
  parseTitleNView
} from './title-nview-parser'

import {
  parsePullToRefresh
} from './pull-to-refresh-parser'

fxy060608's avatar
fxy060608 已提交
9 10 11 12
import {
  TABBAR_HEIGHT
} from '../../../constants'

fxy060608's avatar
fxy060608 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
const WEBVIEW_STYLE_BLACKLIST = [
  'navigationBarBackgroundColor',
  'navigationBarTextStyle',
  'navigationBarTitleText',
  'navigationBarShadow',
  'navigationStyle',
  'disableScroll',
  'backgroundColor',
  'backgroundTextStyle',
  'enablePullDownRefresh',
  'onReachBottomDistance',
  'usingComponents',
  // 需要解析的
  'titleNView',
  'pullToRefresh'
]

fxy060608's avatar
fxy060608 已提交
30
export function parseWebviewStyle (id, path, routeOptions = {}) {
fxy060608's avatar
fxy060608 已提交
31 32 33
  const webviewStyle = Object.create(null)

  // 合并
fxy060608's avatar
fxy060608 已提交
34
  routeOptions.window = Object.assign(
fxy060608's avatar
fxy060608 已提交
35 36 37 38
    JSON.parse(JSON.stringify(__uniConfig.window || {})),
    routeOptions.window || {}
  )

fxy060608's avatar
fxy060608 已提交
39
  Object.keys(routeOptions.window).forEach(name => {
fxy060608's avatar
fxy060608 已提交
40
    if (WEBVIEW_STYLE_BLACKLIST.indexOf(name) === -1) {
fxy060608's avatar
fxy060608 已提交
41
      webviewStyle[name] = routeOptions.window[name]
fxy060608's avatar
fxy060608 已提交
42 43 44 45 46 47 48 49 50 51 52
    }
  })

  const titleNView = parseTitleNView(routeOptions)
  if (titleNView) {
    if (id === 1 && __uniConfig.realEntryPagePath === path) {
      titleNView.autoBackButton = true
    }
    webviewStyle.titleNView = titleNView
  }

fxy060608's avatar
fxy060608 已提交
53
  const pullToRefresh = parsePullToRefresh(routeOptions)
fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  if (pullToRefresh) {
    if (pullToRefresh.style === 'circle') {
      webviewStyle.bounce = 'none'
    }
    webviewStyle.pullToRefresh = pullToRefresh
  }

  // 不支持 hide
  if (webviewStyle.popGesture === 'hide') {
    delete webviewStyle.popGesture
  }

  // TODO 下拉刷新

  if (path && routeOptions.meta.isNVue) {
    webviewStyle.uniNView = {
      path,
      defaultFontSize: __uniConfig.defaultFontSize,
      viewport: __uniConfig.viewport
    }
  }

fxy060608's avatar
fxy060608 已提交
76 77 78 79 80
  if (routeOptions.meta.isTabBar) {
    webviewStyle.top = 0
    webviewStyle.bottom = TABBAR_HEIGHT
  }

fxy060608's avatar
fxy060608 已提交
81 82
  return webviewStyle
}