forked from bootstrap-vue/bootstrap-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten-on-document.js
64 lines (57 loc) · 1.8 KB
/
listen-on-document.js
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
import { extend } from '../vue'
import { IS_BROWSER } from '../constants/env'
import { EVENT_OPTIONS_NO_CAPTURE } from '../constants/events'
import { arrayIncludes } from '../utils/array'
import { eventOn, eventOff } from '../utils/events'
import { keys } from '../utils/object'
// --- Constants ---
const PROP = '$_documentListeners'
// --- Mixin ---
// @vue/component
export const listenOnDocumentMixin = extend({
created() {
// Define non-reactive property
// Object of arrays, keyed by event name,
// where value is an array of callbacks
this[PROP] = {}
},
beforeDestroy() {
// Unregister all registered listeners
keys(this[PROP] || {}).forEach(event => {
this[PROP][event].forEach(callback => {
this.listenOffDocument(event, callback)
})
})
this[PROP] = null
},
methods: {
registerDocumentListener(event, callback) {
if (this[PROP]) {
this[PROP][event] = this[PROP][event] || []
if (!arrayIncludes(this[PROP][event], callback)) {
this[PROP][event].push(callback)
}
}
},
unregisterDocumentListener(event, callback) {
if (this[PROP] && this[PROP][event]) {
this[PROP][event] = this[PROP][event].filter(cb => cb !== callback)
}
},
listenDocument(on, event, callback) {
on ? this.listenOnDocument(event, callback) : this.listenOffDocument(event, callback)
},
listenOnDocument(event, callback) {
if (IS_BROWSER) {
eventOn(document, event, callback, EVENT_OPTIONS_NO_CAPTURE)
this.registerDocumentListener(event, callback)
}
},
listenOffDocument(event, callback) {
if (IS_BROWSER) {
eventOff(document, event, callback, EVENT_OPTIONS_NO_CAPTURE)
}
this.unregisterDocumentListener(event, callback)
}
}
})