-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathon.js
47 lines (40 loc) · 1.07 KB
/
on.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
define(function() {
return function on(type, listener, capture) {
var name = "__on" + type
, i = type.indexOf(".")
if (i > 0) type = type.slice(0, i)
function onRemove() {
var l = this[name]
if (l) {
this.removeEventListener(type, l, l.$)
delete this[name]
}
}
function onAdd() {
var l = onListener(listener)
onRemove.call(this)
this.addEventListener(type, this[name] = l, l.$ = !!capture)
l._ = listener
}
function removeAll() {
var re = new RegExp("^__on([^.]+)" + type.replace(/[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g, "\\$&") + "$")
, match
for (var name in this) {
if (match = name.match(re)) {
var l = this[name]
this.removeEventListener(match[1], l, l.$)
delete this[name]
}
}
}
return (i
? listener ? onAdd : onRemove
: listener ? noop : removeAll).call(this)
}
function onListener(listener) {
return function() {
return listener.apply(this, arguments)
}
}
function noop() {}
})