Subversion Repositories Applications.papyrus

Rev

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