webview.js 1.0 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
const WEBVIEW_LISTENERS = [
  'close',
  'resize',
  'popGesture',
  'pullToRefresh',
  'titleNViewSearchInputChanged',
  'titleNViewSearchInputConfirmed',
  'titleNViewSearchInputClicked'
]

let id = 1

function parseWebviewStyle (path, windowOptions = {}) {
  return {
    titleNView: {
      autoBackButton: true,
      titleText: 'titleText'
    },
    uniNView: {
      path
    }
  }
}

function parseWindowOptions (windowOptions = {}, globalWindowOptions = {}) {
  // TODO
  return windowOptions
}

export function createWebview (path, {
  plus,
  __uniConfig
}, windowOptions) {
  return plus.webview.create(
    '',
    String(id++),
    parseWebviewStyle(
      path,
      parseWindowOptions(windowOptions, __uniConfig.window)
    ))
}

export function initWebview (webview, {
  UniJSServiceBridge
}) {
  // TODO subNVues
  WEBVIEW_LISTENERS.forEach(listener => {
    webview.addEventListener(listener, (e) => {
      UniJSServiceBridge.emit(listener, e)
    })
  })
}