root / trunk / web / dojo / dojox / lang / aspect.js @ 12
History | View | Annotate | Download (4.12 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.lang.aspect"]){ |
9 |
dojo._hasResource["dojox.lang.aspect"]=true; |
10 |
dojo.provide("dojox.lang.aspect");
|
11 |
(function(){
|
12 |
var d=dojo,_1=dojox.lang.aspect,ap=Array.prototype,_2=[],_3;
|
13 |
var _4=function(){ |
14 |
this.next_before=this.prev_before=this.next_around=this.prev_around=this.next_afterReturning=this.prev_afterReturning=this.next_afterThrowing=this.prev_afterThrowing=this; |
15 |
this.counter=0; |
16 |
}; |
17 |
d.extend(_4,{add:function(_5){ |
18 |
var _6=d.isFunction(_5),_7={advice:_5,dynamic:_6}; |
19 |
this._add(_7,"before","",_6,_5); |
20 |
this._add(_7,"around","",_6,_5); |
21 |
this._add(_7,"after","Returning",_6,_5); |
22 |
this._add(_7,"after","Throwing",_6,_5); |
23 |
++this.counter;
|
24 |
return _7;
|
25 |
},_add:function(_8,_9,_a,_b,_c){ |
26 |
var _d=_9+_a;
|
27 |
if(_b||_c[_9]||(_a&&_c[_d])){
|
28 |
var _e="next_"+_d,_f="prev_"+_d; |
29 |
(_8[_f]=this[_f])[_e]=_8;
|
30 |
(_8[_e]=this)[_f]=_8;
|
31 |
} |
32 |
},remove:function(_10){ |
33 |
this._remove(_10,"before"); |
34 |
this._remove(_10,"around"); |
35 |
this._remove(_10,"afterReturning"); |
36 |
this._remove(_10,"afterThrowing"); |
37 |
--this.counter;
|
38 |
},_remove:function(_11,_12){ |
39 |
var _13="next_"+_12,_14="prev_"+_12; |
40 |
if(_11[_13]){
|
41 |
_11[_13][_14]=_11[_14]; |
42 |
_11[_14][_13]=_11[_13]; |
43 |
} |
44 |
},isEmpty:function(){ |
45 |
return !this.counter; |
46 |
}}); |
47 |
var _15=function(){ |
48 |
return function(){ |
49 |
var _16=arguments.callee,_17=_16.advices,ret,i,a,e,t; |
50 |
if(_3){
|
51 |
_2.push(_3); |
52 |
} |
53 |
_3={instance:this,joinPoint:_16,depth:_2.length,around:_17.prev_around,dynAdvices:[],dynIndex:0}; |
54 |
try{
|
55 |
for(i=_17.prev_before;i!=_17;i=i.prev_before){
|
56 |
if(i.dynamic){
|
57 |
_3.dynAdvices.push(a=new i.advice(_3));
|
58 |
if(t=a.before){
|
59 |
t.apply(a,arguments);
|
60 |
} |
61 |
}else{
|
62 |
t=i.advice; |
63 |
t.before.apply(t,arguments);
|
64 |
} |
65 |
} |
66 |
try{
|
67 |
ret=(_17.prev_around==_17?_16.target:_1.proceed).apply(this,arguments); |
68 |
} |
69 |
catch(e){
|
70 |
_3.dynIndex=_3.dynAdvices.length; |
71 |
for(i=_17.next_afterThrowing;i!=_17;i=i.next_afterThrowing){
|
72 |
a=i.dynamic?_3.dynAdvices[--_3.dynIndex]:i.advice; |
73 |
if(t=a.afterThrowing){
|
74 |
t.call(a,e); |
75 |
} |
76 |
if(t=a.after){
|
77 |
t.call(a); |
78 |
} |
79 |
} |
80 |
throw e;
|
81 |
} |
82 |
_3.dynIndex=_3.dynAdvices.length; |
83 |
for(i=_17.next_afterReturning;i!=_17;i=i.next_afterReturning){
|
84 |
a=i.dynamic?_3.dynAdvices[--_3.dynIndex]:i.advice; |
85 |
if(t=a.afterReturning){
|
86 |
t.call(a,ret); |
87 |
} |
88 |
if(t=a.after){
|
89 |
t.call(a); |
90 |
} |
91 |
} |
92 |
var ls=_16._listeners;
|
93 |
for(i in ls){ |
94 |
if(!(i in ap)){ |
95 |
ls[i].apply(this,arguments); |
96 |
} |
97 |
} |
98 |
} |
99 |
finally{
|
100 |
for(i=0;i<_3.dynAdvices.length;++i){ |
101 |
a=_3.dynAdvices[i]; |
102 |
if(a.destroy){
|
103 |
a.destroy(); |
104 |
} |
105 |
} |
106 |
_3=_2.length?_2.pop():null;
|
107 |
} |
108 |
return ret;
|
109 |
}; |
110 |
}; |
111 |
_1.advise=function(obj,_18,_19){ |
112 |
if(typeof obj!="object"){ |
113 |
obj=obj.prototype; |
114 |
} |
115 |
var _1a=[];
|
116 |
if(!(_18 instanceof Array)){ |
117 |
_18=[_18]; |
118 |
} |
119 |
for(var j=0;j<_18.length;++j){ |
120 |
var t=_18[j];
|
121 |
if(t instanceof RegExp){ |
122 |
for(var i in obj){ |
123 |
if(d.isFunction(obj[i])&&t.test(i)){
|
124 |
_1a.push(i); |
125 |
} |
126 |
} |
127 |
}else{
|
128 |
if(d.isFunction(obj[t])){
|
129 |
_1a.push(t); |
130 |
} |
131 |
} |
132 |
} |
133 |
if(!d.isArray(_19)){
|
134 |
_19=[_19]; |
135 |
} |
136 |
return _1.adviseRaw(obj,_1a,_19);
|
137 |
}; |
138 |
_1.adviseRaw=function(obj,_1b,_1c){ |
139 |
if(!_1b.length||!_1c.length){
|
140 |
return null; |
141 |
} |
142 |
var m={},al=_1c.length;
|
143 |
for(var i=_1b.length-1;i>=0;--i){ |
144 |
var _1d=_1b[i],o=obj[_1d],ao=new Array(al),t=o.advices; |
145 |
if(!t){
|
146 |
var x=obj[_1d]=_15();
|
147 |
x.target=o.target||o; |
148 |
x.targetName=_1d; |
149 |
x._listeners=o._listeners||[]; |
150 |
x.advices=new _4;
|
151 |
t=x.advices; |
152 |
} |
153 |
for(var j=0;j<al;++j){ |
154 |
ao[j]=t.add(_1c[j]); |
155 |
} |
156 |
m[_1d]=ao; |
157 |
} |
158 |
return [obj,m];
|
159 |
}; |
160 |
_1.unadvise=function(_1e){ |
161 |
if(!_1e){
|
162 |
return;
|
163 |
} |
164 |
var obj=_1e[0],_1f=_1e[1]; |
165 |
for(var _20 in _1f){ |
166 |
var o=obj[_20],t=o.advices,ao=_1f[_20];
|
167 |
for(var i=ao.length-1;i>=0;--i){ |
168 |
t.remove(ao[i]); |
169 |
} |
170 |
if(t.isEmpty()){
|
171 |
var _21=true,ls=o._listeners; |
172 |
if(ls.length){
|
173 |
for(i in ls){ |
174 |
if(!(i in ap)){ |
175 |
_21=false;
|
176 |
break;
|
177 |
} |
178 |
} |
179 |
} |
180 |
if(_21){
|
181 |
obj[_20]=o.target; |
182 |
}else{
|
183 |
var x=obj[_20]=d._listener.getDispatcher();
|
184 |
x.target=o.target; |
185 |
x._listeners=ls; |
186 |
} |
187 |
} |
188 |
} |
189 |
}; |
190 |
_1.getContext=function(){ |
191 |
return _3;
|
192 |
}; |
193 |
_1.getContextStack=function(){ |
194 |
return _2;
|
195 |
}; |
196 |
_1.proceed=function(){ |
197 |
var _22=_3.joinPoint,_23=_22.advices;
|
198 |
for(var c=_3.around;c!=_23;c=_3.around){ |
199 |
_3.around=c.prev_around; |
200 |
if(c.dynamic){
|
201 |
var a=_3.dynAdvices[_3.dynIndex++],t=a.around;
|
202 |
if(t){
|
203 |
return t.apply(a,arguments); |
204 |
} |
205 |
}else{
|
206 |
return c.advice.around.apply(c.advice,arguments); |
207 |
} |
208 |
} |
209 |
return _22.target.apply(_3.instance,arguments); |
210 |
}; |
211 |
})(); |
212 |
} |