1 | import { isSSR, client } from './plugins/Platform.js'
|
2 | import { noop } from './utils/event.js'
|
3 |
|
4 | const getTrue = () => true
|
5 |
|
6 | function filterInvalidPath (path) {
|
7 | return typeof path === 'string' &&
|
8 | path !== '' &&
|
9 | path !== '/' &&
|
10 | path !== '#/'
|
11 | }
|
12 |
|
13 | function normalizeExitPath (path) {
|
14 | path.startsWith('#') === true && (path = path.substr(1))
|
15 | path.startsWith('/') === false && (path = '/' + path)
|
16 | path.endsWith('/') === true && (path = path.substr(0, path.length - 1))
|
17 | return '#' + path
|
18 | }
|
19 |
|
20 | function getShouldExitFn (cfg) {
|
21 | if (cfg.backButtonExit === false) {
|
22 | return () => false
|
23 | }
|
24 |
|
25 | if (cfg.backButtonExit === '*') {
|
26 | return getTrue
|
27 | }
|
28 |
|
29 |
|
30 | const exitPaths = [ '#/' ]
|
31 |
|
32 |
|
33 | Array.isArray(cfg.backButtonExit) === true && exitPaths.push(
|
34 | ...cfg.backButtonExit.filter(filterInvalidPath).map(normalizeExitPath)
|
35 | )
|
36 |
|
37 | return () => exitPaths.includes(window.location.hash)
|
38 | }
|
39 |
|
40 | export default {
|
41 | __history: [],
|
42 | add: noop,
|
43 | remove: noop,
|
44 |
|
45 | install (cfg) {
|
46 | if (isSSR === true) {
|
47 | return
|
48 | }
|
49 |
|
50 | const { cordova, capacitor } = client.is
|
51 |
|
52 | if (cordova !== true && capacitor !== true) {
|
53 | return
|
54 | }
|
55 |
|
56 | this.add = entry => {
|
57 | if (entry.condition === void 0) {
|
58 | entry.condition = getTrue
|
59 | }
|
60 | this.__history.push(entry)
|
61 | }
|
62 |
|
63 | this.remove = entry => {
|
64 | const index = this.__history.indexOf(entry)
|
65 | if (index >= 0) {
|
66 | this.__history.splice(index, 1)
|
67 | }
|
68 | }
|
69 |
|
70 | const shouldExit = getShouldExitFn(
|
71 | Object.assign(
|
72 | { backButtonExit: true },
|
73 | cfg[cordova === true ? 'cordova' : 'capacitor']
|
74 | )
|
75 | )
|
76 |
|
77 | const backHandler = () => {
|
78 | if (this.__history.length) {
|
79 | const entry = this.__history[this.__history.length - 1]
|
80 |
|
81 | if (entry.condition() === true) {
|
82 | this.__history.pop()
|
83 | entry.handler()
|
84 | }
|
85 | }
|
86 | else if (shouldExit() === true) {
|
87 | navigator.app.exitApp()
|
88 | }
|
89 | else {
|
90 | window.history.back()
|
91 | }
|
92 | }
|
93 |
|
94 | if (cordova === true) {
|
95 | document.addEventListener('deviceready', () => {
|
96 | document.addEventListener('backbutton', backHandler, false)
|
97 | })
|
98 | }
|
99 | else {
|
100 | window.Capacitor.Plugins.App.addListener('backButton', backHandler)
|
101 | }
|
102 | }
|
103 | }
|