forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathescape-key.js
More file actions
41 lines (36 loc) · 958 Bytes
/
escape-key.js
File metadata and controls
41 lines (36 loc) · 958 Bytes
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
import { isKeyCode } from './key-composition.js'
const handlers = []
let escDown = false
export default {
__install () {
this.__installed = true
window.addEventListener('keydown', evt => {
escDown = evt.keyCode === 27
})
window.addEventListener('blur', () => {
escDown === true && (escDown = false)
})
window.addEventListener('keyup', evt => {
if (escDown === true) {
escDown = false
if (handlers.length !== 0 && isKeyCode(evt, 27) === true) {
handlers[handlers.length - 1].fn(evt)
}
}
})
},
register (comp, fn) {
if (comp.$q.platform.is.desktop === true) {
this.__installed !== true && this.__install()
handlers.push({ comp, fn })
}
},
pop (comp) {
if (comp.$q.platform.is.desktop === true) {
const index = handlers.findIndex(h => h.comp === comp)
if (index > -1) {
handlers.splice(index, 1)
}
}
}
}