-
Notifications
You must be signed in to change notification settings - Fork 6
/
focusin.js
37 lines (32 loc) · 1.1 KB
/
focusin.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
/* from https://gist.github.com/nuxodin/9250e56a3ce6c0446efa */
function polyfill () {
var w = window
var d = w.document
if (w.onfocusin === undefined) {
d.addEventListener('focus', addPolyfill, true)
d.addEventListener('blur', addPolyfill, true)
d.addEventListener('focusin', removePolyfill, true)
d.addEventListener('focusout', removePolyfill, true)
}
function addPolyfill (e) {
var type = e.type === 'focus' ? 'focusin' : 'focusout'
var event = new window.CustomEvent(type, { bubbles: true, cancelable: false })
event.c1Generated = true
e.target.dispatchEvent(event)
}
function removePolyfill (e) {
if (!e.c1Generated) {
d.removeEventListener('focus', addPolyfill, true)
d.removeEventListener('blur', addPolyfill, true)
d.removeEventListener('focusin', removePolyfill, true)
d.removeEventListener('focusout', removePolyfill, true)
}
setTimeout(function () {
d.removeEventListener('focusin', removePolyfill, true)
d.removeEventListener('focusout', removePolyfill, true)
})
}
}
module.exports = {
polyfill: polyfill
}