-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaya-event.js
74 lines (74 loc) · 1.5 KB
/
yaya-event.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
65
66
67
68
69
70
71
72
73
74
/*yaya event 1.1.0 under MIT License 2011.3.25*/
var YayaEvent = window.YayaEvent = (function(){
var events = {};
var eventlist={};
var tag = "action";
function addEvt(type,listener){
type = type.toLowerCase().replace(/^on/,"");
if (document.addEventListener) {
document.addEventListener(type,listener, false);
} else if (document.attachEvent) {
document.attachEvent('on' + type,listener);
}
}
function bindevt(evt){
addEvt(evt,function(e){
e=e||window.event;
var target = e.target||e.srcElement;
var bubble = true;
var first = true;
while (target)
{
if (!bubble){
return;
}
var action = target.getAttribute&&target.getAttribute(tag);
if (action&&eventlist[action]&&eventlist[action][evt])
{
bubble = eventlist[action][evt].call(target,e);
}
else{
if (first){
return;
}
}
target = target.parentNode;
first = false;
}
});
}
return {
unbind:function(map){
for (var i in map)
{
map[i] = typeof map[i]=="object"?map[i]:[map[i]];
for (var j=0;j<map[i].length;j++)
{
if (eventlist[i]&&eventlist[i][map[i][j]])
{
delete eventlist[i][map[i][j]];
}
}
}
},
bind:function(map,_tag){
tag=_tag||tag;
for (var i in map)
{
if (!eventlist[i])
{
eventlist[i]={};
}
for (var evt in map[i])
{
if (!events[evt])
{
events[evt] = true;
bindevt(evt);
}
eventlist[i][evt]=map[i][evt];
}
}
}
};
})();