index.js 1.7 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
import {
  isPlainObject
}
  from 'uni-shared'

import {
  parseWebviewStyle
} from './parser/webview-style-parser'

let id = 2

const WEBVIEW_LISTENERS = {
  'pullToRefresh': 'onPullDownRefresh',
  'titleNViewSearchInputChanged': 'onNavigationBarSearchInputChanged',
  'titleNViewSearchInputConfirmed': 'onNavigationBarSearchInputConfirmed',
  'titleNViewSearchInputClicked': 'onNavigationBarSearchInputClicked'
}

fxy060608's avatar
fxy060608 已提交
19
export function createWebview (path, routeOptions) {
fxy060608's avatar
fxy060608 已提交
20 21 22 23
  const webviewId = id++
  const webviewStyle = parseWebviewStyle(
    webviewId,
    path,
fxy060608's avatar
fxy060608 已提交
24
    routeOptions
fxy060608's avatar
fxy060608 已提交
25 26 27 28
  )
  if (process.env.NODE_ENV !== 'production') {
    console.log(`[uni-app] createWebview`, webviewId, path, webviewStyle)
  }
fxy060608's avatar
fxy060608 已提交
29
  const webview = plus.webview.create('', String(webviewId), webviewStyle)
fxy060608's avatar
fxy060608 已提交
30 31 32 33

  return webview
}

fxy060608's avatar
fxy060608 已提交
34
export function initWebview (webview, routeOptions) {
fxy060608's avatar
fxy060608 已提交
35 36 37 38
  if (isPlainObject(routeOptions)) {
    const webviewStyle = parseWebviewStyle(
      parseInt(webview.id),
      '',
fxy060608's avatar
fxy060608 已提交
39
      routeOptions
fxy060608's avatar
fxy060608 已提交
40 41 42 43 44 45 46
    )
    if (process.env.NODE_ENV !== 'production') {
      console.log(`[uni-app] updateWebview`, webviewStyle)
    }

    webview.setStyle(webviewStyle)
  }
fxy060608's avatar
fxy060608 已提交
47 48 49 50

  const {
    on,
    emit
fxy060608's avatar
fxy060608 已提交
51
  } = UniServiceJSBridge
fxy060608's avatar
fxy060608 已提交
52

fxy060608's avatar
fxy060608 已提交
53 54 55
  // TODO subNVues
  Object.keys(WEBVIEW_LISTENERS).forEach(name => {
    webview.addEventListener(name, (e) => {
fxy060608's avatar
fxy060608 已提交
56
      emit(WEBVIEW_LISTENERS[name], e, parseInt(webview.id))
fxy060608's avatar
fxy060608 已提交
57 58 59
    })
  })

fxy060608's avatar
fxy060608 已提交
60 61 62 63 64 65
  // TODO 应该结束之前未完成的下拉刷新
  on(webview.id + '.startPullDownRefresh', () => {
    webview.beginPullToRefresh()
  })

  on(webview.id + '.stopPullDownRefresh', () => {
fxy060608's avatar
fxy060608 已提交
66 67 68 69 70
    webview.endPullToRefresh()
  })

  return webview
}