-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (34 loc) · 1.04 KB
/
index.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
var deligate = require('deligate')
// sourced from http://www.w3.org/TR/DOM-Level-3-Events/#event-types-list
var non_bubble_phase_events = [
'abort',
'blur',
'error',
'focus',
'load',
'mouseenter',
'mouseleave',
'resize',
'scroll',
'unload'
]
function addEvents(el, events, context) {
var eventDescriptions = Object.keys(events).map(function(key){
var eventName = key.split(' ')[0]
var handler = deligate(key.split(' ')[1], events[key])
if (context) handler = handler.bind(context)
var capture = non_bubble_phase_events.indexOf(eventName) != -1
el.addEventListener(eventName, handler, capture)
return {
eventName: eventName,
handler: handler
}
})
return function removeEvents(){
eventDescriptions.forEach(function(desc){
var capture = non_bubble_phase_events.indexOf(desc.eventName) != -1
el.removeEventListener(desc.eventName, desc.handler, capture)
})
}
}
module.exports = addEvents