UNPKG

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