在上一版的基础上进行了如下改进:
1、增加了事件参数传递的支持;
2、增加了事件、拦截器的有效范围定义的支持;
3、增加了事件、拦截器的清除的支持。
源码如下:
1
/**/
/*
2
* ============================================================
3
* ------------------------------------------------------------
4
* Author: Bluedavy (Bluedavy.blogjava.net)
5
* ------------------------------------------------------------
6
* 事件管理器
7
* 1、事件的注册/反注册
8
* 2、事件的调用
9
* 3、事件的拦截器(事件执行前和事件执行后)
10
* 4、拦截器的有效范围
11
* 5、事件执行的有效范围
12
* TODO:COR Pattern
13
* ============================================================
14
*/
15
var
EventManager
=
Class.create();
16
17
//
事件集合
18
EventManager.events
=
{}
;
19
20
//
事件拦截器集合
21
EventManager.interceptors
=
{}
;
22
23
//
常量定义
24
EventManager.Constant
=
{
25
SCOPE:
"
__ALL__
"
26
}
27
28
//
注册事件
29
EventManager.registerEvent
=
function
(eventalias,event,scope)
{
30
if
(
typeof
(scope)
==
'undefined')
{
31
scope
=
EventManager.Constant.SCOPE;
32
}
33
if
(EventManager.events[scope]
==
null
)
{
34
EventManager.events[scope]
=
{}
;
35
}
36
EventManager.events[scope][eventalias]
=
event;
37
}
38
39
//
反注册事件
40
EventManager.unregisterEvent
=
function
(eventalias,scope)
{
41
if
(
typeof
(scope)
==
'undefined')
{
42
scope
=
EventManager.Constant.SCOPE;
43
}
44
EventManager.events[scope][eventalias]
=
null
;
45
}
46
47
//
拦截器常量定义
48
EventManager.Interceptor
=
{
49
BEFORE:
"
before
"
,
50
AFTER:
"
after
"
51
}
52
53
//
清除某范围内的事件以及拦截器
54
EventManager.clear
=
function
(scope)
{
55
if
(
typeof
(scope)
==
'undefined')
{
56
EventManager.events
=
{}
;
57
EventManager.interceptors
=
{}
;
58
}
59
else
{
60
EventManager.events[scope]
=
null
;
61
EventManager.interceptors[scope]
=
null
;
62
}
63
}
64
65
//
事件直接调用
66
EventManager.directyinvoke
=
function
(eventalias,params,scope)
{
67
if
(
typeof
(scope)
==
'undefined')
{
68
scope
=
EventManager.Constant.SCOPE;
69
}
70
if
(EventManager.events[scope]
==
null
)
{
71
return
;
72
}
73
var
bindevent
=
EventManager.events[scope][eventalias];
74
if
(bindevent
!=
null
)
{
75
try
{
76
EventManager.invokeInterceptor(eventalias,EventManager.Interceptor.BEFORE,params,scope);
77
if
(
typeof
(params)
==
'undefined')
{
78
bindevent.apply(
this
,[event]);
79
}
80
else
{
81
params.push(event);
82
bindevent.apply(
this
,params);
83
}
84
EventManager.invokeInterceptor(eventalias,EventManager.Interceptor.AFTER,params,scope);
85
}
86
catch
(e)
{
87
//
不做任何提示
88
alert(e);
89
}
90
}
91
}
92
93
//
事件的调用(用于绑定到元素的事件上)
94
EventManager.invoke
=
function
(eventalias,params,scope)
{
95
return
function
()
{
96
EventManager.directyinvoke(eventalias,params,scope);
97
}
98
}
99
100
//
执行拦截器
101
EventManager.invokeInterceptor
=
function
(eventalias,type,params,scope)
{
102
if
(EventManager.interceptors[scope]
==
null
)
{
103
return
;
104
}
105
var
interceptors
=
EventManager.interceptors[scope][eventalias];
106
if
(
typeof
(interceptors)
==
'undefined')
{
107
return
;
108
}
109
var
typeInterceptors
=
interceptors[type];
110
if
(typeInterceptors
==
null
)
{
111
return
;
112
}
113
for
(
var
i
=
0
;i
<
typeInterceptors.length;i
++
)
{
114
try
{
115
if
(
typeof
(params)
==
'undefined')
{
116
typeInterceptors[i].apply(
this
,[event]);
117
}
118
else
{
119
params.push(event);
120
typeInterceptors[i].apply(
this
,params);
121
}
122
}
123
catch
(e)
{
124
alert(e);
125
}
126
}
127
}
128
129
//
注册方法执行前的拦截器
130
EventManager.addBeforeInterceptor
=
function
(eventalias,interceptor,scope)
{
131
EventManager.addInterceptor(eventalias,interceptor,EventManager.Interceptor.BEFORE,scope);
132
}
133
134
//
删除方法执行前的拦截器
135
EventManager.removeBeforeInterceptor
=
function
(eventalias,interceptor,scope)
{
136
EventManager.removeInterceptor(eventalias,interceptor,EventManager.Interceptor.BEFORE,scope);
137
}
138
139
//
注册方法执行后的拦截器
140
EventManager.addAfterInterceptor
=
function
(eventalias,interceptor,scope)
{
141
EventManager.addInterceptor(eventalias,interceptor,EventManager.Interceptor.AFTER,scope);
142
}
143
144
//
删除方法执行后的拦截器
145
EventManager.removeAfterInterceptor
=
function
(eventalias,interceptor,scope)
{
146
EventManager.removeInterceptor(eventalias,interceptor,EventManager.Interceptor.AFTER,scope);
147
}
148
149
//
添加拦截器
150
EventManager.addInterceptor
=
function
(eventalias,interceptor,type,scope)
{
151
if
(
typeof
(scope)
==
'undefined')
{
152
scope
=
EventManager.Constant.SCOPE;
153
}
154
if
(EventManager.interceptors[scope]
==
null
)
{
155
EventManager.interceptors[scope]
=
{}
;
156
}
157
var
interceptors
=
EventManager.interceptors[scope][eventalias];
158
if
(interceptors
==
null
)
{
159
interceptors
=
{}
;
160
}
161
if
(interceptors[type]
==
null
)
{
162
interceptors[type]
=
new
Array();
163
}
164
interceptors[type].push(interceptor);
165
EventManager.interceptors[scope][eventalias]
=
interceptors;
166
}
167
168
//
删除拦截器
169
EventManager.removeInterceptor
=
function
(eventalias,interceptor,type,scope)
{
170
if
(
typeof
(scope)
==
'undefined')
{
171
scope
=
EventManager.Constant.SCOPE;
172
}
173
if
(EventManager.interceptors[scope]
==
null
)
{
174
return
;
175
}
176
var
interceptors
=
EventManager.events[scope][eventalias];
177
if
(interceptors
==
null
)
{
178
return
;
179
}
180
if
(interceptors[type]
==
null
)
{
181
return
;
182
}
183
interceptors[type].pop(interceptor);
184
EventManager.interceptors[scope][eventalias]
=
interceptors;
185
}