hardware-back-button-a7eb8233.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { w as win } from './index-a5d50daf.js';
  5. import { c as config, d as printIonError } from './index-cfd9c1f2.js';
  6. /**
  7. * CloseWatcher is a newer API that lets
  8. * use detect the hardware back button event
  9. * in a web browser: https://caniuse.com/?search=closewatcher
  10. * However, not every browser supports it yet.
  11. *
  12. * This needs to be a function so that we can
  13. * check the config once it has been set.
  14. * Otherwise, this code would be evaluated the
  15. * moment this file is evaluated which could be
  16. * before the config is set.
  17. */
  18. const shouldUseCloseWatcher = () => config.get('experimentalCloseWatcher', false) && win !== undefined && 'CloseWatcher' in win;
  19. /**
  20. * When hardwareBackButton: false in config,
  21. * we need to make sure we also block the default
  22. * webview behavior. If we don't then it will be
  23. * possible for users to navigate backward while
  24. * an overlay is still open. Additionally, it will
  25. * give the appearance that the hardwareBackButton
  26. * config is not working as the page transition
  27. * will still happen.
  28. */
  29. const blockHardwareBackButton = () => {
  30. document.addEventListener('backbutton', () => { }); // eslint-disable-line
  31. };
  32. const startHardwareBackButton = () => {
  33. const doc = document;
  34. let busy = false;
  35. const backButtonCallback = () => {
  36. if (busy) {
  37. return;
  38. }
  39. let index = 0;
  40. let handlers = [];
  41. const ev = new CustomEvent('ionBackButton', {
  42. bubbles: false,
  43. detail: {
  44. register(priority, handler) {
  45. handlers.push({ priority, handler, id: index++ });
  46. },
  47. },
  48. });
  49. doc.dispatchEvent(ev);
  50. const executeAction = async (handlerRegister) => {
  51. try {
  52. if (handlerRegister === null || handlerRegister === void 0 ? void 0 : handlerRegister.handler) {
  53. const result = handlerRegister.handler(processHandlers);
  54. if (result != null) {
  55. await result;
  56. }
  57. }
  58. }
  59. catch (e) {
  60. printIonError('[ion-app] - Exception in startHardwareBackButton:', e);
  61. }
  62. };
  63. const processHandlers = () => {
  64. if (handlers.length > 0) {
  65. let selectedHandler = {
  66. priority: Number.MIN_SAFE_INTEGER,
  67. handler: () => undefined,
  68. id: -1,
  69. };
  70. handlers.forEach((handler) => {
  71. if (handler.priority >= selectedHandler.priority) {
  72. selectedHandler = handler;
  73. }
  74. });
  75. busy = true;
  76. handlers = handlers.filter((handler) => handler.id !== selectedHandler.id);
  77. executeAction(selectedHandler).then(() => (busy = false));
  78. }
  79. };
  80. processHandlers();
  81. };
  82. /**
  83. * If the CloseWatcher is defined then
  84. * we don't want to also listen for the native
  85. * backbutton event otherwise we may get duplicate
  86. * events firing.
  87. */
  88. if (shouldUseCloseWatcher()) {
  89. let watcher;
  90. const configureWatcher = () => {
  91. watcher === null || watcher === void 0 ? void 0 : watcher.destroy();
  92. watcher = new win.CloseWatcher();
  93. /**
  94. * Once a close request happens
  95. * the watcher gets destroyed.
  96. * As a result, we need to re-configure
  97. * the watcher so we can respond to other
  98. * close requests.
  99. */
  100. watcher.onclose = () => {
  101. backButtonCallback();
  102. configureWatcher();
  103. };
  104. };
  105. configureWatcher();
  106. }
  107. else {
  108. doc.addEventListener('backbutton', backButtonCallback);
  109. }
  110. };
  111. const OVERLAY_BACK_BUTTON_PRIORITY = 100;
  112. const MENU_BACK_BUTTON_PRIORITY = 99; // 1 less than overlay priority since menu is displayed behind overlays
  113. export { MENU_BACK_BUTTON_PRIORITY, OVERLAY_BACK_BUTTON_PRIORITY, blockHardwareBackButton, shouldUseCloseWatcher, startHardwareBackButton };