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

feat(app-plus-nvue): refactor route

上级 2d30a925
import { createUniInstance } from './uni'; import { createUniInstance } from './uni';
const ANI_SHOW = 'pop-in';
const ANI_DURATION = 300; const ANI_DURATION = 300;
const ANI_SHOW = 'pop-in';
let id = 0; function showWebview (webview, animationType, animationDuration) {
setTimeout(() => {
function getId () { webview.show(
return id++ animationType || ANI_SHOW,
} animationDuration || ANI_DURATION,
() => {
function parseWebviewStyle (path) { console.log('show.callback');
return { }
titleNView: { );
autoBackButton: true, }, 50);
titleText: 'titleText'
},
uniNView: {
path
}
}
} }
function initNavigateTo ({ function initNavigateTo ({
plus,
__registerPage __registerPage
}) { }) {
return function navigateTo (path, { return function navigateTo (path, {
animationType, animationType,
animationDuration animationDuration
}) { }) {
const webview = plus.webview.open( showWebview(
'', __registerPage({
String(getId()), path
parseWebviewStyle(path), }),
animationType || ANI_SHOW, animationType,
animationDuration || ANI_DURATION, animationDuration
() => { );
console.log('show.callback');
});
__registerPage({
path,
webview
});
} }
} }
...@@ -139,7 +125,7 @@ class Router { ...@@ -139,7 +125,7 @@ class Router {
let appCtx; let appCtx;
function getApp () { function getApp$1 () {
return appCtx return appCtx
} }
...@@ -160,9 +146,62 @@ function registerApp (appVm, instanceContext) { ...@@ -160,9 +146,62 @@ function registerApp (appVm, instanceContext) {
initListeners(instanceContext); initListeners(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
}
function createWebview (path, {
plus,
__uniConfig
}, windowOptions) {
return plus.webview.create(
'',
String(id++),
parseWebviewStyle(
path,
parseWindowOptions(windowOptions, __uniConfig.window)
))
}
function initWebview (webview, {
UniJSServiceBridge
}) {
// TODO subNVues
WEBVIEW_LISTENERS.forEach(listener => {
webview.addEventListener(listener, (e) => {
UniJSServiceBridge.emit(listener, e);
});
});
}
const pages = []; const pages = [];
function getCurrentPages () { function getCurrentPages$1 () {
return pages return pages
} }
/** /**
...@@ -183,23 +222,34 @@ function getCurrentPages () { ...@@ -183,23 +222,34 @@ function getCurrentPages () {
* *
* *
*/ */
/**
* 首页需要主动registerPage,二级页面路由跳转时registerPage
*/
function registerPage ({ function registerPage ({
vm,
path, path,
webview webview
}, instanceContext) { }, instanceContext) {
const routeOptions = instanceContext.__uniRoutes.find(route => route.path === path);
if (!webview) {
webview = createWebview(path, instanceContext, routeOptions.window);
}
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] registerPage`, path, webview.id); console.log(`[uni-app] registerPage`, path, webview.id);
} }
initWebview(webview, instanceContext, webview.id === '1' && routeOptions.window);
pages.push({ pages.push({
route: path.slice(1), route: path.slice(1),
$getAppWebview () { $getAppWebview () {
return webview return webview
}, },
$meta: instanceContext.__uniRoutes.find(route => route.path === path).meta, $meta: routeOptions.meta
$vm: vm });
});
return webview
} }
const uniConfig = Object.create(null); const uniConfig = Object.create(null);
...@@ -231,23 +281,140 @@ function registerConfig (config) { ...@@ -231,23 +281,140 @@ function registerConfig (config) {
parseRoutes(uniConfig); parseRoutes(uniConfig);
} }
function callHook (vm, hook, params) {
return (vm.$vm || vm).__call_hook(hook, params)
}
function callAppHook (vm, hook, params) {
if (hook !== 'onError') {
console.debug(`App:${hook} have been invoked` + (params ? ` ${JSON.stringify(params)}` : ''));
}
return (vm.$vm || vm).__call_hook(hook, params)
}
function callPageHook (vm, hook, params) {
// hack 一下,H5 平台通知 View 层onShow,方便 View 层来切换 scroll 事件监听
if (__PLATFORM__ === 'h5') {
if (hook === 'onLoad') {
vm.$mp.query = params;
UniServiceJSBridge.publishHandler('onPageLoad', vm, vm.$page.id);
}
if (hook === 'onShow') {
if (
vm.$route.meta.isTabBar &&
vm.$route.params.detail
) {
UniServiceJSBridge.emit('onTabItemTap', vm.$route.params.detail);
}
UniServiceJSBridge.publishHandler('onPageShow', vm, vm.$page.id);
}
}
if (hook !== 'onPageScroll') {
console.debug(`${vm.$page.route}[${vm.$page.id}]:${hook} have been invoked`);
}
return callHook(vm, hook, params)
}
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) {
callPageHook(page, 'onPullDownRefresh');
}
}
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') ; else {
uni[name](arg);
}
}
function initOn (on) {
on('onError', onError);
on('onPageNotFound', onPageNotFound);
on('onAppEnterBackground', onAppEnterBackground);
on('onAppEnterForeground', onAppEnterForeground);
on('onPullDownRefresh', onPullDownRefresh);
on('onTabItemTap', createCallCurrentPageHook('onTabItemTap'));
on('onNavigationBarButtonTap', createCallCurrentPageHook('onNavigationBarButtonTap'));
on('onNavigationBarSearchInputChanged', createCallCurrentPageHook('onNavigationBarSearchInputChanged'));
on('onNavigationBarSearchInputConfirmed', createCallCurrentPageHook('onNavigationBarSearchInputConfirmed'));
on('onNavigationBarSearchInputClicked', createCallCurrentPageHook('onNavigationBarSearchInputClicked'));
on('onWebInvokeAppService', onWebInvokeAppService);
}
function initServiceJSBridge (Vue) {
const Emitter = new Vue();
const bridge = {
on: Emitter.$on.bind(Emitter),
off: Emitter.$off.bind(Emitter),
once: Emitter.$once.bind(Emitter),
emit: Emitter.$emit.bind(Emitter)
};
initOn(bridge.on);
return bridge
}
function createInstanceContext (instanceContext) { function createInstanceContext (instanceContext) {
const { const {
weex, weex,
Vue,
WeexPlus WeexPlus
} = instanceContext; } = instanceContext;
const plus = new WeexPlus(weex); const plus = new WeexPlus(weex);
const UniJSServiceBridge = initServiceJSBridge(Vue);
return { return {
__uniConfig: uniConfig, __uniConfig: uniConfig,
__uniRoutes: uniRoutes, __uniRoutes: uniRoutes,
__registerConfig (config) { __registerConfig (config) {
registerConfig(config, instanceContext); return registerConfig(config, instanceContext)
}, },
__registerApp (appVm) { __registerApp (appVm) {
registerApp(appVm, instanceContext); return registerApp(appVm, instanceContext)
}, },
__registerPage (page) { __registerPage (page) {
registerPage(page, instanceContext); return registerPage(page, instanceContext)
}, },
plus, plus,
uni: createUniInstance( uni: createUniInstance(
...@@ -255,11 +422,12 @@ function createInstanceContext (instanceContext) { ...@@ -255,11 +422,12 @@ function createInstanceContext (instanceContext) {
plus, plus,
uniConfig, uniConfig,
uniRoutes, uniRoutes,
getApp, getApp$1,
getCurrentPages getCurrentPages$1
), ),
getApp, getApp: getApp$1,
getCurrentPages getCurrentPages: getCurrentPages$1,
UniJSServiceBridge
} }
} }
......
function callHook (vm, hook, params) { function callHook (vm, hook, params) {
return vm.__call_hook(hook, params) return (vm.$vm || vm).__call_hook(hook, params)
} }
export function callAppHook (vm, hook, params) { export function callAppHook (vm, hook, params) {
if (hook !== 'onError') { if (hook !== 'onError') {
console.debug(`App:${hook} have been invoked` + (params ? ` ${JSON.stringify(params)}` : '')) console.debug(`App:${hook} have been invoked` + (params ? ` ${JSON.stringify(params)}` : ''))
} }
return vm.__call_hook(hook, params) return (vm.$vm || vm).__call_hook(hook, params)
} }
export function callPageHook (vm, hook, params) { export function callPageHook (vm, hook, params) {
...@@ -19,7 +19,7 @@ export function callPageHook (vm, hook, params) { ...@@ -19,7 +19,7 @@ export function callPageHook (vm, hook, params) {
if (hook === 'onShow') { if (hook === 'onShow') {
if ( if (
vm.$route.meta.isTabBar && vm.$route.meta.isTabBar &&
vm.$route.params.detail vm.$route.params.detail
) { ) {
UniServiceJSBridge.emit('onTabItemTap', vm.$route.params.detail) UniServiceJSBridge.emit('onTabItemTap', vm.$route.params.detail)
} }
......
import { import {
ANI_DURATION ANI_DURATION
} from './util' } from './webview'
let firstBackTime = 0 let firstBackTime = 0
......
import { import {
getId, showWebview
ANI_SHOW, } from './webview'
ANI_DURATION,
parseWebviewStyle
} from './util'
export default function initNavigateTo ({ export default function initNavigateTo ({
plus,
__registerPage __registerPage
}) { }) {
return function navigateTo (path, { return function navigateTo (path, {
animationType, animationType,
animationDuration animationDuration
}) { }) {
const webview = plus.webview.open( showWebview(
'', __registerPage({
String(getId()), path
parseWebviewStyle(path), }),
animationType || ANI_SHOW, animationType,
animationDuration || ANI_DURATION, animationDuration
() => { )
console.log('show.callback')
})
__registerPage({
path,
webview
})
} }
} }
export const ANI_SHOW = 'pop-in'
export const ANI_DURATION = 300
let id = 0
export function getId () {
return id++
}
export function parseWebviewStyle (path) {
return {
titleNView: {
autoBackButton: true,
titleText: 'titleText'
},
uniNView: {
path
}
}
}
export const ANI_DURATION = 300
const ANI_SHOW = 'pop-in'
export function showWebview (webview, animationType, animationDuration) {
setTimeout(() => {
webview.show(
animationType || ANI_SHOW,
animationDuration || ANI_DURATION,
() => {
console.log('show.callback')
}
)
}, 50)
}
import initOn from 'uni-core/service/bridge/on'
export function initServiceJSBridge (Vue) {
const Emitter = new Vue()
const bridge = {
on: Emitter.$on.bind(Emitter),
off: Emitter.$off.bind(Emitter),
once: Emitter.$once.bind(Emitter),
emit: Emitter.$emit.bind(Emitter)
}
initOn(bridge.on)
return bridge
}
...@@ -14,7 +14,8 @@ function parseRoutes (config) { ...@@ -14,7 +14,8 @@ function parseRoutes (config) {
meta: { meta: {
isQuit, isQuit,
isTabBar isTabBar
} },
window: config.page[pagePath] || {}
}) })
}) })
} }
......
...@@ -16,25 +16,31 @@ import { ...@@ -16,25 +16,31 @@ import {
import { import {
createUniInstance createUniInstance
} from './uni' } from './uni'
import {
initServiceJSBridge
} from './bridge'
export function createInstanceContext (instanceContext) { export function createInstanceContext (instanceContext) {
const { const {
weex, weex,
Vue,
WeexPlus WeexPlus
} = instanceContext } = instanceContext
const plus = new WeexPlus(weex) const plus = new WeexPlus(weex)
const UniJSServiceBridge = initServiceJSBridge(Vue)
return { return {
__uniConfig: uniConfig, __uniConfig: uniConfig,
__uniRoutes: uniRoutes, __uniRoutes: uniRoutes,
__registerConfig (config) { __registerConfig (config) {
registerConfig(config, instanceContext) return registerConfig(config, instanceContext)
}, },
__registerApp (appVm) { __registerApp (appVm) {
registerApp(appVm, instanceContext) return registerApp(appVm, instanceContext)
}, },
__registerPage (page) { __registerPage (page) {
registerPage(page, instanceContext) return registerPage(page, instanceContext)
}, },
plus, plus,
uni: createUniInstance( uni: createUniInstance(
...@@ -46,6 +52,7 @@ export function createInstanceContext (instanceContext) { ...@@ -46,6 +52,7 @@ export function createInstanceContext (instanceContext) {
getCurrentPages getCurrentPages
), ),
getApp, getApp,
getCurrentPages getCurrentPages,
UniJSServiceBridge
} }
} }
import {
initWebview,
createWebview
} from './webview'
const pages = [] const pages = []
export function getCurrentPages () { export function getCurrentPages () {
...@@ -21,21 +26,32 @@ export function getCurrentPages () { ...@@ -21,21 +26,32 @@ export function getCurrentPages () {
* *
* *
*/ */
/**
* 首页需要主动registerPage,二级页面路由跳转时registerPage
*/
export function registerPage ({ export function registerPage ({
vm,
path, path,
webview webview
}, instanceContext) { }, instanceContext) {
const routeOptions = instanceContext.__uniRoutes.find(route => route.path === path)
if (!webview) {
webview = createWebview(path, instanceContext, routeOptions.window)
}
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] registerPage`, path, webview.id) console.log(`[uni-app] registerPage`, path, webview.id)
} }
initWebview(webview, instanceContext, webview.id === '1' && routeOptions.window)
pages.push({ pages.push({
route: path.slice(1), route: path.slice(1),
$getAppWebview () { $getAppWebview () {
return webview return webview
}, },
$meta: instanceContext.__uniRoutes.find(route => route.path === path).meta, $meta: routeOptions.meta
$vm: vm })
})
return webview
} }
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)
})
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册