mui.back.5+.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * mui back 5+
  3. * @param {type} $
  4. * @param {type} window
  5. * @returns {undefined}
  6. */
  7. (function($, window) {
  8. if ($.os.plus && $.os.android) {
  9. $.addBack({
  10. name: 'mui',
  11. index: 5,
  12. handle: function() {
  13. //后续重新设计此处,将back放到各个空间内部实现
  14. //popover
  15. if ($.targets._popover && $.targets._popover.classList.contains($.className('active'))) {
  16. $($.targets._popover).popover('hide');
  17. return true;
  18. }
  19. //offcanvas
  20. var offCanvas = document.querySelector($.classSelector('.off-canvas-wrap.active'));
  21. if (offCanvas) {
  22. $(offCanvas).offCanvas('close');
  23. return true;
  24. }
  25. var previewImage = $.isFunction($.getPreviewImage) && $.getPreviewImage();
  26. if (previewImage && previewImage.isShown()) {
  27. previewImage.close();
  28. return true;
  29. }
  30. //popup
  31. return $.closePopup();
  32. }
  33. });
  34. }
  35. //首次按下back按键的时间
  36. $.__back__first = null;
  37. /**
  38. * 5+ back
  39. */
  40. $.addBack({
  41. name: '5+',
  42. index: 10,
  43. handle: function() {
  44. if (!window.plus) {
  45. return false;
  46. }
  47. var wobj = plus.webview.currentWebview();
  48. var parent = wobj.parent();
  49. if (parent) {
  50. parent.evalJS('mui&&mui.back();');
  51. } else {
  52. wobj.canBack(function(e) {
  53. //by chb 暂时注释,在碰到类似popover之类的锚点的时候,需多次点击才能返回;
  54. if (e.canBack) { //webview history back
  55. window.history.back();
  56. } else { //webview close or hide
  57. //fixed by fxy 此处不应该用opener判断,因为用户有可能自己close掉当前窗口的opener。这样的话。opener就为空了,导致不能执行close
  58. if (wobj.id === plus.runtime.appid) { //首页
  59. //首页不存在opener的情况下,后退实际上应该是退出应用;
  60. //首次按键,提示‘再按一次退出应用’
  61. if (!$.__back__first) {
  62. $.__back__first = new Date().getTime();
  63. mui.toast('再按一次退出应用');
  64. setTimeout(function() {
  65. $.__back__first = null;
  66. }, 2000);
  67. } else {
  68. if (new Date().getTime() - $.__back__first < 2000) {
  69. plus.runtime.quit();
  70. }
  71. }
  72. } else { //其他页面,
  73. if (wobj.preload) {
  74. wobj.hide("auto");
  75. } else {
  76. //关闭页面时,需要将其打开的所有子页面全部关闭;
  77. $.closeAll(wobj);
  78. }
  79. }
  80. }
  81. });
  82. }
  83. return true;
  84. }
  85. });
  86. $.menu = function() {
  87. var menu = document.querySelector($.classSelector('.action-menu'));
  88. if (menu) {
  89. $.trigger(menu, $.EVENT_START); //临时处理menu无touchstart的话,找不到当前targets的问题
  90. $.trigger(menu, 'tap');
  91. } else { //执行父窗口的menu
  92. if (window.plus) {
  93. var wobj = $.currentWebview;
  94. var parent = wobj.parent();
  95. if (parent) { //又得evalJS
  96. parent.evalJS('mui&&mui.menu();');
  97. }
  98. }
  99. }
  100. };
  101. var __back = function() {
  102. $.back();
  103. };
  104. var __menu = function() {
  105. $.menu();
  106. };
  107. //默认监听
  108. $.plusReady(function() {
  109. if ($.options.keyEventBind.backbutton) {
  110. plus.key.addEventListener('backbutton', __back, false);
  111. }
  112. if ($.options.keyEventBind.menubutton) {
  113. plus.key.addEventListener('menubutton', __menu, false);
  114. }
  115. });
  116. //处理按键监听事件
  117. $.addInit({
  118. name: 'keyEventBind',
  119. index: 1000,
  120. handle: function() {
  121. $.plusReady(function() {
  122. //如果不为true,则移除默认监听
  123. if (!$.options.keyEventBind.backbutton) {
  124. plus.key.removeEventListener('backbutton', __back);
  125. }
  126. if (!$.options.keyEventBind.menubutton) {
  127. plus.key.removeEventListener('menubutton', __menu);
  128. }
  129. });
  130. }
  131. });
  132. })(mui, window);