forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoBack.js
More file actions
70 lines (57 loc) · 1.41 KB
/
GoBack.js
File metadata and controls
70 lines (57 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { client } from '../plugins/Platform.js'
import { isKeyCode } from '../utils/key-composition.js'
function destroy (el) {
const ctx = el.__qgoback
if (ctx !== void 0) {
el.removeEventListener('click', ctx.goBack)
el.removeEventListener('keyup', ctx.goBackKey)
delete el.__qgoback
}
}
export default {
name: 'go-back',
bind (el, { value, modifiers }, vnode) {
if (el.__qgoback !== void 0) {
destroy(el)
el.__qgoback_destroyed = true
}
const ctx = {
value,
position: window.history.length - 1,
single: modifiers.single,
goBack () {
const router = vnode.context.$router
if (ctx.single === true) {
router.go(-1)
}
else if (client.is.nativeMobile === true) {
router.go(ctx.position - window.history.length)
}
else {
router.replace(ctx.value)
}
},
goBackKey (e) {
// if ENTER key
isKeyCode(e, 13) === true && ctx.goBack()
}
}
el.__qgoback = ctx
el.addEventListener('click', ctx.goBack)
el.addEventListener('keyup', ctx.goBackKey)
},
update (el, { value, oldValue }) {
const ctx = el.__qgoback
if (ctx !== void 0 && value !== oldValue) {
ctx.value = value
}
},
unbind (el) {
if (el.__qgoback_destroyed === void 0) {
destroy(el)
}
else {
delete el.__qgoback_destroyed
}
}
}