Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.event.common");
12
dojo.require("dojo.lang.array");
13
dojo.require("dojo.lang.extras");
14
dojo.require("dojo.lang.func");
15
dojo.event = new function () {
16
	this._canTimeout = dojo.lang.isFunction(dj_global["setTimeout"]) || dojo.lang.isAlien(dj_global["setTimeout"]);
17
	function interpolateArgs(args, searchForNames) {
18
		var dl = dojo.lang;
19
		var ao = {srcObj:dj_global, srcFunc:null, adviceObj:dj_global, adviceFunc:null, aroundObj:null, aroundFunc:null, adviceType:(args.length > 2) ? args[0] : "after", precedence:"last", once:false, delay:null, rate:0, adviceMsg:false, maxCalls:-1};
20
		switch (args.length) {
21
		  case 0:
22
			return;
23
		  case 1:
24
			return;
25
		  case 2:
26
			ao.srcFunc = args[0];
27
			ao.adviceFunc = args[1];
28
			break;
29
		  case 3:
30
			if ((dl.isObject(args[0])) && (dl.isString(args[1])) && (dl.isString(args[2]))) {
31
				ao.adviceType = "after";
32
				ao.srcObj = args[0];
33
				ao.srcFunc = args[1];
34
				ao.adviceFunc = args[2];
35
			} else {
36
				if ((dl.isString(args[1])) && (dl.isString(args[2]))) {
37
					ao.srcFunc = args[1];
38
					ao.adviceFunc = args[2];
39
				} else {
40
					if ((dl.isObject(args[0])) && (dl.isString(args[1])) && (dl.isFunction(args[2]))) {
41
						ao.adviceType = "after";
42
						ao.srcObj = args[0];
43
						ao.srcFunc = args[1];
44
						var tmpName = dl.nameAnonFunc(args[2], ao.adviceObj, searchForNames);
45
						ao.adviceFunc = tmpName;
46
					} else {
47
						if ((dl.isFunction(args[0])) && (dl.isObject(args[1])) && (dl.isString(args[2]))) {
48
							ao.adviceType = "after";
49
							ao.srcObj = dj_global;
50
							var tmpName = dl.nameAnonFunc(args[0], ao.srcObj, searchForNames);
51
							ao.srcFunc = tmpName;
52
							ao.adviceObj = args[1];
53
							ao.adviceFunc = args[2];
54
						}
55
					}
56
				}
57
			}
58
			break;
59
		  case 4:
60
			if ((dl.isObject(args[0])) && (dl.isObject(args[2]))) {
61
				ao.adviceType = "after";
62
				ao.srcObj = args[0];
63
				ao.srcFunc = args[1];
64
				ao.adviceObj = args[2];
65
				ao.adviceFunc = args[3];
66
			} else {
67
				if ((dl.isString(args[0])) && (dl.isString(args[1])) && (dl.isObject(args[2]))) {
68
					ao.adviceType = args[0];
69
					ao.srcObj = dj_global;
70
					ao.srcFunc = args[1];
71
					ao.adviceObj = args[2];
72
					ao.adviceFunc = args[3];
73
				} else {
74
					if ((dl.isString(args[0])) && (dl.isFunction(args[1])) && (dl.isObject(args[2]))) {
75
						ao.adviceType = args[0];
76
						ao.srcObj = dj_global;
77
						var tmpName = dl.nameAnonFunc(args[1], dj_global, searchForNames);
78
						ao.srcFunc = tmpName;
79
						ao.adviceObj = args[2];
80
						ao.adviceFunc = args[3];
81
					} else {
82
						if ((dl.isString(args[0])) && (dl.isObject(args[1])) && (dl.isString(args[2])) && (dl.isFunction(args[3]))) {
83
							ao.srcObj = args[1];
84
							ao.srcFunc = args[2];
85
							var tmpName = dl.nameAnonFunc(args[3], dj_global, searchForNames);
86
							ao.adviceObj = dj_global;
87
							ao.adviceFunc = tmpName;
88
						} else {
89
							if (dl.isObject(args[1])) {
90
								ao.srcObj = args[1];
91
								ao.srcFunc = args[2];
92
								ao.adviceObj = dj_global;
93
								ao.adviceFunc = args[3];
94
							} else {
95
								if (dl.isObject(args[2])) {
96
									ao.srcObj = dj_global;
97
									ao.srcFunc = args[1];
98
									ao.adviceObj = args[2];
99
									ao.adviceFunc = args[3];
100
								} else {
101
									ao.srcObj = ao.adviceObj = ao.aroundObj = dj_global;
102
									ao.srcFunc = args[1];
103
									ao.adviceFunc = args[2];
104
									ao.aroundFunc = args[3];
105
								}
106
							}
107
						}
108
					}
109
				}
110
			}
111
			break;
112
		  case 6:
113
			ao.srcObj = args[1];
114
			ao.srcFunc = args[2];
115
			ao.adviceObj = args[3];
116
			ao.adviceFunc = args[4];
117
			ao.aroundFunc = args[5];
118
			ao.aroundObj = dj_global;
119
			break;
120
		  default:
121
			ao.srcObj = args[1];
122
			ao.srcFunc = args[2];
123
			ao.adviceObj = args[3];
124
			ao.adviceFunc = args[4];
125
			ao.aroundObj = args[5];
126
			ao.aroundFunc = args[6];
127
			ao.once = args[7];
128
			ao.delay = args[8];
129
			ao.rate = args[9];
130
			ao.adviceMsg = args[10];
131
			ao.maxCalls = (!isNaN(parseInt(args[11]))) ? args[11] : -1;
132
			break;
133
		}
134
		if (dl.isFunction(ao.aroundFunc)) {
135
			var tmpName = dl.nameAnonFunc(ao.aroundFunc, ao.aroundObj, searchForNames);
136
			ao.aroundFunc = tmpName;
137
		}
138
		if (dl.isFunction(ao.srcFunc)) {
139
			ao.srcFunc = dl.getNameInObj(ao.srcObj, ao.srcFunc);
140
		}
141
		if (dl.isFunction(ao.adviceFunc)) {
142
			ao.adviceFunc = dl.getNameInObj(ao.adviceObj, ao.adviceFunc);
143
		}
144
		if ((ao.aroundObj) && (dl.isFunction(ao.aroundFunc))) {
145
			ao.aroundFunc = dl.getNameInObj(ao.aroundObj, ao.aroundFunc);
146
		}
147
		if (!ao.srcObj) {
148
			dojo.raise("bad srcObj for srcFunc: " + ao.srcFunc);
149
		}
150
		if (!ao.adviceObj) {
151
			dojo.raise("bad adviceObj for adviceFunc: " + ao.adviceFunc);
152
		}
153
		if (!ao.adviceFunc) {
154
			dojo.debug("bad adviceFunc for srcFunc: " + ao.srcFunc);
155
			dojo.debugShallow(ao);
156
		}
157
		return ao;
158
	}
159
	this.connect = function () {
160
		if (arguments.length == 1) {
161
			var ao = arguments[0];
162
		} else {
163
			var ao = interpolateArgs(arguments, true);
164
		}
165
		if (dojo.lang.isArray(ao.srcObj) && ao.srcObj != "") {
166
			var tmpAO = {};
167
			for (var x in ao) {
168
				tmpAO[x] = ao[x];
169
			}
170
			var mjps = [];
171
			dojo.lang.forEach(ao.srcObj, function (src) {
172
				if ((dojo.render.html.capable) && (dojo.lang.isString(src))) {
173
					src = dojo.byId(src);
174
				}
175
				tmpAO.srcObj = src;
176
				mjps.push(dojo.event.connect.call(dojo.event, tmpAO));
177
			});
178
			return mjps;
179
		}
180
		var mjp = dojo.event.MethodJoinPoint.getForMethod(ao.srcObj, ao.srcFunc);
181
		if (ao.adviceFunc) {
182
			var mjp2 = dojo.event.MethodJoinPoint.getForMethod(ao.adviceObj, ao.adviceFunc);
183
		}
184
		mjp.kwAddAdvice(ao);
185
		return mjp;
186
	};
187
	this.log = function (a1, a2) {
188
		var kwArgs;
189
		if ((arguments.length == 1) && (typeof a1 == "object")) {
190
			kwArgs = a1;
191
		} else {
192
			kwArgs = {srcObj:a1, srcFunc:a2};
193
		}
194
		kwArgs.adviceFunc = function () {
195
			var argsStr = [];
196
			for (var x = 0; x < arguments.length; x++) {
197
				argsStr.push(arguments[x]);
198
			}
199
			dojo.debug("(" + kwArgs.srcObj + ")." + kwArgs.srcFunc, ":", argsStr.join(", "));
200
		};
201
		this.kwConnect(kwArgs);
202
	};
203
	this.connectBefore = function () {
204
		var args = ["before"];
205
		for (var i = 0; i < arguments.length; i++) {
206
			args.push(arguments[i]);
207
		}
208
		return this.connect.apply(this, args);
209
	};
210
	this.connectAround = function () {
211
		var args = ["around"];
212
		for (var i = 0; i < arguments.length; i++) {
213
			args.push(arguments[i]);
214
		}
215
		return this.connect.apply(this, args);
216
	};
217
	this.connectOnce = function () {
218
		var ao = interpolateArgs(arguments, true);
219
		ao.once = true;
220
		return this.connect(ao);
221
	};
222
	this.connectRunOnce = function () {
223
		var ao = interpolateArgs(arguments, true);
224
		ao.maxCalls = 1;
225
		return this.connect(ao);
226
	};
227
	this._kwConnectImpl = function (kwArgs, disconnect) {
228
		var fn = (disconnect) ? "disconnect" : "connect";
229
		if (typeof kwArgs["srcFunc"] == "function") {
230
			kwArgs.srcObj = kwArgs["srcObj"] || dj_global;
231
			var tmpName = dojo.lang.nameAnonFunc(kwArgs.srcFunc, kwArgs.srcObj, true);
232
			kwArgs.srcFunc = tmpName;
233
		}
234
		if (typeof kwArgs["adviceFunc"] == "function") {
235
			kwArgs.adviceObj = kwArgs["adviceObj"] || dj_global;
236
			var tmpName = dojo.lang.nameAnonFunc(kwArgs.adviceFunc, kwArgs.adviceObj, true);
237
			kwArgs.adviceFunc = tmpName;
238
		}
239
		kwArgs.srcObj = kwArgs["srcObj"] || dj_global;
240
		kwArgs.adviceObj = kwArgs["adviceObj"] || kwArgs["targetObj"] || dj_global;
241
		kwArgs.adviceFunc = kwArgs["adviceFunc"] || kwArgs["targetFunc"];
242
		return dojo.event[fn](kwArgs);
243
	};
244
	this.kwConnect = function (kwArgs) {
245
		return this._kwConnectImpl(kwArgs, false);
246
	};
247
	this.disconnect = function () {
248
		if (arguments.length == 1) {
249
			var ao = arguments[0];
250
		} else {
251
			var ao = interpolateArgs(arguments, true);
252
		}
253
		if (!ao.adviceFunc) {
254
			return;
255
		}
256
		if (dojo.lang.isString(ao.srcFunc) && (ao.srcFunc.toLowerCase() == "onkey")) {
257
			if (dojo.render.html.ie) {
258
				ao.srcFunc = "onkeydown";
259
				this.disconnect(ao);
260
			}
261
			ao.srcFunc = "onkeypress";
262
		}
263
		if (!ao.srcObj[ao.srcFunc]) {
264
			return null;
265
		}
266
		var mjp = dojo.event.MethodJoinPoint.getForMethod(ao.srcObj, ao.srcFunc, true);
267
		mjp.removeAdvice(ao.adviceObj, ao.adviceFunc, ao.adviceType, ao.once);
268
		return mjp;
269
	};
270
	this.kwDisconnect = function (kwArgs) {
271
		return this._kwConnectImpl(kwArgs, true);
272
	};
273
};
274
dojo.event.MethodInvocation = function (join_point, obj, args) {
275
	this.jp_ = join_point;
276
	this.object = obj;
277
	this.args = [];
278
	for (var x = 0; x < args.length; x++) {
279
		this.args[x] = args[x];
280
	}
281
	this.around_index = -1;
282
};
283
dojo.event.MethodInvocation.prototype.proceed = function () {
284
	this.around_index++;
285
	if (this.around_index >= this.jp_.around.length) {
286
		return this.jp_.object[this.jp_.methodname].apply(this.jp_.object, this.args);
287
	} else {
288
		var ti = this.jp_.around[this.around_index];
289
		var mobj = ti[0] || dj_global;
290
		var meth = ti[1];
291
		return mobj[meth].call(mobj, this);
292
	}
293
};
294
dojo.event.MethodJoinPoint = function (obj, funcName) {
295
	this.object = obj || dj_global;
296
	this.methodname = funcName;
297
	this.methodfunc = this.object[funcName];
298
	this.squelch = false;
299
};
300
dojo.event.MethodJoinPoint.getForMethod = function (obj, funcName) {
301
	if (!obj) {
302
		obj = dj_global;
303
	}
304
	var ofn = obj[funcName];
305
	if (!ofn) {
306
		ofn = obj[funcName] = function () {
307
		};
308
		if (!obj[funcName]) {
309
			dojo.raise("Cannot set do-nothing method on that object " + funcName);
310
		}
311
	} else {
312
		if ((typeof ofn != "function") && (!dojo.lang.isFunction(ofn)) && (!dojo.lang.isAlien(ofn))) {
313
			return null;
314
		}
315
	}
316
	var jpname = funcName + "$joinpoint";
317
	var jpfuncname = funcName + "$joinpoint$method";
318
	var joinpoint = obj[jpname];
319
	if (!joinpoint) {
320
		var isNode = false;
321
		if (dojo.event["browser"]) {
322
			if ((obj["attachEvent"]) || (obj["nodeType"]) || (obj["addEventListener"])) {
323
				isNode = true;
324
				dojo.event.browser.addClobberNodeAttrs(obj, [jpname, jpfuncname, funcName]);
325
			}
326
		}
327
		var origArity = ofn.length;
328
		obj[jpfuncname] = ofn;
329
		joinpoint = obj[jpname] = new dojo.event.MethodJoinPoint(obj, jpfuncname);
330
		if (!isNode) {
331
			obj[funcName] = function () {
332
				return joinpoint.run.apply(joinpoint, arguments);
333
			};
334
		} else {
335
			obj[funcName] = function () {
336
				var args = [];
337
				if (!arguments.length) {
338
					var evt = null;
339
					try {
340
						if (obj.ownerDocument) {
341
							evt = obj.ownerDocument.parentWindow.event;
342
						} else {
343
							if (obj.documentElement) {
344
								evt = obj.documentElement.ownerDocument.parentWindow.event;
345
							} else {
346
								if (obj.event) {
347
									evt = obj.event;
348
								} else {
349
									evt = window.event;
350
								}
351
							}
352
						}
353
					}
354
					catch (e) {
355
						evt = window.event;
356
					}
357
					if (evt) {
358
						args.push(dojo.event.browser.fixEvent(evt, this));
359
					}
360
				} else {
361
					for (var x = 0; x < arguments.length; x++) {
362
						if ((x == 0) && (dojo.event.browser.isEvent(arguments[x]))) {
363
							args.push(dojo.event.browser.fixEvent(arguments[x], this));
364
						} else {
365
							args.push(arguments[x]);
366
						}
367
					}
368
				}
369
				return joinpoint.run.apply(joinpoint, args);
370
			};
371
		}
372
		obj[funcName].__preJoinArity = origArity;
373
	}
374
	return joinpoint;
375
};
376
dojo.lang.extend(dojo.event.MethodJoinPoint, {squelch:false, unintercept:function () {
377
	this.object[this.methodname] = this.methodfunc;
378
	this.before = [];
379
	this.after = [];
380
	this.around = [];
381
}, disconnect:dojo.lang.forward("unintercept"), run:function () {
382
	var obj = this.object || dj_global;
383
	var args = arguments;
384
	var aargs = [];
385
	for (var x = 0; x < args.length; x++) {
386
		aargs[x] = args[x];
387
	}
388
	var unrollAdvice = function (marr) {
389
		if (!marr) {
390
			dojo.debug("Null argument to unrollAdvice()");
391
			return;
392
		}
393
		var callObj = marr[0] || dj_global;
394
		var callFunc = marr[1];
395
		if (!callObj[callFunc]) {
396
			dojo.raise("function \"" + callFunc + "\" does not exist on \"" + callObj + "\"");
397
		}
398
		var aroundObj = marr[2] || dj_global;
399
		var aroundFunc = marr[3];
400
		var msg = marr[6];
401
		var maxCount = marr[7];
402
		if (maxCount > -1) {
403
			if (maxCount == 0) {
404
				return;
405
			}
406
			marr[7]--;
407
		}
408
		var undef;
409
		var to = {args:[], jp_:this, object:obj, proceed:function () {
410
			return callObj[callFunc].apply(callObj, to.args);
411
		}};
412
		to.args = aargs;
413
		var delay = parseInt(marr[4]);
414
		var hasDelay = ((!isNaN(delay)) && (marr[4] !== null) && (typeof marr[4] != "undefined"));
415
		if (marr[5]) {
416
			var rate = parseInt(marr[5]);
417
			var cur = new Date();
418
			var timerSet = false;
419
			if ((marr["last"]) && ((cur - marr.last) <= rate)) {
420
				if (dojo.event._canTimeout) {
421
					if (marr["delayTimer"]) {
422
						clearTimeout(marr.delayTimer);
423
					}
424
					var tod = parseInt(rate * 2);
425
					var mcpy = dojo.lang.shallowCopy(marr);
426
					marr.delayTimer = setTimeout(function () {
427
						mcpy[5] = 0;
428
						unrollAdvice(mcpy);
429
					}, tod);
430
				}
431
				return;
432
			} else {
433
				marr.last = cur;
434
			}
435
		}
436
		if (aroundFunc) {
437
			aroundObj[aroundFunc].call(aroundObj, to);
438
		} else {
439
			if ((hasDelay) && ((dojo.render.html) || (dojo.render.svg))) {
440
				dj_global["setTimeout"](function () {
441
					if (msg) {
442
						callObj[callFunc].call(callObj, to);
443
					} else {
444
						callObj[callFunc].apply(callObj, args);
445
					}
446
				}, delay);
447
			} else {
448
				if (msg) {
449
					callObj[callFunc].call(callObj, to);
450
				} else {
451
					callObj[callFunc].apply(callObj, args);
452
				}
453
			}
454
		}
455
	};
456
	var unRollSquelch = function () {
457
		if (this.squelch) {
458
			try {
459
				return unrollAdvice.apply(this, arguments);
460
			}
461
			catch (e) {
462
				dojo.debug(e);
463
			}
464
		} else {
465
			return unrollAdvice.apply(this, arguments);
466
		}
467
	};
468
	if ((this["before"]) && (this.before.length > 0)) {
469
		dojo.lang.forEach(this.before.concat(new Array()), unRollSquelch);
470
	}
471
	var result;
472
	try {
473
		if ((this["around"]) && (this.around.length > 0)) {
474
			var mi = new dojo.event.MethodInvocation(this, obj, args);
475
			result = mi.proceed();
476
		} else {
477
			if (this.methodfunc) {
478
				result = this.object[this.methodname].apply(this.object, args);
479
			}
480
		}
481
	}
482
	catch (e) {
483
		if (!this.squelch) {
484
			dojo.debug(e, "when calling", this.methodname, "on", this.object, "with arguments", args);
485
			dojo.raise(e);
486
		}
487
	}
488
	if ((this["after"]) && (this.after.length > 0)) {
489
		dojo.lang.forEach(this.after.concat(new Array()), unRollSquelch);
490
	}
491
	return (this.methodfunc) ? result : null;
492
}, getArr:function (kind) {
493
	var type = "after";
494
	if ((typeof kind == "string") && (kind.indexOf("before") != -1)) {
495
		type = "before";
496
	} else {
497
		if (kind == "around") {
498
			type = "around";
499
		}
500
	}
501
	if (!this[type]) {
502
		this[type] = [];
503
	}
504
	return this[type];
505
}, kwAddAdvice:function (args) {
506
	this.addAdvice(args["adviceObj"], args["adviceFunc"], args["aroundObj"], args["aroundFunc"], args["adviceType"], args["precedence"], args["once"], args["delay"], args["rate"], args["adviceMsg"], args["maxCalls"]);
507
}, addAdvice:function (thisAdviceObj, thisAdvice, thisAroundObj, thisAround, adviceType, precedence, once, delay, rate, asMessage, maxCalls) {
508
	var arr = this.getArr(adviceType);
509
	if (!arr) {
510
		dojo.raise("bad this: " + this);
511
	}
512
	var ao = [thisAdviceObj, thisAdvice, thisAroundObj, thisAround, delay, rate, asMessage, maxCalls];
513
	if (once) {
514
		if (this.hasAdvice(thisAdviceObj, thisAdvice, adviceType, arr) >= 0) {
515
			return;
516
		}
517
	}
518
	if (precedence == "first") {
519
		arr.unshift(ao);
520
	} else {
521
		arr.push(ao);
522
	}
523
}, hasAdvice:function (thisAdviceObj, thisAdvice, adviceType, arr) {
524
	if (!arr) {
525
		arr = this.getArr(adviceType);
526
	}
527
	var ind = -1;
528
	for (var x = 0; x < arr.length; x++) {
529
		var aao = (typeof thisAdvice == "object") ? (new String(thisAdvice)).toString() : thisAdvice;
530
		var a1o = (typeof arr[x][1] == "object") ? (new String(arr[x][1])).toString() : arr[x][1];
531
		if ((arr[x][0] == thisAdviceObj) && (a1o == aao)) {
532
			ind = x;
533
		}
534
	}
535
	return ind;
536
}, removeAdvice:function (thisAdviceObj, thisAdvice, adviceType, once) {
537
	var arr = this.getArr(adviceType);
538
	var ind = this.hasAdvice(thisAdviceObj, thisAdvice, adviceType, arr);
539
	if (ind == -1) {
540
		return false;
541
	}
542
	while (ind != -1) {
543
		arr.splice(ind, 1);
544
		if (once) {
545
			break;
546
		}
547
		ind = this.hasAdvice(thisAdviceObj, thisAdvice, adviceType, arr);
548
	}
549
	return true;
550
}});
551