提交 aadf4be6 编写于 作者: fxy060608's avatar fxy060608

feat(app-plus-nvue): add lifecycle

上级 ccc71a43
......@@ -17,7 +17,7 @@ if (process.env.UNI_SERVICE === 'legacy') {
input = 'src/platforms/app-plus/service/uni/index.js'
output.file = 'packages/uni-app-plus-nvue/dist/uni.js'
output.banner =
'export function createUniInstance(weex, plus, __uniConfig, __uniRoutes, getApp, getCurrentPages){\n'
'export function createUniInstance(weex, plus, __uniConfig, __uniRoutes, UniServiceJSBridge, getApp, getCurrentPages){\n'
output.footer = '\n return uni$1 \n}'
}
......@@ -35,6 +35,7 @@ module.exports = {
}),
replace({
__GLOBAL__: 'getGlobalUni()',
__PLATFORM__: JSON.stringify(process.env.UNI_PLATFORM),
__PLATFORM_TITLE__: 'app-plus-nvue'
})
],
......
......@@ -5,16 +5,16 @@
"lint": "eslint --fix --config package.json --ext .js --ext .vue --ignore-path .eslintignore build src",
"dev:h5": "npm run lint && cross-env NODE_ENV=production UNI_WATCH=true UNI_PLATFORM=h5 node build/build.js",
"build:h5": "npm run lint && cross-env NODE_ENV=production UNI_WATCH=false UNI_PLATFORM=h5 node build/build.js",
"build:app-plus": "cross-env UNI_PLATFORM=app-plus rollup -c build/rollup.config.js",
"build:uni:all": "npm run lint && npm run build:uni && npm run build:uni:nvue && npm run build:uni:legacy",
"build:uni": "UNI_PLATFORM=app-plus-nvue UNI_SERVICE=uni rollup -c build/rollup.config.service.js",
"build:uni:nvue": "cross-env UNI_PLATFORM=app-plus-nvue rollup -c build/rollup.config.service.js",
"build:uni:legacy": "cross-env UNI_PLATFORM=app-plus-nvue UNI_SERVICE=legacy rollup -c build/rollup.config.service.js",
"build:mp-qq": "cross-env UNI_PLATFORM=mp-qq rollup -c build/rollup.config.js",
"build:mp-weixin": "cross-env UNI_PLATFORM=mp-weixin rollup -c build/rollup.config.js",
"build:mp-baidu": "cross-env UNI_PLATFORM=mp-baidu rollup -c build/rollup.config.js",
"build:mp-alipay": "cross-env UNI_PLATFORM=mp-alipay rollup -c build/rollup.config.js",
"build:mp-toutiao": "cross-env UNI_PLATFORM=mp-toutiao rollup -c build/rollup.config.js",
"build:app-plus": "cross-env UNI_PLATFORM=app-plus rollup -c build/rollup.config.mp.js",
"build:app:all": "npm run lint && npm run build:app && npm run build:app:nvue && npm run build:app:legacy",
"build:app": "UNI_PLATFORM=app-plus-nvue UNI_SERVICE=uni rollup -c build/rollup.config.app.js",
"build:app:nvue": "cross-env UNI_PLATFORM=app-plus-nvue rollup -c build/rollup.config.app.js",
"build:app:legacy": "cross-env UNI_PLATFORM=app-plus-nvue UNI_SERVICE=legacy rollup -c build/rollup.config.app.js",
"build:mp-qq": "cross-env UNI_PLATFORM=mp-qq rollup -c build/rollup.config.mp.js",
"build:mp-weixin": "cross-env UNI_PLATFORM=mp-weixin rollup -c build/rollup.config.mp.js",
"build:mp-baidu": "cross-env UNI_PLATFORM=mp-baidu rollup -c build/rollup.config.mp.js",
"build:mp-alipay": "cross-env UNI_PLATFORM=mp-alipay rollup -c build/rollup.config.mp.js",
"build:mp-toutiao": "cross-env UNI_PLATFORM=mp-toutiao rollup -c build/rollup.config.mp.js",
"build:runtime": "npm run lint && npm run build:mp-weixin && npm run build:mp-qq && npm run build:mp-alipay && npm run build:mp-baidu && npm run build:mp-toutiao && npm run build:app-plus",
"test:unit": "cross-env NODE_ENV=test UNI_PLATFORM=h5 mocha-webpack --require tests/unit/setup.js --webpack-config build/webpack.config.test.js tests/unit/**/*.spec.js"
},
......
export function createUniInstance(weex, plus, __uniConfig, __uniRoutes, getApp, getCurrentPages){
export function createUniInstance(weex, plus, __uniConfig, __uniRoutes, UniServiceJSBridge, getApp, getCurrentPages){
const _toString = Object.prototype.toString;
const hasOwnProperty = Object.prototype.hasOwnProperty;
......@@ -1027,10 +1027,6 @@ const showLoading = {
visible: {
type: Boolean,
default: true
},
isShowLoading: {
type: Boolean,
default: true
}
};
......@@ -1860,14 +1856,117 @@ function switchTab$1 (args) {
return onAppRoute('switchTab', args)
}
function pageScrollTo$1 (args) {
const pages = getCurrentPages();
if (pages.length) {
UniServiceJSBridge.publishHandler('pageScrollTo', args, pages[pages.length - 1].$page.id);
}
return {}
}
let pageId;
function startPullDownRefresh () {
if (pageId) {
UniServiceJSBridge.emit(pageId + '.stopPullDownRefresh', {}, pageId);
}
const pages = getCurrentPages();
if (pages.length) {
pageId = pages[pages.length - 1].$page.id;
UniServiceJSBridge.emit(pageId + '.startPullDownRefresh', {}, pageId);
}
return {}
}
function stopPullDownRefresh () {
if (pageId) {
UniServiceJSBridge.emit(pageId + '.stopPullDownRefresh', {}, pageId);
pageId = null;
} else {
const pages = getCurrentPages();
if (pages.length) {
pageId = pages[pages.length - 1].$page.id;
UniServiceJSBridge.emit(pageId + '.stopPullDownRefresh', {}, pageId);
}
}
return {}
}
function setNavigationBar (type, args) {
const pages = getCurrentPages();
if (pages.length) {
const page = pages[pages.length - 1].$holder;
switch (type) {
case 'setNavigationBarColor':
const {
frontColor,
backgroundColor,
animation
} = args;
const {
duration,
timingFunc
} = animation;
if (frontColor) {
page.navigationBar.textColor = frontColor === '#000000' ? 'black' : 'white';
}
if (backgroundColor) {
page.navigationBar.backgroundColor = backgroundColor;
}
page.navigationBar.duration = duration + 'ms';
page.navigationBar.timingFunc = timingFunc;
break
case 'showNavigationBarLoading':
page.navigationBar.loading = true;
break
case 'hideNavigationBarLoading':
page.navigationBar.loading = false;
break
case 'setNavigationBarTitle':
const {
title
} = args;
page.navigationBar.titleText = title;
break
}
}
return {}
}
function setNavigationBarColor$1 (args) {
return setNavigationBar('setNavigationBarColor', args)
}
function showNavigationBarLoading () {
return setNavigationBar('showNavigationBarLoading')
}
function hideNavigationBarLoading () {
return setNavigationBar('hideNavigationBarLoading')
}
function setNavigationBarTitle$1 (args) {
return setNavigationBar('setNavigationBarTitle', args)
}
var api = /*#__PURE__*/Object.freeze({
pageScrollTo: pageScrollTo$1,
startPullDownRefresh: startPullDownRefresh,
stopPullDownRefresh: stopPullDownRefresh,
redirectTo: redirectTo$1,
navigateTo: navigateTo$1,
navigateBack: navigateBack$1,
reLaunch: reLaunch$1,
switchTab: switchTab$1
switchTab: switchTab$1,
setNavigationBarColor: setNavigationBarColor$1,
showNavigationBarLoading: showNavigationBarLoading,
hideNavigationBarLoading: hideNavigationBarLoading,
setNavigationBarTitle: setNavigationBarTitle$1
});
const uni$1 = Object.create(null);
......
<meta charset="utf-8">
<title>index demo</title>
<script src="./index.umd.js"></script>
<link rel="stylesheet" href="./index.css">
<script>
console.log(index)
</script>
此差异已折叠。
此差异已折叠。
因为 它太大了无法显示 source diff 。你可以改为 查看blob
function setNavigationBar (type, args) {
const pages = getCurrentPages()
if (pages.length) {
const page = pages[pages.length - 1].$parent.$parent
const page = pages[pages.length - 1].$holder
switch (type) {
case 'setNavigationBarColor':
const {
frontColor,
backgroundColor,
animation: {
duration,
timingFunc
}
} = args
animation
} = args
const {
duration,
timingFunc
} = animation
if (frontColor) {
page.navigationBar.textColor = frontColor === '#000000' ? 'black' : 'white'
......@@ -41,7 +43,8 @@ function setNavigationBar (type, args) {
}
}
return {}
}
}
export function setNavigationBarColor (args) {
return setNavigationBar('setNavigationBarColor', args)
}
......
......@@ -28,6 +28,12 @@ export function stopPullDownRefresh () {
if (pageId) {
UniServiceJSBridge.emit(pageId + '.stopPullDownRefresh', {}, pageId)
pageId = null
} else {
const pages = getCurrentPages()
if (pages.length) {
pageId = pages[pages.length - 1].$page.id
UniServiceJSBridge.emit(pageId + '.stopPullDownRefresh', {}, pageId)
}
}
return {}
}
......@@ -32,5 +32,9 @@ export {
}
from 'uni-platform/service/bridge'
initOn(on)
initOn(on, {
getApp,
getCurrentPages
})
initSubscribe(subscribe)
......@@ -7,60 +7,83 @@ import {
setPullDownRefreshPageId
} from '../api/page-event'
function onError (err) {
callAppHook(getApp(), 'onError', err)
}
function onPageNotFound (page) {
callAppHook(getApp(), 'onPageNotFound', page)
}
function onPullDownRefresh (args, pageId) {
const page = getCurrentPages().find(page => page.$page.id === pageId)
if (page) {
setPullDownRefreshPageId(pageId)
callPageHook(page, 'onPullDownRefresh')
export default function initOn (on, {
getApp,
getCurrentPages
}) {
function onError (err) {
callAppHook(getApp(), 'onError', err)
}
}
function callCurrentPageHook (hook, args) {
const pages = getCurrentPages()
if (pages.length) {
callPageHook(pages[pages.length - 1], hook, args)
function onPageNotFound (page) {
callAppHook(getApp(), 'onPageNotFound', page)
}
}
function createCallCurrentPageHook (hook) {
return function (args) {
callCurrentPageHook(hook, args)
function onPullDownRefresh (args, pageId) {
const page = getCurrentPages().find(page => page.$page.id === pageId)
if (page) {
setPullDownRefreshPageId(pageId)
callPageHook(page, 'onPullDownRefresh')
}
}
}
function onAppEnterBackground () {
callAppHook(getApp(), 'onHide')
callCurrentPageHook('onHide')
}
function onAppEnterForeground () {
callAppHook(getApp(), 'onShow')
callCurrentPageHook('onShow')
}
function onWebInvokeAppService ({
name,
arg
}, pageId) {
if (name === 'postMessage') {
// TODO 小程序后退、组件销毁、分享时通知
} else {
uni[name](arg)
function callCurrentPageHook (hook, args) {
const pages = getCurrentPages()
if (pages.length) {
callPageHook(pages[pages.length - 1], hook, args)
}
}
function createCallCurrentPageHook (hook) {
return function (args) {
callCurrentPageHook(hook, args)
}
}
function onAppEnterBackground () {
callAppHook(getApp(), 'onHide')
callCurrentPageHook('onHide')
}
function onAppEnterForeground () {
callAppHook(getApp(), 'onShow')
callCurrentPageHook('onShow')
}
function onWebInvokeAppService ({
name,
arg
}, pageId) {
if (name === 'postMessage') {
// TODO 小程序后退、组件销毁、分享时通知
} else {
uni[name](arg)
}
}
const routeHooks = {
navigateTo () {
callCurrentPageHook('onHide')
},
navigateBack () {
callCurrentPageHook('onShow')
}
}
function onAppRoute ({
type
}) {
const routeHook = routeHooks[type]
routeHook && routeHook()
}
}
export default function initOn (on) {
on('onError', onError)
on('onPageNotFound', onPageNotFound)
if (__PLATFORM__ !== 'h5') { // 后续有时间,h5 平台也要迁移到 onAppRoute
on('onAppRoute', onAppRoute)
}
on('onAppEnterBackground', onAppEnterBackground)
on('onAppEnterForeground', onAppEnterForeground)
......@@ -74,4 +97,4 @@ export default function initOn (on) {
on('onNavigationBarSearchInputClicked', createCallCurrentPageHook('onNavigationBarSearchInputClicked'))
on('onWebInvokeAppService', onWebInvokeAppService)
}
}
......@@ -9,7 +9,8 @@ export default function createPage (pageVm) {
}
// 兼容 mpvue
pageVm.$vm = pageVm
pageVm.$root = pageVm
pageVm.$root = pageVm
pageVm.$holder = pageVm.$parent.$parent
// 补充 mp 相关属性
pageVm.$mp = {
mpType: 'page',
......
import {
ANI_DURATION
} from './webview'
let firstBackTime = 0
export default function initNavigateBack ({
plus,
getCurrentPages
}) {
return function navigateBack (delta, {
animationType,
animationDuration
}) {
const pages = getCurrentPages()
const len = pages.length - 1
const page = pages[len]
if (page.$meta.isQuit) {
if (!firstBackTime) {
firstBackTime = Date.now()
plus.nativeUI.toast('再按一次退出应用')
setTimeout(() => {
firstBackTime = null
}, 2000)
} else if (Date.now() - firstBackTime < 2000) {
plus.runtime.quit()
}
} else {
pages.splice(len, 1)
if (animationType) {
page.$getAppWebview().close(animationType, animationDuration || ANI_DURATION)
} else {
page.$getAppWebview().close('auto')
}
}
}
}
import {
showWebview
} from './webview'
export default function initNavigateTo ({
__registerPage
}) {
return function navigateTo (path, {
animationType,
animationDuration
}) {
showWebview(
__registerPage({
path
}),
animationType,
animationDuration
)
}
}
export default function initReLaunch () {
return function reLaunch (path) {
}
}
export default function initRedirectTo () {
return function redirectTo (path) {
}
}
export default function initSwitchTab (path) {
return function switchTab () {
}
}
export * from 'uni-core/service/api/route'
export {
pageScrollTo,
startPullDownRefresh,
stopPullDownRefresh
}
from 'uni-core/service/api/page-event'
export * from 'uni-core/service/api/navigation-bar'
import Router from 'uni-platform/service/uni/router/index'
import {
callAppHook
} from 'uni-core/service/plugins/util'
import Router from './router/index'
let appCtx
const NETWORK_TYPES = [
'unknown',
'none',
'ethernet',
'wifi',
'2g',
'3g',
'4g'
]
export function getApp () {
return appCtx
}
function initListeners ({
plus
function initGlobalListeners ({
plus,
UniServiceJSBridge
}) {
const emit = UniServiceJSBridge.emit
plus.key.addEventListener('backbutton', () => {
appCtx.$router.go(-1)
})
plus.globalEvent.addEventListener('pause', () => {
emit('onAppEnterBackground')
})
plus.globalEvent.addEventListener('resume', () => {
emit('onAppEnterForeground')
})
plus.globalEvent.addEventListener('netchange', () => {
const networkType = NETWORK_TYPES[plus.networkinfo.getCurrentType()]
emit('onNetworkStatusChange', {
isConnected: networkType !== 'none',
networkType
})
})
}
function initAppLaunch (appVm, {
__uniConfig
}) {
const args = {
path: __uniConfig.entryPagePath,
query: {},
scene: 1001
}
callAppHook(appVm, 'onLaunch', args)
callAppHook(appVm, 'onShow', args)
}
export function registerApp (appVm, instanceContext) {
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] registerApp`)
}
appCtx = appVm
appCtx.$router = new Router(instanceContext)
initListeners(instanceContext)
initAppLaunch(appVm, instanceContext)
initGlobalListeners(instanceContext)
}
import initOn from 'uni-core/service/bridge/on'
export function initServiceJSBridge (Vue) {
let bridge
export function initServiceJSBridge (Vue, instanceContext) {
if (bridge) {
return bridge
}
const Emitter = new Vue()
const bridge = {
bridge = {
on: Emitter.$on.bind(Emitter),
off: Emitter.$off.bind(Emitter),
once: Emitter.$once.bind(Emitter),
emit: Emitter.$emit.bind(Emitter)
}
initOn(bridge.on)
initOn(bridge.on, instanceContext)
return bridge
}
......@@ -9,21 +9,35 @@ function parseRoutes (config) {
Object.keys(config.page).forEach(function (pagePath) {
const isTabBar = tabBarList.indexOf(pagePath) !== -1
const isQuit = isTabBar || (config.pages[0] === pagePath)
const isNVue = !!config.page[pagePath].nvue
uniRoutes.push({
path: '/' + pagePath,
meta: {
isQuit,
isTabBar
isTabBar,
isNVue
},
window: config.page[pagePath] || {}
window: config.page[pagePath].window || {}
})
})
}
export function registerConfig (config) {
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] registerConfig`)
}
export function registerConfig (config, {
plus
}) {
Object.assign(uniConfig, config)
uniConfig.viewport = ''
uniConfig.defaultFontSize = ''
if (uniConfig.nvueCompiler === 'uni-app') {
uniConfig.viewport = plus.screen.resolutionWidth
uniConfig.defaultFontSize = uniConfig.viewport / 20
}
parseRoutes(uniConfig)
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] registerConfig`, uniConfig)
}
}
......@@ -16,7 +16,7 @@ import {
import {
createUniInstance
} from './uni'
} from './uni'
import {
initServiceJSBridge
......@@ -29,7 +29,10 @@ export function createInstanceContext (instanceContext) {
WeexPlus
} = instanceContext
const plus = new WeexPlus(weex)
const UniJSServiceBridge = initServiceJSBridge(Vue)
const UniServiceJSBridge = initServiceJSBridge(Vue, {
getApp,
getCurrentPages
})
return {
__uniConfig: uniConfig,
__uniRoutes: uniRoutes,
......@@ -48,11 +51,12 @@ export function createInstanceContext (instanceContext) {
plus,
uniConfig,
uniRoutes,
UniServiceJSBridge,
getApp,
getCurrentPages
),
getApp,
getCurrentPages,
UniJSServiceBridge
UniServiceJSBridge
}
}
export function createHolder (webview, {
navigationBar
}, {
Vue
}) {
const navigationBarState = Vue.observable(navigationBar)
/* eslint-disable no-new */
new Vue({
created () {
this.$watch(() => navigationBarState.titleText, (val, oldVal) => {
webview.setStyle({
titleNView: {
titleText: val || ''
}
})
})
this.$watch(() => [navigationBarState.textColor, navigationBarState.backgroundColor], (val) => {
webview.setStyle({
titleNView: {
titleColor: val[0],
backgroundColor: val[1]
}
})
})
}
})
return {
navigationBar: navigationBarState
}
}
......@@ -12,4 +12,4 @@ const uni = Object.create(null)
Object.keys(api).forEach(name => {
uni[name] = promisify(name, wrapper(name, api[name]))
})
})
import {
initWebview,
createWebview
} from './webview'
} from './webview/index'
import {
createHolder
} from './holder'
const pages = []
......@@ -33,25 +37,34 @@ export function registerPage ({
path,
webview
}, instanceContext) {
const routeOptions = instanceContext.__uniRoutes.find(route => route.path === path)
const routeOptions = JSON.parse(JSON.stringify(instanceContext.__uniRoutes.find(route => route.path === path)))
if (!webview) {
webview = createWebview(path, instanceContext, routeOptions.window)
webview = createWebview(path, instanceContext, routeOptions)
}
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] registerPage`, path, webview.id)
}
initWebview(webview, instanceContext, webview.id === '1' && routeOptions.window)
initWebview(webview, instanceContext, webview.id === '1' && routeOptions)
const route = path.slice(1)
pages.push({
route: path.slice(1),
route,
$getAppWebview () {
return webview
},
$meta: routeOptions.meta
})
$page: {
id: parseInt(webview.id),
meta: routeOptions.meta,
path,
route
},
$holder: createHolder(webview, {
navigationBar: webview.$navigationBar
}, instanceContext)
})
return webview
}
import initNavigateTo from './navigate-to'
import initRedirectTo from './redirect-to'
import initNavigateBack from './navigate-back'
import initSwitchTab from './switch-tab'
import initReLaunch from './re-launch'
import navigateBack from './navigate-back'
import navigateTo from './navigate-to'
import reLaunch from './re-launch'
import redirectTo from './redirect-to'
import switchTab from './switch-tab'
function initRouter (instanceContext) {
return {
navigateTo: initNavigateTo(instanceContext),
redirectTo: initRedirectTo(instanceContext),
navigateBack: initNavigateBack(instanceContext),
switchTab: initSwitchTab(instanceContext),
reLaunch: initReLaunch(instanceContext)
}
const route = {
navigateBack,
navigateTo,
reLaunch,
redirectTo,
switchTab
}
export default class Router {
constructor (instanceContext) {
this.router = initRouter(instanceContext)
this.instanceContext = instanceContext
this.$emit = instanceContext.UniServiceJSBridge.emit
}
push ({
......@@ -24,11 +23,17 @@ export default class Router {
path,
animationType,
animationDuration
} = {}) {
this.router[type](path, {
} = {}) {
this.$emit('onAppRoute', {
type,
path
})
route[type]({
path,
animationType,
animationDuration
})
}, this.instanceContext)
}
go (delta, {
......@@ -36,9 +41,14 @@ export default class Router {
animationDuration
} = {}) {
delta = Math.abs(parseInt(delta) || 1)
this.router.navigateBack(delta, {
route.navigateBack({
delta,
animationType,
animationDuration
}, this.instanceContext, () => {
this.$emit('onAppRoute', {
type: 'navigateBack'
})
})
}
}
import {
ANI_DURATION
} from './util'
let firstBackTime = 0
export default function navigateBack ({
delta,
animationType,
animationDuration
}, {
plus,
getCurrentPages
}, callback) {
const pages = getCurrentPages()
const len = pages.length - 1
const page = pages[len]
if (page.$page.meta.isQuit) {
if (!firstBackTime) {
firstBackTime = Date.now()
plus.nativeUI.toast('再按一次退出应用')
setTimeout(() => {
firstBackTime = null
}, 2000)
} else if (Date.now() - firstBackTime < 2000) {
plus.runtime.quit()
}
} else {
pages.splice(len, 1)
if (animationType) {
page.$getAppWebview().close(animationType, animationDuration || ANI_DURATION)
} else {
page.$getAppWebview().close('auto')
}
callback && callback()
}
}
import {
showWebview
} from './util'
export default function navigateTo ({
path,
animationType,
animationDuration
}, {
__registerPage
}) {
showWebview(
__registerPage({
path
}),
animationType,
animationDuration
)
}
export default function reLaunch ({
path
}, instanceContext) {}
export default function redirectTo ({
path
}, instanceContext) {}
export default function switchTab ({
path
}, instanceContext) {}
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)
})
})
}
import {
isPlainObject
}
from 'uni-shared'
import {
parseWebviewStyle
} from './parser/webview-style-parser'
import {
parseNavigationBar
} from './parser/navigation-bar-parser'
let id = 2
const WEBVIEW_LISTENERS = {
'pullToRefresh': 'onPullDownRefresh',
'titleNViewSearchInputChanged': 'onNavigationBarSearchInputChanged',
'titleNViewSearchInputConfirmed': 'onNavigationBarSearchInputConfirmed',
'titleNViewSearchInputClicked': 'onNavigationBarSearchInputClicked'
}
export function createWebview (path, instanceContext, routeOptions) {
const webviewId = id++
const webviewStyle = parseWebviewStyle(
webviewId,
path,
routeOptions,
instanceContext
)
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] createWebview`, webviewId, path, webviewStyle)
}
const webview = instanceContext.plus.webview.create('', String(webviewId), webviewStyle)
webview.$navigationBar = parseNavigationBar(webviewStyle)
return webview
}
export function initWebview (webview, instanceContext, routeOptions) {
if (isPlainObject(routeOptions)) {
const webviewStyle = parseWebviewStyle(
parseInt(webview.id),
'',
routeOptions,
instanceContext
)
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] updateWebview`, webviewStyle)
}
webview.$navigationBar = parseNavigationBar(webviewStyle)
webview.setStyle(webviewStyle)
}
// TODO subNVues
Object.keys(WEBVIEW_LISTENERS).forEach(name => {
webview.addEventListener(name, (e) => {
instanceContext.UniServiceJSBridge.emit(WEBVIEW_LISTENERS[name], e, parseInt(webview.id))
})
})
instanceContext.UniServiceJSBridge.on(webview.id + '.stopPullDownRefresh', () => {
webview.endPullToRefresh()
})
return webview
}
export function parseNavigationBar (webviewStyle) {
let titleText = ''
let textColor = ''
let backgroundColor = ''
const titleNView = webviewStyle.titleNView
if (titleNView) {
titleText = titleNView.titleText || ''
textColor = titleNView.textColor || ''
backgroundColor = titleNView.backgroundColor || ''
}
return {
titleText,
textColor,
backgroundColor
}
}
export function parsePullToRefresh (routeOptions, {
plus
}) {
const windowOptions = routeOptions.window
if (windowOptions.enablePullDownRefresh) {
const pullToRefreshStyles = Object.create(null)
// 初始化默认值
if (plus.os.name === 'Android') {
Object.assign(pullToRefreshStyles, {
support: true,
style: 'circle'
})
} else {
Object.assign(pullToRefreshStyles, {
support: true,
style: 'default',
height: '50px',
range: '200px',
contentdown: {
caption: ''
},
contentover: {
caption: ''
},
contentrefresh: {
caption: ''
}
})
}
if (windowOptions.backgroundTextStyle) {
pullToRefreshStyles.color = windowOptions.backgroundTextStyle
pullToRefreshStyles.snowColor = windowOptions.backgroundTextStyle
}
Object.assign(pullToRefreshStyles, windowOptions.pullToRefresh || {})
return pullToRefreshStyles
}
}
import {
isPlainObject
}
from 'uni-shared'
export function parseTitleNView (routeOptions) {
const windowOptions = routeOptions.window
const titleNView = windowOptions.titleNView
if ( // 无头
titleNView === false ||
titleNView === 'false' ||
(
windowOptions.navigationStyle === 'custom' &&
!isPlainObject(titleNView)
)
) {
return false
}
const ret = {
autoBackButton: !routeOptions.meta.isQuit,
backgroundColor: windowOptions.navigationBarBackgroundColor || '#000000',
titleText: windowOptions.navigationBarTitleText || '',
titleColor: windowOptions.navigationBarTextStyle === 'black' ? '#000000' : '#ffffff'
}
if (isPlainObject(titleNView)) {
return Object.assign(ret, titleNView)
}
return ret
}
import {
parseTitleNView
} from './title-nview-parser'
import {
parsePullToRefresh
} from './pull-to-refresh-parser'
const WEBVIEW_STYLE_BLACKLIST = [
'navigationBarBackgroundColor',
'navigationBarTextStyle',
'navigationBarTitleText',
'navigationBarShadow',
'navigationStyle',
'disableScroll',
'backgroundColor',
'backgroundTextStyle',
'enablePullDownRefresh',
'onReachBottomDistance',
'usingComponents',
// 需要解析的
'titleNView',
'pullToRefresh'
]
export function parseWebviewStyle (id, path, routeOptions = {}, instanceContext) {
const {
__uniConfig
} = instanceContext
const webviewStyle = Object.create(null)
// 合并
const windowOptions = Object.assign(
JSON.parse(JSON.stringify(__uniConfig.window || {})),
routeOptions.window || {}
)
Object.keys(windowOptions).forEach(name => {
if (WEBVIEW_STYLE_BLACKLIST.indexOf(name) === -1) {
webviewStyle[name] = windowOptions[name]
}
})
const titleNView = parseTitleNView(routeOptions)
if (titleNView) {
if (id === 1 && __uniConfig.realEntryPagePath === path) {
titleNView.autoBackButton = true
}
webviewStyle.titleNView = titleNView
}
const pullToRefresh = parsePullToRefresh(routeOptions, instanceContext)
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
}
}
return webviewStyle
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册