UNPKG

2.29 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * When hardwareBackButton: false in config,
5 * we need to make sure we also block the default
6 * webview behavior. If we don't then it will be
7 * possible for users to navigate backward while
8 * an overlay is still open. Additionally, it will
9 * give the appearance that the hardwareBackButton
10 * config is not working as the page transition
11 * will still happen.
12 */
13const blockHardwareBackButton = () => {
14 document.addEventListener('backbutton', () => { }); // tslint:disable-line
15};
16const startHardwareBackButton = () => {
17 const doc = document;
18 let busy = false;
19 doc.addEventListener('backbutton', () => {
20 if (busy) {
21 return;
22 }
23 let index = 0;
24 let handlers = [];
25 const ev = new CustomEvent('ionBackButton', {
26 bubbles: false,
27 detail: {
28 register(priority, handler) {
29 handlers.push({ priority, handler, id: index++ });
30 }
31 }
32 });
33 doc.dispatchEvent(ev);
34 const executeAction = async (handlerRegister) => {
35 try {
36 if (handlerRegister && handlerRegister.handler) {
37 const result = handlerRegister.handler(processHandlers);
38 if (result != null) {
39 await result;
40 }
41 }
42 }
43 catch (e) {
44 console.error(e);
45 }
46 };
47 const processHandlers = () => {
48 if (handlers.length > 0) {
49 let selectedHandler = {
50 priority: Number.MIN_SAFE_INTEGER,
51 handler: () => undefined,
52 id: -1
53 };
54 handlers.forEach(handler => {
55 if (handler.priority >= selectedHandler.priority) {
56 selectedHandler = handler;
57 }
58 });
59 busy = true;
60 handlers = handlers.filter(handler => handler.id !== selectedHandler.id);
61 executeAction(selectedHandler).then(() => busy = false);
62 }
63 };
64 processHandlers();
65 });
66};
67const OVERLAY_BACK_BUTTON_PRIORITY = 100;
68const MENU_BACK_BUTTON_PRIORITY = 99; // 1 less than overlay priority since menu is displayed behind overlays
69
70exports.MENU_BACK_BUTTON_PRIORITY = MENU_BACK_BUTTON_PRIORITY;
71exports.OVERLAY_BACK_BUTTON_PRIORITY = OVERLAY_BACK_BUTTON_PRIORITY;
72exports.blockHardwareBackButton = blockHardwareBackButton;
73exports.startHardwareBackButton = startHardwareBackButton;