hardware-back-button-3d2b1004.js 4.3 KB

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