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

refactor app api

上级 60e8e2e2
...@@ -5,24 +5,28 @@ const resolve = require('rollup-plugin-node-resolve') ...@@ -5,24 +5,28 @@ const resolve = require('rollup-plugin-node-resolve')
const commonjs = require('rollup-plugin-commonjs') const commonjs = require('rollup-plugin-commonjs')
const requireContext = require('./rollup-plugin-require-context') const requireContext = require('./rollup-plugin-require-context')
let input = 'src/platforms/app-plus/service/uni/create-instance-context.js' let input = 'src/platforms/app-plus/service/framework/create-instance-context.js'
const output = { const output = {
file: 'packages/uni-app-plus-nvue/dist/index.js', file: 'packages/uni-app-plus-nvue/dist/index.js',
format: 'es' format: 'es'
} }
const external = []
if (process.env.UNI_SERVICE === 'legacy') { if (process.env.UNI_SERVICE === 'legacy') {
input = 'src/platforms/app-plus-nvue/service/index.legacy.js' input = 'src/platforms/app-plus-nvue/services/index.legacy.js'
output.file = 'packages/uni-app-plus-nvue/dist/index.legacy.js' output.file = 'packages/uni-app-plus-nvue/dist/index.legacy.js'
} else if (process.env.UNI_SERVICE === 'uni') { } else if (process.env.UNI_SERVICE === 'uni') {
input = 'src/platforms/app-plus/service/uni/index.js' input = 'src/platforms/app-plus/service/uni.js'
output.file = 'packages/uni-app-plus-nvue/dist/uni.js' output.file = 'packages/uni-app-plus-nvue/dist/uni.js'
output.banner = output.banner =
`export function createUniInstance(weex, plus, __uniConfig, __uniRoutes, UniServiceJSBridge, getApp, getCurrentPages){ `export function createUniInstance(weex, plus, __uniConfig, __uniRoutes, __registerPage, UniServiceJSBridge, getApp, getCurrentPages){
var localStorage = plus.storage var localStorage = plus.storage
` `
output.footer = '\n return uni$1 \n}' output.footer = '\n return uni$1 \n}'
} else {
external.push('./uni')
} }
module.exports = { module.exports = {
...@@ -34,16 +38,16 @@ module.exports = { ...@@ -34,16 +38,16 @@ module.exports = {
requireContext(), requireContext(),
alias({ alias({
'uni-core': path.resolve(__dirname, '../src/core'), 'uni-core': path.resolve(__dirname, '../src/core'),
'uni-platform': path.resolve(__dirname, '../src/platforms/' + process.env.UNI_PLATFORM), 'uni-platform': path.resolve(__dirname, '../src/platforms/app-plus'),
'uni-platforms': path.resolve(__dirname, '../src/platforms'), 'uni-platforms': path.resolve(__dirname, '../src/platforms'),
'uni-shared': path.resolve(__dirname, '../src/shared/util.js'), 'uni-shared': path.resolve(__dirname, '../src/shared/util.js'),
'uni-helpers': path.resolve(__dirname, '../src/core/helpers') 'uni-helpers': path.resolve(__dirname, '../src/core/helpers')
}), }),
replace({ replace({
__GLOBAL__: 'getGlobalUni()', __GLOBAL__: 'getGlobalUni()',
__PLATFORM__: JSON.stringify(process.env.UNI_PLATFORM), __PLATFORM__: JSON.stringify('app-plus'),
__PLATFORM_TITLE__: 'app-plus-nvue' __PLATFORM_TITLE__: 'app-plus-nvue'
}) })
], ],
external: ['./uni'] external
} }
{ {
"name": "uniapp-js-framework", "name": "uniapp-js-framework",
"version": "0.0.1", "version": "0.0.1",
"scripts": { "scripts": {
"lint": "eslint --fix --config package.json --ext .js --ext .vue --ignore-path .eslintignore build src", "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", "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: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.mp.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:all": "npm run lint && npm run build:app:uni && 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:uni": "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: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: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-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-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-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-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: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", "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" "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"
},
"dependencies": {
"base64-arraybuffer": "^0.2.0",
"intersection-observer": "^0.7.0"
},
"private": true,
"devDependencies": {
"@vue/cli-plugin-babel": "^3.4.1",
"@vue/cli-plugin-eslint": "^3.4.1",
"@vue/cli-plugin-unit-mocha": "^3.4.1",
"@vue/cli-service": "^3.4.1",
"@vue/test-utils": "^1.0.0-beta.25",
"babel-eslint": "^10.0.1",
"babylon": "^6.18.0",
"browserslist": "^4.4.2",
"caniuse-lite": "^1.0.30000940",
"chai": "^4.1.2",
"copy": "^0.3.2",
"cross-env": "^5.2.0",
"del": "^5.0.0",
"eslint": "^5.5.0",
"eslint-config-standard": "^12.0.0",
"eslint-loader": "^2.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-promise": "^4.0.0",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^4.7.1",
"jsdom": "^13.0.0",
"jsdom-global": "^3.0.2",
"jsonfile": "^5.0.0",
"rollup": "^1.17.0",
"rollup-plugin-alias": "^1.4.0",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.1.0",
"strip-json-comments": "^2.0.1",
"vue": "^2.6.8",
"vue-router": "^3.0.1",
"vue-template-compiler": "^2.6.8",
"webpack": "^4.18.0",
"webpack-bundle-analyzer": "^3.0.3",
"webpack-virtual-modules": "^0.1.10"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
}, },
"dependencies": { "extends": [
"base64-arraybuffer": "^0.2.0", "plugin:vue/recommended",
"intersection-observer": "^0.7.0" "standard"
}, ],
"private": true, "globals": {
"devDependencies": { "App": true,
"@vue/cli-plugin-babel": "^3.4.1", "Page": true,
"@vue/cli-plugin-eslint": "^3.4.1", "Component": true,
"@vue/cli-plugin-unit-mocha": "^3.4.1", "Behavior": true,
"@vue/cli-service": "^3.4.1", "getApp": true,
"@vue/test-utils": "^1.0.0-beta.25", "getCurrentPages": true,
"babel-eslint": "^10.0.1", "plus": true,
"babylon": "^6.18.0", "uni": true,
"browserslist": "^4.4.2", "Vue": true,
"caniuse-lite": "^1.0.30000940", "wx": true,
"chai": "^4.1.2", "my": true,
"copy": "^0.3.2", "swan": true,
"cross-env": "^5.2.0", "__uniConfig": true,
"del": "^5.0.0", "__uniRoutes": true,
"eslint": "^5.5.0", "__registerPage": true,
"eslint-config-standard": "^12.0.0", "UniViewJSBridge": true,
"eslint-loader": "^2.1.0", "UniServiceJSBridge": true,
"eslint-plugin-import": "^2.14.0", "__PLATFORM__": true,
"eslint-plugin-node": "^7.0.1", "__VERSION__": true,
"eslint-plugin-promise": "^4.0.0", "__GLOBAL__": true,
"eslint-plugin-standard": "^4.0.0", "__PLATFORM_TITLE__": true,
"eslint-plugin-vue": "^4.7.1", "__PLATFORM_PREFIX__": true
"jsdom": "^13.0.0",
"jsdom-global": "^3.0.2",
"jsonfile": "^5.0.0",
"rollup": "^1.17.0",
"rollup-plugin-alias": "^1.4.0",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.1.0",
"strip-json-comments": "^2.0.1",
"vue": "^2.6.8",
"vue-router": "^3.0.1",
"vue-template-compiler": "^2.6.8",
"webpack": "^4.18.0",
"webpack-bundle-analyzer": "^3.0.3",
"webpack-virtual-modules": "^0.1.10"
}, },
"eslintConfig": { "rules": {
"root": true, "no-tabs": 0,
"env": { "standard/no-callback-literal": 0
"node": true
},
"extends": [
"plugin:vue/recommended",
"standard"
],
"globals": {
"App": true,
"Page": true,
"Component": true,
"Behavior": true,
"getApp": true,
"getCurrentPages": true,
"plus": true,
"uni": true,
"Vue": true,
"wx": true,
"my": true,
"swan": true,
"__uniConfig": true,
"__uniRoutes": true,
"UniViewJSBridge": true,
"UniServiceJSBridge": true,
"__PLATFORM__": true,
"__VERSION__": true,
"__GLOBAL__": true,
"__PLATFORM_TITLE__": true,
"__PLATFORM_PREFIX__": true
},
"rules": {
"no-tabs": 0,
"standard/no-callback-literal": 0
},
"parserOptions": {
"parser": "babel-eslint"
}
}, },
"browserslist": [ "parserOptions": {
"last 3 versions", "parser": "babel-eslint"
"Android >= 4.1", }
"ios >= 8" },
], "browserslist": [
"license": "Apache-2.0", "last 3 versions",
"main": "index.js", "Android >= 4.1",
"description": "", "ios >= 8"
"author": "" ],
} "license": "Apache-2.0",
"main": "index.js",
"description": "",
"author": ""
}
...@@ -18,132 +18,6 @@ function callPageHook (vm, hook, params) { ...@@ -18,132 +18,6 @@ function callPageHook (vm, hook, params) {
return callHook(vm, hook, params) return callHook(vm, hook, params)
} }
const ANI_DURATION = 300;
const ANI_SHOW = 'pop-in';
function showWebview (webview, animationType, animationDuration) {
setTimeout(() => {
webview.show(
animationType || ANI_SHOW,
animationDuration || ANI_DURATION,
() => {
console.log('show.callback');
}
);
}, 50);
}
let firstBackTime = 0;
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();
}
}
function navigateTo ({
path,
animationType,
animationDuration
}, {
__registerPage
}) {
showWebview(
__registerPage({
path
}),
animationType,
animationDuration
);
}
function reLaunch ({
path
}, instanceContext) {}
function redirectTo ({
path
}, instanceContext) {}
function switchTab ({
path
}, instanceContext) {}
const route = {
navigateBack,
navigateTo,
reLaunch,
redirectTo,
switchTab
};
class Router {
constructor (instanceContext) {
this.instanceContext = instanceContext;
this.$emit = instanceContext.UniServiceJSBridge.emit;
}
push ({
type,
path,
animationType,
animationDuration
} = {}) {
this.$emit('onAppRoute', {
type,
path
});
route[type]({
path,
animationType,
animationDuration
}, this.instanceContext);
}
go (delta, {
animationType,
animationDuration
} = {}) {
delta = Math.abs(parseInt(delta) || 1);
route.navigateBack({
delta,
animationType,
animationDuration
}, this.instanceContext, () => {
this.$emit('onAppRoute', {
type: 'navigateBack'
});
});
}
}
let appCtx; let appCtx;
const NETWORK_TYPES = [ const NETWORK_TYPES = [
...@@ -160,14 +34,15 @@ function getApp () { ...@@ -160,14 +34,15 @@ function getApp () {
return appCtx return appCtx
} }
function initGlobalListeners ({ function initGlobalListeners ({
uni,
plus, plus,
UniServiceJSBridge UniServiceJSBridge
}) { }) {
const emit = UniServiceJSBridge.emit; const emit = UniServiceJSBridge.emit;
plus.key.addEventListener('backbutton', () => { plus.key.addEventListener('backbutton', () => {
appCtx.$router.go(-1); uni.navigateBack();
}); });
plus.globalEvent.addEventListener('pause', () => { plus.globalEvent.addEventListener('pause', () => {
...@@ -207,8 +82,6 @@ function registerApp (appVm, instanceContext) { ...@@ -207,8 +82,6 @@ function registerApp (appVm, instanceContext) {
appCtx = appVm; appCtx = appVm;
appCtx.$router = new Router(instanceContext);
initAppLaunch(appVm, instanceContext); initAppLaunch(appVm, instanceContext);
initGlobalListeners(instanceContext); initGlobalListeners(instanceContext);
...@@ -445,39 +318,6 @@ function initWebview (webview, instanceContext, routeOptions) { ...@@ -445,39 +318,6 @@ function initWebview (webview, instanceContext, routeOptions) {
return webview return webview
} }
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
}
}
const pages = []; const pages = [];
function getCurrentPages () { function getCurrentPages () {
...@@ -531,10 +371,7 @@ function registerPage ({ ...@@ -531,10 +371,7 @@ function registerPage ({
meta: routeOptions.meta, meta: routeOptions.meta,
path, path,
route route
}, }
$holder: createHolder(webview, {
navigationBar: webview.$navigationBar
}, instanceContext)
}); });
return webview return webview
...@@ -587,7 +424,7 @@ function registerConfig (config, { ...@@ -587,7 +424,7 @@ function registerConfig (config, {
function initOn (on, { function initOn (on, {
getApp, getApp,
getCurrentPages getCurrentPages
}) { }) {
function onError (err) { function onError (err) {
callAppHook(getApp(), 'onError', err); callAppHook(getApp(), 'onError', err);
} }
...@@ -673,169 +510,9 @@ function initOn (on, { ...@@ -673,169 +510,9 @@ function initOn (on, {
on('onWebInvokeAppService', onWebInvokeAppService); on('onWebInvokeAppService', onWebInvokeAppService);
} }
let waiting;
let waitingTimeout;
let toast = false;
let toastTimeout;
// 此处设计不对
function initPopup (on, {
plus
}) {
on('onShowToast', showToast);
on('onHideToast', hideToast);
on('onShowLoading', showToast);
on('onHideLoading', hideToast);
on('onShowModal', showModal);
on('onShowActionSheet', showActionSheet);
function showToast ({
title = '',
icon = 'success',
image = '',
duration = 1500,
mask = false,
position = ''
}) {
if (position) {
if (toast) {
toastTimeout && clearTimeout(toastTimeout);
plus.nativeUI.closeToast();
}
if (waiting) {
waitingTimeout && clearTimeout(waitingTimeout);
waiting.close();
}
if (~['top', 'center', 'bottom'].indexOf(position)) {
let richText = `<span>${title}</span>`;
plus.nativeUI.toast(richText, {
verticalAlign: position,
type: 'richtext'
});
toast = true;
toastTimeout = setTimeout(() => {
hideToast();
}, 2000);
return
}
console.warn('uni.showToast 传入的 "position" 值 "' + position + '" 无效');
}
if (duration) {
if (waiting) {
waitingTimeout && clearTimeout(waitingTimeout);
waiting.close();
}
if (toast) {
toastTimeout && clearTimeout(toastTimeout);
plus.nativeUI.closeToast();
}
if (icon && !~['success', 'loading', 'none'].indexOf(icon)) {
icon = 'success';
}
const waitingOptions = {
modal: mask,
back: 'transmit',
padding: '10px',
size: '16px' // 固定字体大小
};
if (!image && (!icon || icon === 'none')) { // 无图
// waitingOptions.width = '120px'
// waitingOptions.height = '40px'
waitingOptions.loading = {
display: 'none'
};
} else { // 有图
waitingOptions.width = '140px';
waitingOptions.height = '112px';
}
if (image) {
waitingOptions.loading = {
display: 'block',
height: '55px',
icon: image,
interval: duration
};
} else {
if (icon === 'success') {
waitingOptions.loading = {
display: 'block',
height: '55px',
icon: '__uniappsuccess.png',
interval: duration
};
}
}
waiting = plus.nativeUI.showWaiting(title, waitingOptions);
waitingTimeout = setTimeout(() => {
hideToast();
}, duration);
}
}
function hideToast () {
if (toast) {
toastTimeout && clearTimeout(toastTimeout);
plus.nativeUI.closeToast();
toast = false;
}
if (waiting) {
waitingTimeout && clearTimeout(waitingTimeout);
waiting.close();
waiting = null;
waitingTimeout = null;
}
}
function showModal ({
title = '',
content = '',
showCancel = true,
cancelText = '取消',
cancelColor = '#000000',
confirmText = '确定',
confirmColor = '#3CC51F'
}, callback) {
plus.nativeUI.confirm(content, (e) => {
if (showCancel) {
callback(e.index === 1 ? 'confirm' : 'cancel');
} else {
callback(e.index === 0 ? 'confirm' : 'cancel');
}
}, title, showCancel ? [cancelText, confirmText] : [confirmText]);
}
function showActionSheet ({
itemList = [],
itemColor = '#000000',
title = ''
}, callback) {
const options = {
buttons: itemList.map(item => ({
title: item
}))
};
if (title) {
options.title = title;
}
if (plus.os.name === 'iOS') {
options.cancel = '取消';
}
plus.nativeUI.actionSheet(options, (e) => {
if (e.index > 0) {
callback(e.index - 1);
} else {
callback(-1);
}
});
}
}
let bridge; let bridge;
function initServiceJSBridge (Vue, instanceContext) { function initServiceJSBridge (Vue, instanceContext) {
if (bridge) { if (bridge) {
return bridge return bridge
} }
...@@ -850,7 +527,6 @@ function initServiceJSBridge (Vue, instanceContext) { ...@@ -850,7 +527,6 @@ function initServiceJSBridge (Vue, instanceContext) {
}; };
initOn(bridge.on, instanceContext); initOn(bridge.on, instanceContext);
initPopup(bridge.on, instanceContext);
return bridge return bridge
} }
...@@ -871,12 +547,17 @@ function createInstanceContext (instanceContext) { ...@@ -871,12 +547,17 @@ function createInstanceContext (instanceContext) {
getCurrentPages getCurrentPages
}); });
function __registerPage (page) {
return registerPage(page, instanceContext)
}
if (!uni$1) { if (!uni$1) {
uni$1 = createUniInstance( uni$1 = createUniInstance(
weex, weex,
plus, plus,
uniConfig, uniConfig,
uniRoutes, uniRoutes,
__registerPage,
UniServiceJSBridge, UniServiceJSBridge,
getApp, getApp,
getCurrentPages getCurrentPages
...@@ -892,9 +573,7 @@ function createInstanceContext (instanceContext) { ...@@ -892,9 +573,7 @@ function createInstanceContext (instanceContext) {
__registerApp (appVm) { __registerApp (appVm) {
return registerApp(appVm, instanceContext) return registerApp(appVm, instanceContext)
}, },
__registerPage (page) { __registerPage,
return registerPage(page, instanceContext)
},
plus, plus,
uni: uni$1, uni: uni$1,
getApp, getApp,
......
<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
import Vue from 'vue' import Vue from 'vue'
import initOn from './on'
import initSubscribe from './subscribe'
const Emitter = new Vue() const Emitter = new Vue()
export const on = Emitter.$on.bind(Emitter) export const on = Emitter.$on.bind(Emitter)
...@@ -30,11 +27,4 @@ export function subscribeHandler (event, args, pageId) { ...@@ -30,11 +27,4 @@ export function subscribeHandler (event, args, pageId) {
export { export {
publishHandler publishHandler
} }
from 'uni-platform/service/bridge' from 'uni-platform/service/bridge'
initOn(on, {
getApp: global.getApp,
getCurrentPages: global.getCurrentPages
})
initSubscribe(subscribe)
...@@ -10,7 +10,7 @@ import { ...@@ -10,7 +10,7 @@ import {
export default function initOn (on, { export default function initOn (on, {
getApp, getApp,
getCurrentPages getCurrentPages
}) { }) {
function onError (err) { function onError (err) {
callAppHook(getApp(), 'onError', err) callAppHook(getApp(), 'onError', err)
} }
......
...@@ -4,52 +4,55 @@ import { ...@@ -4,52 +4,55 @@ import {
callPageHook callPageHook
} from '../plugins/util' } from '../plugins/util'
function createPageEvent (eventType) { export default function initSubscribe (subscribe, {
return function (args, pageId) { getApp,
const pages = getCurrentPages() getCurrentPages
const page = pages.find(page => page.$page.id === pageId) }) {
if (page) { function createPageEvent (eventType) {
callPageHook(page, eventType, args) return function (args, pageId) {
} else { const pages = getCurrentPages()
console.error(`Not Found:Page[${pageId}]`) const page = pages.find(page => page.$page.id === pageId)
if (page) {
callPageHook(page, eventType, args)
} else {
console.error(`Not Found:Page[${pageId}]`)
}
} }
} }
}
const requestComponentInfoCallbacks = createCallbacks('requestComponentInfo') const requestComponentInfoCallbacks = createCallbacks('requestComponentInfo')
function onRequestComponentInfo ({ function onRequestComponentInfo ({
reqId, reqId,
res res
}) { }) {
const callback = requestComponentInfoCallbacks.pop(reqId) const callback = requestComponentInfoCallbacks.pop(reqId)
if (callback) { if (callback) {
callback(res) callback(res)
}
} }
}
const requestComponentObserverCallbacks = createCallbacks('requestComponentObserver') const requestComponentObserverCallbacks = createCallbacks('requestComponentObserver')
function onRequestComponentObserver ({ function onRequestComponentObserver ({
reqId, reqId,
reqEnd, reqEnd,
res res
}) { }) {
const callback = requestComponentObserverCallbacks.get(reqId) const callback = requestComponentObserverCallbacks.get(reqId)
if (callback) { if (callback) {
if (reqEnd) { if (reqEnd) {
requestComponentObserverCallbacks.pop(reqId) requestComponentObserverCallbacks.pop(reqId)
return return
}
callback(res)
} }
callback(res)
} }
}
export default function initSubscribe (subscribe) {
subscribe('onPageReady', createPageEvent('onReady')) subscribe('onPageReady', createPageEvent('onReady'))
subscribe('onPageScroll', createPageEvent('onPageScroll')) subscribe('onPageScroll', createPageEvent('onPageScroll'))
subscribe('onReachBottom', createPageEvent('onReachBottom')) subscribe('onReachBottom', createPageEvent('onReachBottom'))
subscribe('onRequestComponentInfo', onRequestComponentInfo) subscribe('onRequestComponentInfo', onRequestComponentInfo)
subscribe('onRequestComponentObserver', onRequestComponentObserver) subscribe('onRequestComponentObserver', onRequestComponentObserver)
} }
import apis from 'uni-helpers/apis' import initOn from './bridge/on'
import initSubscribe from './bridge/subscribe'
import { import {
wrapper, uni
wrapperUnimplemented } from './uni'
} from 'uni-helpers/api'
import { import {
promisify getApp,
} from 'uni-helpers/promise' getCurrentPages
}
import { from './plugins/app'
api
} from './api'
const uni = Object.create(null)
/* eslint-disable no-undef */ initOn(UniServiceJSBridge.on, {
uni.version = __VERSION__ getApp,
getCurrentPages
})
apis.forEach(name => { initSubscribe(UniServiceJSBridge.subscribe, {
if (api[name]) { getApp,
uni[name] = promisify(name, wrapper(name, api[name])) getCurrentPages
} else {
uni[name] = wrapperUnimplemented(name)
}
}) })
export default uni
export { export {
getApp, getApp,
getCurrentPages getCurrentPages
} }
from './plugins/app' from './plugins/app'
export default uni
...@@ -2,7 +2,7 @@ import VueRouter from 'vue-router' ...@@ -2,7 +2,7 @@ import VueRouter from 'vue-router'
import { import {
isPage isPage
} from 'uni-helpers' } from 'uni-helpers/index'
import { import {
createAppMixin createAppMixin
......
export const api = Object.create(null) import apis from 'uni-helpers/apis'
import {
wrapper,
wrapperUnimplemented
} from 'uni-helpers/api'
import {
promisify
} from 'uni-helpers/promise'
const api = Object.create(null)
export const uni = Object.create(null)
const baseApis = require.context( const baseApis = require.context(
'./api', './api',
true, true,
/\.js$/ /\.js$/
) )
baseApis.keys().forEach(function (key) { baseApis.keys().forEach(function (key) {
Object.assign(api, baseApis(key)) Object.assign(api, baseApis(key))
}) })
...@@ -13,12 +25,22 @@ const platformApis = require.context( ...@@ -13,12 +25,22 @@ const platformApis = require.context(
'../../platforms/' + __PLATFORM__ + '/service/api', '../../platforms/' + __PLATFORM__ + '/service/api',
true, true,
/\.js$/ /\.js$/
) )
platformApis.keys().forEach(function (key) { platformApis.keys().forEach(function (key) {
Object.assign(api, platformApis(key)) Object.assign(api, platformApis(key))
}) })
/* eslint-disable no-undef */
uni.version = __VERSION__
apis.forEach(name => {
if (api[name]) {
uni[name] = promisify(name, wrapper(name, api[name]))
} else {
uni[name] = wrapperUnimplemented(name)
}
})
export function invokeMethod (name, ...args) { export function invokeMethod (name, ...args) {
return api[name](...args) return api[name](...args)
} }
import { import {
normalizeDataset normalizeDataset
} from 'uni-helpers' } from 'uni-helpers/index'
import getWindowOffset from 'uni-platform/helpers/get-window-offset' import getWindowOffset from 'uni-platform/helpers/get-window-offset'
......
...@@ -2,7 +2,7 @@ import 'intersection-observer' ...@@ -2,7 +2,7 @@ import 'intersection-observer'
import { import {
normalizeDataset normalizeDataset
} from 'uni-helpers' } from 'uni-helpers/index'
function getRect (rect) { function getRect (rect) {
return { return {
......
...@@ -4,7 +4,7 @@ import { ...@@ -4,7 +4,7 @@ import {
import { import {
normalizeDataset normalizeDataset
} from 'uni-helpers' } from 'uni-helpers/index'
import { import {
wrapperMPEvent wrapperMPEvent
......
import { import {
isPage isPage
} from 'uni-helpers' } from 'uni-helpers/index'
import { import {
initEvents, initEvents,
......
export function publishHandler (event, args, pageId) {
// TODO
}
...@@ -4,14 +4,11 @@ import { ...@@ -4,14 +4,11 @@ import {
let firstBackTime = 0 let firstBackTime = 0
export default function navigateBack ({ export function navigateBack ({
delta, delta,
animationType, animationType,
animationDuration animationDuration
}, { }) {
plus,
getCurrentPages
}, callback) {
const pages = getCurrentPages() const pages = getCurrentPages()
const len = pages.length - 1 const len = pages.length - 1
const page = pages[len] const page = pages[len]
...@@ -32,6 +29,8 @@ export default function navigateBack ({ ...@@ -32,6 +29,8 @@ export default function navigateBack ({
} else { } else {
page.$getAppWebview().close('auto') page.$getAppWebview().close('auto')
} }
callback && callback() UniServiceJSBridge.emit('onAppRoute', {
type: 'navigateBack'
})
} }
} }
...@@ -2,13 +2,18 @@ import { ...@@ -2,13 +2,18 @@ import {
showWebview showWebview
} from './util' } from './util'
export default function navigateTo ({ export function navigateTo ({
path, url,
animationType, animationType,
animationDuration animationDuration
}, {
__registerPage
}) { }) {
const path = url.split('?')[0]
UniServiceJSBridge.emit('onAppRoute', {
type: 'navigateTo',
path
})
showWebview( showWebview(
__registerPage({ __registerPage({
path path
......
export function publishHandler (event, args, pageId) {
// TODO
}
...@@ -2,8 +2,6 @@ import { ...@@ -2,8 +2,6 @@ import {
callAppHook callAppHook
} from 'uni-core/service/plugins/util' } from 'uni-core/service/plugins/util'
import Router from './router/index'
let appCtx let appCtx
const NETWORK_TYPES = [ const NETWORK_TYPES = [
...@@ -20,14 +18,15 @@ export function getApp () { ...@@ -20,14 +18,15 @@ export function getApp () {
return appCtx return appCtx
} }
function initGlobalListeners ({ function initGlobalListeners ({
uni,
plus, plus,
UniServiceJSBridge UniServiceJSBridge
}) { }) {
const emit = UniServiceJSBridge.emit const emit = UniServiceJSBridge.emit
plus.key.addEventListener('backbutton', () => { plus.key.addEventListener('backbutton', () => {
appCtx.$router.go(-1) uni.navigateBack()
}) })
plus.globalEvent.addEventListener('pause', () => { plus.globalEvent.addEventListener('pause', () => {
...@@ -67,8 +66,6 @@ export function registerApp (appVm, instanceContext) { ...@@ -67,8 +66,6 @@ export function registerApp (appVm, instanceContext) {
appCtx = appVm appCtx = appVm
appCtx.$router = new Router(instanceContext)
initAppLaunch(appVm, instanceContext) initAppLaunch(appVm, instanceContext)
initGlobalListeners(instanceContext) initGlobalListeners(instanceContext)
......
import initOn from 'uni-core/service/bridge/on' import initOn from 'uni-core/service/bridge/on'
import initPopup from './popup'
let bridge let bridge
export function initServiceJSBridge (Vue, instanceContext) { export function initServiceJSBridge (Vue, instanceContext) {
if (bridge) { if (bridge) {
return bridge return bridge
} }
...@@ -18,7 +17,6 @@ export function initServiceJSBridge (Vue, instanceContext) { ...@@ -18,7 +17,6 @@ export function initServiceJSBridge (Vue, instanceContext) {
} }
initOn(bridge.on, instanceContext) initOn(bridge.on, instanceContext)
initPopup(bridge.on, instanceContext)
return bridge return bridge
} }
...@@ -38,12 +38,17 @@ export function createInstanceContext (instanceContext) { ...@@ -38,12 +38,17 @@ export function createInstanceContext (instanceContext) {
getCurrentPages getCurrentPages
}) })
function __registerPage (page) {
return registerPage(page, instanceContext)
}
if (!uni) { if (!uni) {
uni = createUniInstance( uni = createUniInstance(
weex, weex,
plus, plus,
uniConfig, uniConfig,
uniRoutes, uniRoutes,
__registerPage,
UniServiceJSBridge, UniServiceJSBridge,
getApp, getApp,
getCurrentPages getCurrentPages
...@@ -59,9 +64,7 @@ export function createInstanceContext (instanceContext) { ...@@ -59,9 +64,7 @@ export function createInstanceContext (instanceContext) {
__registerApp (appVm) { __registerApp (appVm) {
return registerApp(appVm, instanceContext) return registerApp(appVm, instanceContext)
}, },
__registerPage (page) { __registerPage,
return registerPage(page, instanceContext)
},
plus, plus,
uni, uni,
getApp, getApp,
......
...@@ -3,10 +3,6 @@ import { ...@@ -3,10 +3,6 @@ import {
createWebview createWebview
} from './webview/index' } from './webview/index'
import {
createHolder
} from './holder'
const pages = [] const pages = []
export function getCurrentPages () { export function getCurrentPages () {
...@@ -60,10 +56,7 @@ export function registerPage ({ ...@@ -60,10 +56,7 @@ export function registerPage ({
meta: routeOptions.meta, meta: routeOptions.meta,
path, path,
route route
}, }
$holder: createHolder(webview, {
navigationBar: webview.$navigationBar
}, instanceContext)
}) })
return webview return webview
......
import apis from 'uni-helpers/apis'
import {
wrapper,
wrapperUnimplemented
} from 'uni-helpers/api'
import {
promisify
} from 'uni-helpers/promise'
const api = Object.create(null)
const uni = Object.create(null)
const baseApis = require.context(
'../../../core/service/api',
true,
/\.js$/
)
baseApis.keys().forEach(function (key) {
Object.assign(api, baseApis(key))
})
const platformApis = require.context(
'./api',
true,
/\.js$/
)
platformApis.keys().forEach(function (key) {
Object.assign(api, platformApis(key))
})
apis.forEach(name => {
if (api[name]) {
uni[name] = promisify(name, wrapper(name, api[name]))
} else {
uni[name] = wrapperUnimplemented(name)
}
})
import './polyfill'
import {
wrapper
} from 'uni-helpers/api'
import {
promisify
} from 'uni-helpers/promise'
import api from 'uni-core/service/api'
const uni = Object.create(null)
Object.keys(api).forEach(name => {
uni[name] = promisify(name, wrapper(name, api[name]))
})
let waiting
let waitingTimeout
let toast = false
let toastTimeout
// 此处设计不对
export default function initPopup (on, {
plus
}) {
on('onShowToast', showToast)
on('onHideToast', hideToast)
on('onShowLoading', showToast)
on('onHideLoading', hideToast)
on('onShowModal', showModal)
on('onShowActionSheet', showActionSheet)
function showToast ({
title = '',
icon = 'success',
image = '',
duration = 1500,
mask = false,
position = ''
}) {
if (position) {
if (toast) {
toastTimeout && clearTimeout(toastTimeout)
plus.nativeUI.closeToast()
}
if (waiting) {
waitingTimeout && clearTimeout(waitingTimeout)
waiting.close()
}
if (~['top', 'center', 'bottom'].indexOf(position)) {
let richText = `<span>${title}</span>`
plus.nativeUI.toast(richText, {
verticalAlign: position,
type: 'richtext'
})
toast = true
toastTimeout = setTimeout(() => {
hideToast()
}, 2000)
return
}
console.warn('uni.showToast 传入的 "position" 值 "' + position + '" 无效')
}
if (duration) {
if (waiting) {
waitingTimeout && clearTimeout(waitingTimeout)
waiting.close()
}
if (toast) {
toastTimeout && clearTimeout(toastTimeout)
plus.nativeUI.closeToast()
}
if (icon && !~['success', 'loading', 'none'].indexOf(icon)) {
icon = 'success'
}
const waitingOptions = {
modal: mask,
back: 'transmit',
padding: '10px',
size: '16px' // 固定字体大小
}
if (!image && (!icon || icon === 'none')) { // 无图
// waitingOptions.width = '120px'
// waitingOptions.height = '40px'
waitingOptions.loading = {
display: 'none'
}
} else { // 有图
waitingOptions.width = '140px'
waitingOptions.height = '112px'
}
if (image) {
waitingOptions.loading = {
display: 'block',
height: '55px',
icon: image,
interval: duration
}
} else {
if (icon === 'success') {
waitingOptions.loading = {
display: 'block',
height: '55px',
icon: '__uniappsuccess.png',
interval: duration
}
}
}
waiting = plus.nativeUI.showWaiting(title, waitingOptions)
waitingTimeout = setTimeout(() => {
hideToast()
}, duration)
}
}
function hideToast () {
if (toast) {
toastTimeout && clearTimeout(toastTimeout)
plus.nativeUI.closeToast()
toast = false
}
if (waiting) {
waitingTimeout && clearTimeout(waitingTimeout)
waiting.close()
waiting = null
waitingTimeout = null
}
}
function showModal ({
title = '',
content = '',
showCancel = true,
cancelText = '取消',
cancelColor = '#000000',
confirmText = '确定',
confirmColor = '#3CC51F'
}, callback) {
plus.nativeUI.confirm(content, (e) => {
if (showCancel) {
callback(e.index === 1 ? 'confirm' : 'cancel')
} else {
callback(e.index === 0 ? 'confirm' : 'cancel')
}
}, title, showCancel ? [cancelText, confirmText] : [confirmText])
}
function showActionSheet ({
itemList = [],
itemColor = '#000000',
title = ''
}, callback) {
const options = {
buttons: itemList.map(item => ({
title: item
}))
}
if (title) {
options.title = title
}
if (plus.os.name === 'iOS') {
options.cancel = '取消'
}
plus.nativeUI.actionSheet(options, (e) => {
if (e.index > 0) {
callback(e.index - 1)
} else {
callback(-1)
}
})
}
}
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'
const route = {
navigateBack,
navigateTo,
reLaunch,
redirectTo,
switchTab
}
export default class Router {
constructor (instanceContext) {
this.instanceContext = instanceContext
this.$emit = instanceContext.UniServiceJSBridge.emit
}
push ({
type,
path,
animationType,
animationDuration
} = {}) {
this.$emit('onAppRoute', {
type,
path
})
route[type]({
path,
animationType,
animationDuration
}, this.instanceContext)
}
go (delta, {
animationType,
animationDuration
} = {}) {
delta = Math.abs(parseInt(delta) || 1)
route.navigateBack({
delta,
animationType,
animationDuration
}, this.instanceContext, () => {
this.$emit('onAppRoute', {
type: 'navigateBack'
})
})
}
}
export default function reLaunch ({
path
}, instanceContext) {}
export default function redirectTo ({
path
}, instanceContext) {}
export default function switchTab ({
path
}, instanceContext) {}
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
<script> <script>
import { import {
upx2px upx2px
} from 'uni-helpers' } from 'uni-helpers/index'
import { import {
NAVBAR_HEIGHT NAVBAR_HEIGHT
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册