mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
53
54
55
56
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
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
124
import { isPlainObject } from 'uni-shared'
 
import { initNavigationBarI18n } from 'uni-helpers/i18n'
 
function createButtonOnClick (index) {
  return function onClick (btn) {
    const pages = getCurrentPages()
    if (!pages.length) {
      return
    }
    btn.index = index
    const page = pages[pages.length - 1]
    page.$vm &&
      page.$vm.__call_hook &&
      page.$vm.__call_hook('onNavigationBarButtonTap', btn)
  }
}
 
function parseTitleNViewButtons (titleNView) {
  const buttons = titleNView.buttons
  if (!Array.isArray(buttons)) {
    return titleNView
  }
  buttons.forEach((btn, index) => {
    btn.onclick = createButtonOnClick(index)
  })
  return titleNView
}
 
export function parseTitleNView (id, routeOptions) {
  const windowOptions = routeOptions.window
  const titleNView = windowOptions.titleNView
  routeOptions.meta.statusBarStyle =
    windowOptions.navigationBarTextStyle === 'white' ? 'light' : 'dark'
  if (
    // 无头
    titleNView === false ||
    titleNView === 'false' ||
    (windowOptions.navigationStyle === 'custom' &&
      !isPlainObject(titleNView)) ||
    (windowOptions.transparentTitle === 'always' && !isPlainObject(titleNView))
  ) {
    return false
  }
 
  const titleImage = windowOptions.titleImage || ''
  const transparentTitle = windowOptions.transparentTitle || 'none'
  const titleNViewTypeList = {
    none: 'default',
    auto: 'transparent',
    always: 'float'
  }
 
  const navigationBarBackgroundColor =
    windowOptions.navigationBarBackgroundColor
  const ret = {
    autoBackButton: !routeOptions.meta.isQuit,
    titleText:
      titleImage === '' ? windowOptions.navigationBarTitleText || '' : '',
    titleColor:
      windowOptions.navigationBarTextStyle === 'white' ? '#ffffff' : '#000000',
    type: titleNViewTypeList[transparentTitle],
    backgroundColor:
      /^#[a-z0-9]{6}$/i.test(navigationBarBackgroundColor) ||
      navigationBarBackgroundColor === 'transparent'
        ? navigationBarBackgroundColor
        : '#f8f8f8',
    tags:
      titleImage === ''
        ? []
        : [
          {
            tag: 'img',
            src: titleImage,
            position: {
              left: 'auto',
              top: 'auto',
              width: 'auto',
              height: '26px'
            }
          }
        ]
  }
 
  if (isPlainObject(titleNView)) {
    return initTitleNViewI18n(
      id,
      Object.assign(ret, parseTitleNViewButtons(titleNView))
    )
  }
  return initTitleNViewI18n(id, ret)
}
 
function initTitleNViewI18n (id, titleNView) {
  const i18nResult = initNavigationBarI18n(titleNView)
  if (!i18nResult) {
    return titleNView
  }
  const [titleTextI18n, searchInputPlaceholderI18n] = i18nResult
  if (titleTextI18n || searchInputPlaceholderI18n) {
    uni.onLocaleChange(() => {
      const webview = plus.webview.getWebviewById(id + '')
      if (!webview) {
        return
      }
      const newTitleNView = {}
      if (titleTextI18n) {
        newTitleNView.titleText = titleNView.titleText
      }
      if (searchInputPlaceholderI18n) {
        newTitleNView.searchInput = {
          placeholder: titleNView.searchInput.placeholder
        }
      }
      if (process.env.NODE_ENV !== 'production') {
        console.log('[uni-app] updateWebview', webview.id, newTitleNView)
      }
      webview.setStyle({
        titleNView: newTitleNView
      })
    })
  }
  return titleNView
}