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

refactor app api

上级 60e8e2e2
......@@ -5,24 +5,28 @@ const resolve = require('rollup-plugin-node-resolve')
const commonjs = require('rollup-plugin-commonjs')
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 = {
file: 'packages/uni-app-plus-nvue/dist/index.js',
format: 'es'
}
const external = []
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'
} 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.banner =
`export function createUniInstance(weex, plus, __uniConfig, __uniRoutes, UniServiceJSBridge, getApp, getCurrentPages){
var localStorage = plus.storage
output.banner =
`export function createUniInstance(weex, plus, __uniConfig, __uniRoutes, __registerPage, UniServiceJSBridge, getApp, getCurrentPages){
var localStorage = plus.storage
`
output.footer = '\n return uni$1 \n}'
} else {
external.push('./uni')
}
module.exports = {
......@@ -34,16 +38,16 @@ module.exports = {
requireContext(),
alias({
'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-shared': path.resolve(__dirname, '../src/shared/util.js'),
'uni-helpers': path.resolve(__dirname, '../src/core/helpers')
}),
replace({
__GLOBAL__: 'getGlobalUni()',
__PLATFORM__: JSON.stringify(process.env.UNI_PLATFORM),
__PLATFORM__: JSON.stringify('app-plus'),
__PLATFORM_TITLE__: 'app-plus-nvue'
})
],
external: ['./uni']
external
}
{
"name": "uniapp-js-framework",
"version": "0.0.1",
"scripts": {
"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.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"
"name": "uniapp-js-framework",
"version": "0.0.1",
"scripts": {
"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.mp.js",
"build:app:all": "npm run lint && npm run build:app:uni && npm run build:app:nvue && npm run build:app:legacy",
"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: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"
},
"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": {
"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"
"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,
"__registerPage": true,
"UniViewJSBridge": true,
"UniServiceJSBridge": true,
"__PLATFORM__": true,
"__VERSION__": true,
"__GLOBAL__": true,
"__PLATFORM_TITLE__": true,
"__PLATFORM_PREFIX__": true
},
"eslintConfig": {
"root": true,
"env": {
"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"
}
"rules": {
"no-tabs": 0,
"standard/no-callback-literal": 0
},
"browserslist": [
"last 3 versions",
"Android >= 4.1",
"ios >= 8"
],
"license": "Apache-2.0",
"main": "index.js",
"description": "",
"author": ""
}
"parserOptions": {
"parser": "babel-eslint"
}
},
"browserslist": [
"last 3 versions",
"Android >= 4.1",
"ios >= 8"
],
"license": "Apache-2.0",
"main": "index.js",
"description": "",
"author": ""
}
......@@ -18,132 +18,6 @@ function callPageHook (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;
const NETWORK_TYPES = [
......@@ -160,14 +34,15 @@ function getApp () {
return appCtx
}
function initGlobalListeners ({
function initGlobalListeners ({
uni,
plus,
UniServiceJSBridge
}) {
const emit = UniServiceJSBridge.emit;
plus.key.addEventListener('backbutton', () => {
appCtx.$router.go(-1);
uni.navigateBack();
});
plus.globalEvent.addEventListener('pause', () => {
......@@ -207,8 +82,6 @@ function registerApp (appVm, instanceContext) {
appCtx = appVm;
appCtx.$router = new Router(instanceContext);
initAppLaunch(appVm, instanceContext);
initGlobalListeners(instanceContext);
......@@ -445,39 +318,6 @@ function initWebview (webview, instanceContext, routeOptions) {
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 = [];
function getCurrentPages () {
......@@ -531,10 +371,7 @@ function registerPage ({
meta: routeOptions.meta,
path,
route
},
$holder: createHolder(webview, {
navigationBar: webview.$navigationBar
}, instanceContext)
}
});
return webview
......@@ -587,7 +424,7 @@ function registerConfig (config, {
function initOn (on, {
getApp,
getCurrentPages
}) {
}) {
function onError (err) {
callAppHook(getApp(), 'onError', err);
}
......@@ -673,169 +510,9 @@ function initOn (on, {
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;
function initServiceJSBridge (Vue, instanceContext) {
function initServiceJSBridge (Vue, instanceContext) {
if (bridge) {
return bridge
}
......@@ -850,7 +527,6 @@ function initServiceJSBridge (Vue, instanceContext) {
};
initOn(bridge.on, instanceContext);
initPopup(bridge.on, instanceContext);
return bridge
}
......@@ -871,12 +547,17 @@ function createInstanceContext (instanceContext) {
getCurrentPages
});
function __registerPage (page) {
return registerPage(page, instanceContext)
}
if (!uni$1) {
uni$1 = createUniInstance(
weex,
plus,
uniConfig,
uniRoutes,
__registerPage,
UniServiceJSBridge,
getApp,
getCurrentPages
......@@ -892,9 +573,7 @@ function createInstanceContext (instanceContext) {
__registerApp (appVm) {
return registerApp(appVm, instanceContext)
},
__registerPage (page) {
return registerPage(page, instanceContext)
},
__registerPage,
plus,
uni: uni$1,
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 initOn from './on'
import initSubscribe from './subscribe'
const Emitter = new Vue()
export const on = Emitter.$on.bind(Emitter)
......@@ -30,11 +27,4 @@ export function subscribeHandler (event, args, pageId) {
export {
publishHandler
}
from 'uni-platform/service/bridge'
initOn(on, {
getApp: global.getApp,
getCurrentPages: global.getCurrentPages
})
initSubscribe(subscribe)
from 'uni-platform/service/bridge'
......@@ -10,7 +10,7 @@ import {
export default function initOn (on, {
getApp,
getCurrentPages
}) {
}) {
function onError (err) {
callAppHook(getApp(), 'onError', err)
}
......
......@@ -4,52 +4,55 @@ import {
callPageHook
} from '../plugins/util'
function createPageEvent (eventType) {
return function (args, pageId) {
const pages = getCurrentPages()
const page = pages.find(page => page.$page.id === pageId)
if (page) {
callPageHook(page, eventType, args)
} else {
console.error(`Not Found:Page[${pageId}]`)
export default function initSubscribe (subscribe, {
getApp,
getCurrentPages
}) {
function createPageEvent (eventType) {
return function (args, pageId) {
const pages = getCurrentPages()
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 ({
reqId,
res
}) {
const callback = requestComponentInfoCallbacks.pop(reqId)
if (callback) {
callback(res)
function onRequestComponentInfo ({
reqId,
res
}) {
const callback = requestComponentInfoCallbacks.pop(reqId)
if (callback) {
callback(res)
}
}
}
const requestComponentObserverCallbacks = createCallbacks('requestComponentObserver')
function onRequestComponentObserver ({
reqId,
reqEnd,
res
}) {
const callback = requestComponentObserverCallbacks.get(reqId)
if (callback) {
if (reqEnd) {
requestComponentObserverCallbacks.pop(reqId)
return
const requestComponentObserverCallbacks = createCallbacks('requestComponentObserver')
function onRequestComponentObserver ({
reqId,
reqEnd,
res
}) {
const callback = requestComponentObserverCallbacks.get(reqId)
if (callback) {
if (reqEnd) {
requestComponentObserverCallbacks.pop(reqId)
return
}
callback(res)
}
callback(res)
}
}
export default function initSubscribe (subscribe) {
subscribe('onPageReady', createPageEvent('onReady'))
subscribe('onPageScroll', createPageEvent('onPageScroll'))
subscribe('onReachBottom', createPageEvent('onReachBottom'))
subscribe('onRequestComponentInfo', onRequestComponentInfo)
subscribe('onRequestComponentObserver', onRequestComponentObserver)
}
}
import apis from 'uni-helpers/apis'
import initOn from './bridge/on'
import initSubscribe from './bridge/subscribe'
import {
wrapper,
wrapperUnimplemented
} from 'uni-helpers/api'
uni
} from './uni'
import {
promisify
} from 'uni-helpers/promise'
import {
api
} from './api'
const uni = Object.create(null)
getApp,
getCurrentPages
}
from './plugins/app'
/* eslint-disable no-undef */
uni.version = __VERSION__
initOn(UniServiceJSBridge.on, {
getApp,
getCurrentPages
})
apis.forEach(name => {
if (api[name]) {
uni[name] = promisify(name, wrapper(name, api[name]))
} else {
uni[name] = wrapperUnimplemented(name)
}
initSubscribe(UniServiceJSBridge.subscribe, {
getApp,
getCurrentPages
})
export default uni
export {
getApp,
getCurrentPages
}
from './plugins/app'
export default uni
from './plugins/app'
......@@ -2,7 +2,7 @@ import VueRouter from 'vue-router'
import {
isPage
} from 'uni-helpers'
} from 'uni-helpers/index'
import {
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(
'./api',
true,
/\.js$/
)
baseApis.keys().forEach(function (key) {
Object.assign(api, baseApis(key))
})
......@@ -13,12 +25,22 @@ const platformApis = require.context(
'../../platforms/' + __PLATFORM__ + '/service/api',
true,
/\.js$/
)
)
platformApis.keys().forEach(function (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) {
return api[name](...args)
}
import {
normalizeDataset
} from 'uni-helpers'
} from 'uni-helpers/index'
import getWindowOffset from 'uni-platform/helpers/get-window-offset'
......
......@@ -2,7 +2,7 @@ import 'intersection-observer'
import {
normalizeDataset
} from 'uni-helpers'
} from 'uni-helpers/index'
function getRect (rect) {
return {
......
......@@ -4,7 +4,7 @@ import {
import {
normalizeDataset
} from 'uni-helpers'
} from 'uni-helpers/index'
import {
wrapperMPEvent
......
import {
isPage
} from 'uni-helpers'
} from 'uni-helpers/index'
import {
initEvents,
......
export function publishHandler (event, args, pageId) {
// TODO
}
......@@ -4,14 +4,11 @@ import {
let firstBackTime = 0
export default function navigateBack ({
export function navigateBack ({
delta,
animationType,
animationDuration
}, {
plus,
getCurrentPages
}, callback) {
}) {
const pages = getCurrentPages()
const len = pages.length - 1
const page = pages[len]
......@@ -32,6 +29,8 @@ export default function navigateBack ({
} else {
page.$getAppWebview().close('auto')
}
callback && callback()
UniServiceJSBridge.emit('onAppRoute', {
type: 'navigateBack'
})
}
}
......@@ -2,13 +2,18 @@ import {
showWebview
} from './util'
export default function navigateTo ({
path,
export function navigateTo ({
url,
animationType,
animationDuration
}, {
__registerPage
}) {
const path = url.split('?')[0]
UniServiceJSBridge.emit('onAppRoute', {
type: 'navigateTo',
path
})
showWebview(
__registerPage({
path
......
export function publishHandler (event, args, pageId) {
// TODO
}
......@@ -2,8 +2,6 @@ import {
callAppHook
} from 'uni-core/service/plugins/util'
import Router from './router/index'
let appCtx
const NETWORK_TYPES = [
......@@ -20,14 +18,15 @@ export function getApp () {
return appCtx
}
function initGlobalListeners ({
function initGlobalListeners ({
uni,
plus,
UniServiceJSBridge
}) {
const emit = UniServiceJSBridge.emit
plus.key.addEventListener('backbutton', () => {
appCtx.$router.go(-1)
uni.navigateBack()
})
plus.globalEvent.addEventListener('pause', () => {
......@@ -67,8 +66,6 @@ export function registerApp (appVm, instanceContext) {
appCtx = appVm
appCtx.$router = new Router(instanceContext)
initAppLaunch(appVm, instanceContext)
initGlobalListeners(instanceContext)
......
import initOn from 'uni-core/service/bridge/on'
import initPopup from './popup'
let bridge
export function initServiceJSBridge (Vue, instanceContext) {
export function initServiceJSBridge (Vue, instanceContext) {
if (bridge) {
return bridge
}
......@@ -18,7 +17,6 @@ export function initServiceJSBridge (Vue, instanceContext) {
}
initOn(bridge.on, instanceContext)
initPopup(bridge.on, instanceContext)
return bridge
}
......@@ -38,12 +38,17 @@ export function createInstanceContext (instanceContext) {
getCurrentPages
})
function __registerPage (page) {
return registerPage(page, instanceContext)
}
if (!uni) {
uni = createUniInstance(
weex,
plus,
uniConfig,
uniRoutes,
__registerPage,
UniServiceJSBridge,
getApp,
getCurrentPages
......@@ -59,9 +64,7 @@ export function createInstanceContext (instanceContext) {
__registerApp (appVm) {
return registerApp(appVm, instanceContext)
},
__registerPage (page) {
return registerPage(page, instanceContext)
},
__registerPage,
plus,
uni,
getApp,
......
......@@ -3,10 +3,6 @@ import {
createWebview
} from './webview/index'
import {
createHolder
} from './holder'
const pages = []
export function getCurrentPages () {
......@@ -60,10 +56,7 @@ export function registerPage ({
meta: routeOptions.meta,
path,
route
},
$holder: createHolder(webview, {
navigationBar: webview.$navigationBar
}, instanceContext)
}
})
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 @@
<script>
import {
upx2px
} from 'uni-helpers'
} from 'uni-helpers/index'
import {
NAVBAR_HEIGHT
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册