You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* mui back 5+
* @param {type} $
* @param {type} window
* @returns {undefined}
*/
(function($, window) {
if ($.os.plus && $.os.android) {
$.addBack({
name: 'mui',
index: 5,
handle: function() {
//后续重新设计此处将back放到各个空间内部实现
//popover
if ($.targets._popover && $.targets._popover.classList.contains($.className('active'))) {
$($.targets._popover).popover('hide');
return true;
}
//offcanvas
var offCanvas = document.querySelector($.classSelector('.off-canvas-wrap.active'));
if (offCanvas) {
$(offCanvas).offCanvas('close');
return true;
}
var previewImage = $.isFunction($.getPreviewImage) && $.getPreviewImage();
if (previewImage && previewImage.isShown()) {
previewImage.close();
return true;
}
//popup
return $.closePopup();
}
});
}
//首次按下back按键的时间
$.__back__first = null;
/**
* 5+ back
*/
$.addBack({
name: '5+',
index: 10,
handle: function() {
if (!window.plus) {
return false;
}
var wobj = plus.webview.currentWebview();
var parent = wobj.parent();
if (parent) {
parent.evalJS('mui&&mui.back();');
} else {
wobj.canBack(function(e) {
//by chb 暂时注释在碰到类似popover之类的锚点的时候需多次点击才能返回
if (e.canBack) { //webview history back
window.history.back();
} else { //webview close or hide
//fixed by fxy 此处不应该用opener判断因为用户有可能自己close掉当前窗口的opener。这样的话。opener就为空了导致不能执行close
if (wobj.id === plus.runtime.appid) { //首页
//首页不存在opener的情况下后退实际上应该是退出应用
//首次按键,提示‘再按一次退出应用’
if (!$.__back__first) {
$.__back__first = new Date().getTime();
mui.toast('再按一次退出应用');
setTimeout(function() {
$.__back__first = null;
}, 2000);
} else {
if (new Date().getTime() - $.__back__first < 2000) {
plus.runtime.quit();
}
}
} else { //其他页面,
if (wobj.preload) {
wobj.hide("auto");
} else {
//关闭页面时,需要将其打开的所有子页面全部关闭;
$.closeAll(wobj);
}
}
}
});
}
return true;
}
});
$.menu = function() {
var menu = document.querySelector($.classSelector('.action-menu'));
if (menu) {
$.trigger(menu, $.EVENT_START); //临时处理menu无touchstart的话找不到当前targets的问题
$.trigger(menu, 'tap');
} else { //执行父窗口的menu
if (window.plus) {
var wobj = $.currentWebview;
var parent = wobj.parent();
if (parent) { //又得evalJS
parent.evalJS('mui&&mui.menu();');
}
}
}
};
var __back = function() {
$.back();
};
var __menu = function() {
$.menu();
};
//默认监听
$.plusReady(function() {
if ($.options.keyEventBind.backbutton) {
plus.key.addEventListener('backbutton', __back, false);
}
if ($.options.keyEventBind.menubutton) {
plus.key.addEventListener('menubutton', __menu, false);
}
});
//处理按键监听事件
$.addInit({
name: 'keyEventBind',
index: 1000,
handle: function() {
$.plusReady(function() {
//如果不为true则移除默认监听
if (!$.options.keyEventBind.backbutton) {
plus.key.removeEventListener('backbutton', __back);
}
if (!$.options.keyEventBind.menubutton) {
plus.key.removeEventListener('menubutton', __menu);
}
});
}
});
})(mui, window);