1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const blockHardwareBackButton = () => {
|
15 | document.addEventListener('backbutton', () => { });
|
16 | };
|
17 | const 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 | };
|
68 | const OVERLAY_BACK_BUTTON_PRIORITY = 100;
|
69 | const MENU_BACK_BUTTON_PRIORITY = 99;
|
70 |
|
71 | export { MENU_BACK_BUTTON_PRIORITY, OVERLAY_BACK_BUTTON_PRIORITY, blockHardwareBackButton, startHardwareBackButton };
|