Skip to content

Commit

Permalink
feat(app-plus-nvue): add route
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Jul 17, 2019
1 parent e8cf768 commit 2d30a92
Show file tree
Hide file tree
Showing 17 changed files with 485 additions and 124 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ root = true
[*]
charset = utf-8
indent_style = space
indent_size = 4
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
Expand Down
216 changes: 171 additions & 45 deletions packages/uni-app-plus-nvue/dist/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,139 @@
import { createUniInstance } from './uni';

function getWebviewStyle () {
const ANI_SHOW = 'pop-in';
const ANI_DURATION = 300;

let id = 0;

function getId () {
return id++
}

function parseWebviewStyle (path) {
return {
titleNView: {
autoBackButton: true,
titleText: 'titleText'
},
uniNView: {
path
}
}
}
class Router {
constructor (routes, plus) {
this.routes = routes;
this.plus = plus;
this.id = 0;

this.aniShow = plus.os.name === 'Android' ? 'slide-in-right' : 'pop-in';
this.aniClose = 'pop-out';
this.aniDuration = 300;
}

push ({
type,
path
} = {}) {
this.plus.webview.open(
}

function initNavigateTo ({
plus,
__registerPage
}) {
return function navigateTo (path, {
animationType,
animationDuration
}) {
const webview = plus.webview.open(
'',
String(this.id++),
getWebviewStyle(),
this.aniShow,
this.aniDuration,
String(getId()),
parseWebviewStyle(path),
animationType || ANI_SHOW,
animationDuration || ANI_DURATION,
() => {
console.log('show.callback');
});

__registerPage({
path,
webview
});
}
}

function initRedirectTo () {
return function redirectTo (path) {

replace ({
type,
path
} = {}) {
}
}

let firstBackTime = 0;

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');
}
}
}
}

function initSwitchTab (path) {
return function switchTab () {

}
}

function initReLaunch () {
return function reLaunch (path) {

go (delta) {
}
}

function initRouter (instanceContext) {
return {
navigateTo: initNavigateTo(instanceContext),
redirectTo: initRedirectTo(instanceContext),
navigateBack: initNavigateBack(instanceContext),
switchTab: initSwitchTab(instanceContext),
reLaunch: initReLaunch(instanceContext)
}
}

class Router {
constructor (instanceContext) {
this.router = initRouter(instanceContext);
}

push ({
type,
path,
animationType,
animationDuration
} = {}) {
this.router[type](path, {
animationType,
animationDuration
});
}

go (delta, {
animationType,
animationDuration
} = {}) {
delta = Math.abs(parseInt(delta) || 1);
this.router.navigateBack(delta, {
animationType,
animationDuration
});
}
}

Expand All @@ -52,15 +143,27 @@ function getApp () {
return appCtx
}

function registerApp (appVm, routes, plus) {
function initListeners ({
plus
}) {
plus.key.addEventListener('backbutton', () => {
appCtx.$router.go(-1);
});
}

function registerApp (appVm, instanceContext) {
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] registerApp`);
}
appCtx = appVm;
appCtx.$router = new Router(routes, plus);
appCtx.$router = new Router(instanceContext);
initListeners(instanceContext);
}

const pageVms = [];
const pages = [];

function getCurrentPages () {
return pageVms
return pages
}
/**
* @param {Object} pageVm
Expand All @@ -80,55 +183,78 @@ function getCurrentPages () {
*
*
*/
function registerPage (pageVm) {
pageVms.push(pageVm);

function registerPage ({
vm,
path,
webview
}, instanceContext) {
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] registerPage`, path, webview.id);
}
pages.push({
route: path.slice(1),
$getAppWebview () {
return webview
},
$meta: instanceContext.__uniRoutes.find(route => route.path === path).meta,
$vm: vm
});
}

const uniConfig = Object.create(null);
const uniRoutes = [];

function parseRoutes (config) {
uniRoutes.length = 0;
uniRoutes.length = 0;
/* eslint-disable no-mixed-operators */
const tabBarList = (config.tabBar && config.tabBar.list || []).map(item => item.pagePath);

Object.keys(config.page).forEach(function (pagePath) {
const isTabBar = tabBarList.indexOf(pagePath) !== -1;
const isQuit = isTabBar || (config.pages[0] === pagePath);
uniRoutes.push({
path: '/' + pagePath,
meta: {
isTabBar: tabBarList.indexOf(pagePath) !== -1
isQuit,
isTabBar
}
});
});
}

function registerConfig (config) {
if (process.env.NODE_ENV !== 'production') {
console.log(`[uni-app] registerConfig`);
}
Object.assign(uniConfig, config);
parseRoutes(uniConfig);
}

function createInstanceContext ({
weex,
WeexPlus
}) {
function createInstanceContext (instanceContext) {
const {
weex,
WeexPlus
} = instanceContext;
const plus = new WeexPlus(weex);
return {
__uniConfig: uniConfig,
__uniRoutes: uniRoutes,
__registerConfig (config) {
registerConfig(config);
registerConfig(config, instanceContext);
},
__registerApp (appVm) {
registerApp(appVm, uniRoutes, plus);
registerApp(appVm, instanceContext);
},
__registerPage (pageVm) {
registerPage(pageVm);
__registerPage (page) {
registerPage(page, instanceContext);
},
plus,
uni: createUniInstance(
weex,
plus,
__uniConfig,
__uniRoutes,
uniConfig,
uniRoutes,
getApp,
getCurrentPages
),
Expand Down
Loading

0 comments on commit 2d30a92

Please sign in to comment.