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
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.event.browser");
14
dojo.require("dojo.event.common");
15
dojo._ie_clobber = new function () {
16
	this.clobberNodes = [];
17
	function nukeProp(node, prop) {
18
		try {
19
			node[prop] = null;
20
		}
21
		catch (e) {
22
		}
23
		try {
24
			delete node[prop];
25
		}
26
		catch (e) {
27
		}
28
		try {
29
			node.removeAttribute(prop);
30
		}
31
		catch (e) {
32
		}
33
	}
34
	this.clobber = function (nodeRef) {
35
		var na;
36
		var tna;
37
		if (nodeRef) {
38
			tna = nodeRef.all || nodeRef.getElementsByTagName("*");
39
			na = [nodeRef];
40
			for (var x = 0; x < tna.length; x++) {
41
				if (tna[x]["__doClobber__"]) {
42
					na.push(tna[x]);
43
				}
44
			}
45
		} else {
46
			try {
47
				window.onload = null;
48
			}
49
			catch (e) {
50
			}
51
			na = (this.clobberNodes.length) ? this.clobberNodes : document.all;
52
		}
53
		tna = null;
54
		var basis = {};
55
		for (var i = na.length - 1; i >= 0; i = i - 1) {
56
			var el = na[i];
57
			try {
58
				if (el && el["__clobberAttrs__"]) {
59
					for (var j = 0; j < el.__clobberAttrs__.length; j++) {
60
						nukeProp(el, el.__clobberAttrs__[j]);
61
					}
62
					nukeProp(el, "__clobberAttrs__");
63
					nukeProp(el, "__doClobber__");
64
				}
65
			}
66
			catch (e) {
67
			}
68
		}
69
		na = null;
70
	};
71
};
72
if (dojo.render.html.ie) {
73
	dojo.addOnUnload(function () {
74
		dojo._ie_clobber.clobber();
75
		try {
76
			if ((dojo["widget"]) && (dojo.widget["manager"])) {
77
				dojo.widget.manager.destroyAll();
78
			}
79
		}
80
		catch (e) {
81
		}
82
		if (dojo.widget) {
83
			for (var name in dojo.widget._templateCache) {
84
				if (dojo.widget._templateCache[name].node) {
85
					dojo.dom.destroyNode(dojo.widget._templateCache[name].node);
86
					dojo.widget._templateCache[name].node = null;
87
					delete dojo.widget._templateCache[name].node;
88
				}
89
			}
90
		}
91
		try {
92
			window.onload = null;
93
		}
94
		catch (e) {
95
		}
96
		try {
97
			window.onunload = null;
98
		}
99
		catch (e) {
100
		}
101
		dojo._ie_clobber.clobberNodes = [];
102
	});
103
}
104
dojo.event.browser = new function () {
105
	var clobberIdx = 0;
106
	this.normalizedEventName = function (eventName) {
107
		switch (eventName) {
108
		  case "CheckboxStateChange":
109
		  case "DOMAttrModified":
110
		  case "DOMMenuItemActive":
111
		  case "DOMMenuItemInactive":
112
		  case "DOMMouseScroll":
113
		  case "DOMNodeInserted":
114
		  case "DOMNodeRemoved":
115
		  case "RadioStateChange":
116
			return eventName;
117
			break;
118
		  default:
119
			var lcn = eventName.toLowerCase();
120
			return (lcn.indexOf("on") == 0) ? lcn.substr(2) : lcn;
121
			break;
122
		}
123
	};
124
	this.clean = function (node) {
125
		if (dojo.render.html.ie) {
126
			dojo._ie_clobber.clobber(node);
127
		}
128
	};
129
	this.addClobberNode = function (node) {
130
		if (!dojo.render.html.ie) {
131
			return;
132
		}
133
		if (!node["__doClobber__"]) {
134
			node.__doClobber__ = true;
135
			dojo._ie_clobber.clobberNodes.push(node);
136
			node.__clobberAttrs__ = [];
137
		}
138
	};
139
	this.addClobberNodeAttrs = function (node, props) {
140
		if (!dojo.render.html.ie) {
141
			return;
142
		}
143
		this.addClobberNode(node);
144
		for (var x = 0; x < props.length; x++) {
145
			node.__clobberAttrs__.push(props[x]);
146
		}
147
	};
148
	this.removeListener = function (node, evtName, fp, capture) {
149
		if (!capture) {
150
			var capture = false;
151
		}
152
		evtName = dojo.event.browser.normalizedEventName(evtName);
153
		if (evtName == "key") {
154
			if (dojo.render.html.ie) {
155
				this.removeListener(node, "onkeydown", fp, capture);
156
			}
157
			evtName = "keypress";
158
		}
159
		if (node.removeEventListener) {
160
			node.removeEventListener(evtName, fp, capture);
161
		}
162
	};
163
	this.addListener = function (node, evtName, fp, capture, dontFix) {
164
		if (!node) {
165
			return;
166
		}
167
		if (!capture) {
168
			var capture = false;
169
		}
170
		evtName = dojo.event.browser.normalizedEventName(evtName);
171
		if (evtName == "key") {
172
			if (dojo.render.html.ie) {
173
				this.addListener(node, "onkeydown", fp, capture, dontFix);
174
			}
175
			evtName = "keypress";
176
		}
177
		if (!dontFix) {
178
			var newfp = function (evt) {
179
				if (!evt) {
180
					evt = window.event;
181
				}
182
				var ret = fp(dojo.event.browser.fixEvent(evt, this));
183
				if (capture) {
184
					dojo.event.browser.stopEvent(evt);
185
				}
186
				return ret;
187
			};
188
		} else {
189
			newfp = fp;
190
		}
191
		if (node.addEventListener) {
192
			node.addEventListener(evtName, newfp, capture);
193
			return newfp;
194
		} else {
195
			evtName = "on" + evtName;
196
			if (typeof node[evtName] == "function") {
197
				var oldEvt = node[evtName];
198
				node[evtName] = function (e) {
199
					oldEvt(e);
200
					return newfp(e);
201
				};
202
			} else {
203
				node[evtName] = newfp;
204
			}
205
			if (dojo.render.html.ie) {
206
				this.addClobberNodeAttrs(node, [evtName]);
207
			}
208
			return newfp;
209
		}
210
	};
211
	this.isEvent = function (obj) {
212
		return (typeof obj != "undefined") && (obj) && (typeof Event != "undefined") && (obj.eventPhase);
213
	};
214
	this.currentEvent = null;
215
	this.callListener = function (listener, curTarget) {
216
		if (typeof listener != "function") {
217
			dojo.raise("listener not a function: " + listener);
218
		}
219
		dojo.event.browser.currentEvent.currentTarget = curTarget;
220
		return listener.call(curTarget, dojo.event.browser.currentEvent);
221
	};
222
	this._stopPropagation = function () {
223
		dojo.event.browser.currentEvent.cancelBubble = true;
224
	};
225
	this._preventDefault = function () {
226
		dojo.event.browser.currentEvent.returnValue = false;
227
	};
228
	this.keys = {KEY_BACKSPACE:8, KEY_TAB:9, KEY_CLEAR:12, KEY_ENTER:13, KEY_SHIFT:16, KEY_CTRL:17, KEY_ALT:18, KEY_PAUSE:19, KEY_CAPS_LOCK:20, KEY_ESCAPE:27, KEY_SPACE:32, KEY_PAGE_UP:33, KEY_PAGE_DOWN:34, KEY_END:35, KEY_HOME:36, KEY_LEFT_ARROW:37, KEY_UP_ARROW:38, KEY_RIGHT_ARROW:39, KEY_DOWN_ARROW:40, KEY_INSERT:45, KEY_DELETE:46, KEY_HELP:47, KEY_LEFT_WINDOW:91, KEY_RIGHT_WINDOW:92, KEY_SELECT:93, KEY_NUMPAD_0:96, KEY_NUMPAD_1:97, KEY_NUMPAD_2:98, KEY_NUMPAD_3:99, KEY_NUMPAD_4:100, KEY_NUMPAD_5:101, KEY_NUMPAD_6:102, KEY_NUMPAD_7:103, KEY_NUMPAD_8:104, KEY_NUMPAD_9:105, KEY_NUMPAD_MULTIPLY:106, KEY_NUMPAD_PLUS:107, KEY_NUMPAD_ENTER:108, KEY_NUMPAD_MINUS:109, KEY_NUMPAD_PERIOD:110, KEY_NUMPAD_DIVIDE:111, KEY_F1:112, KEY_F2:113, KEY_F3:114, KEY_F4:115, KEY_F5:116, KEY_F6:117, KEY_F7:118, KEY_F8:119, KEY_F9:120, KEY_F10:121, KEY_F11:122, KEY_F12:123, KEY_F13:124, KEY_F14:125, KEY_F15:126, KEY_NUM_LOCK:144, KEY_SCROLL_LOCK:145};
229
	this.revKeys = [];
230
	for (var key in this.keys) {
231
		this.revKeys[this.keys[key]] = key;
232
	}
233
	this.fixEvent = function (evt, sender) {
234
		if (!evt) {
235
			if (window["event"]) {
236
				evt = window.event;
237
			}
238
		}
239
		if ((evt["type"]) && (evt["type"].indexOf("key") == 0)) {
240
			evt.keys = this.revKeys;
241
			for (var key in this.keys) {
242
				evt[key] = this.keys[key];
243
			}
244
			if (evt["type"] == "keydown" && dojo.render.html.ie) {
245
				switch (evt.keyCode) {
246
				  case evt.KEY_SHIFT:
247
				  case evt.KEY_CTRL:
248
				  case evt.KEY_ALT:
249
				  case evt.KEY_CAPS_LOCK:
250
				  case evt.KEY_LEFT_WINDOW:
251
				  case evt.KEY_RIGHT_WINDOW:
252
				  case evt.KEY_SELECT:
253
				  case evt.KEY_NUM_LOCK:
254
				  case evt.KEY_SCROLL_LOCK:
255
				  case evt.KEY_NUMPAD_0:
256
				  case evt.KEY_NUMPAD_1:
257
				  case evt.KEY_NUMPAD_2:
258
				  case evt.KEY_NUMPAD_3:
259
				  case evt.KEY_NUMPAD_4:
260
				  case evt.KEY_NUMPAD_5:
261
				  case evt.KEY_NUMPAD_6:
262
				  case evt.KEY_NUMPAD_7:
263
				  case evt.KEY_NUMPAD_8:
264
				  case evt.KEY_NUMPAD_9:
265
				  case evt.KEY_NUMPAD_PERIOD:
266
					break;
267
				  case evt.KEY_NUMPAD_MULTIPLY:
268
				  case evt.KEY_NUMPAD_PLUS:
269
				  case evt.KEY_NUMPAD_ENTER:
270
				  case evt.KEY_NUMPAD_MINUS:
271
				  case evt.KEY_NUMPAD_DIVIDE:
272
					break;
273
				  case evt.KEY_PAUSE:
274
				  case evt.KEY_TAB:
275
				  case evt.KEY_BACKSPACE:
276
				  case evt.KEY_ENTER:
277
				  case evt.KEY_ESCAPE:
278
				  case evt.KEY_PAGE_UP:
279
				  case evt.KEY_PAGE_DOWN:
280
				  case evt.KEY_END:
281
				  case evt.KEY_HOME:
282
				  case evt.KEY_LEFT_ARROW:
283
				  case evt.KEY_UP_ARROW:
284
				  case evt.KEY_RIGHT_ARROW:
285
				  case evt.KEY_DOWN_ARROW:
286
				  case evt.KEY_INSERT:
287
				  case evt.KEY_DELETE:
288
				  case evt.KEY_F1:
289
				  case evt.KEY_F2:
290
				  case evt.KEY_F3:
291
				  case evt.KEY_F4:
292
				  case evt.KEY_F5:
293
				  case evt.KEY_F6:
294
				  case evt.KEY_F7:
295
				  case evt.KEY_F8:
296
				  case evt.KEY_F9:
297
				  case evt.KEY_F10:
298
				  case evt.KEY_F11:
299
				  case evt.KEY_F12:
300
				  case evt.KEY_F12:
301
				  case evt.KEY_F13:
302
				  case evt.KEY_F14:
303
				  case evt.KEY_F15:
304
				  case evt.KEY_CLEAR:
305
				  case evt.KEY_HELP:
306
					evt.key = evt.keyCode;
307
					break;
308
				  default:
309
					if (evt.ctrlKey || evt.altKey) {
310
						var unifiedCharCode = evt.keyCode;
311
						if (unifiedCharCode >= 65 && unifiedCharCode <= 90 && evt.shiftKey == false) {
312
							unifiedCharCode += 32;
313
						}
314
						if (unifiedCharCode >= 1 && unifiedCharCode <= 26 && evt.ctrlKey) {
315
							unifiedCharCode += 96;
316
						}
317
						evt.key = String.fromCharCode(unifiedCharCode);
318
					}
319
				}
320
			} else {
321
				if (evt["type"] == "keypress") {
322
					if (dojo.render.html.opera) {
323
						if (evt.which == 0) {
324
							evt.key = evt.keyCode;
325
						} else {
326
							if (evt.which > 0) {
327
								switch (evt.which) {
328
								  case evt.KEY_SHIFT:
329
								  case evt.KEY_CTRL:
330
								  case evt.KEY_ALT:
331
								  case evt.KEY_CAPS_LOCK:
332
								  case evt.KEY_NUM_LOCK:
333
								  case evt.KEY_SCROLL_LOCK:
334
									break;
335
								  case evt.KEY_PAUSE:
336
								  case evt.KEY_TAB:
337
								  case evt.KEY_BACKSPACE:
338
								  case evt.KEY_ENTER:
339
								  case evt.KEY_ESCAPE:
340
									evt.key = evt.which;
341
									break;
342
								  default:
343
									var unifiedCharCode = evt.which;
344
									if ((evt.ctrlKey || evt.altKey || evt.metaKey) && (evt.which >= 65 && evt.which <= 90 && evt.shiftKey == false)) {
345
										unifiedCharCode += 32;
346
									}
347
									evt.key = String.fromCharCode(unifiedCharCode);
348
								}
349
							}
350
						}
351
					} else {
352
						if (dojo.render.html.ie) {
353
							if (!evt.ctrlKey && !evt.altKey && evt.keyCode >= evt.KEY_SPACE) {
354
								evt.key = String.fromCharCode(evt.keyCode);
355
							}
356
						} else {
357
							if (dojo.render.html.safari) {
358
								switch (evt.keyCode) {
359
								  case 25:
360
									evt.key = evt.KEY_TAB;
361
									evt.shift = true;
362
									break;
363
								  case 63232:
364
									evt.key = evt.KEY_UP_ARROW;
365
									break;
366
								  case 63233:
367
									evt.key = evt.KEY_DOWN_ARROW;
368
									break;
369
								  case 63234:
370
									evt.key = evt.KEY_LEFT_ARROW;
371
									break;
372
								  case 63235:
373
									evt.key = evt.KEY_RIGHT_ARROW;
374
									break;
375
								  case 63236:
376
									evt.key = evt.KEY_F1;
377
									break;
378
								  case 63237:
379
									evt.key = evt.KEY_F2;
380
									break;
381
								  case 63238:
382
									evt.key = evt.KEY_F3;
383
									break;
384
								  case 63239:
385
									evt.key = evt.KEY_F4;
386
									break;
387
								  case 63240:
388
									evt.key = evt.KEY_F5;
389
									break;
390
								  case 63241:
391
									evt.key = evt.KEY_F6;
392
									break;
393
								  case 63242:
394
									evt.key = evt.KEY_F7;
395
									break;
396
								  case 63243:
397
									evt.key = evt.KEY_F8;
398
									break;
399
								  case 63244:
400
									evt.key = evt.KEY_F9;
401
									break;
402
								  case 63245:
403
									evt.key = evt.KEY_F10;
404
									break;
405
								  case 63246:
406
									evt.key = evt.KEY_F11;
407
									break;
408
								  case 63247:
409
									evt.key = evt.KEY_F12;
410
									break;
411
								  case 63250:
412
									evt.key = evt.KEY_PAUSE;
413
									break;
414
								  case 63272:
415
									evt.key = evt.KEY_DELETE;
416
									break;
417
								  case 63273:
418
									evt.key = evt.KEY_HOME;
419
									break;
420
								  case 63275:
421
									evt.key = evt.KEY_END;
422
									break;
423
								  case 63276:
424
									evt.key = evt.KEY_PAGE_UP;
425
									break;
426
								  case 63277:
427
									evt.key = evt.KEY_PAGE_DOWN;
428
									break;
429
								  case 63302:
430
									evt.key = evt.KEY_INSERT;
431
									break;
432
								  case 63248:
433
								  case 63249:
434
								  case 63289:
435
									break;
436
								  default:
437
									evt.key = evt.charCode >= evt.KEY_SPACE ? String.fromCharCode(evt.charCode) : evt.keyCode;
438
								}
439
							} else {
440
								evt.key = evt.charCode > 0 ? String.fromCharCode(evt.charCode) : evt.keyCode;
441
							}
442
						}
443
					}
444
				}
445
			}
446
		}
447
		if (dojo.render.html.ie) {
448
			if (!evt.target) {
449
				evt.target = evt.srcElement;
450
			}
451
			if (!evt.currentTarget) {
452
				evt.currentTarget = (sender ? sender : evt.srcElement);
453
			}
454
			if (!evt.layerX) {
455
				evt.layerX = evt.offsetX;
456
			}
457
			if (!evt.layerY) {
458
				evt.layerY = evt.offsetY;
459
			}
460
			var doc = (evt.srcElement && evt.srcElement.ownerDocument) ? evt.srcElement.ownerDocument : document;
461
			var docBody = ((dojo.render.html.ie55) || (doc["compatMode"] == "BackCompat")) ? doc.body : doc.documentElement;
462
			if (!evt.pageX) {
463
				evt.pageX = evt.clientX + (docBody.scrollLeft || 0);
464
			}
465
			if (!evt.pageY) {
466
				evt.pageY = evt.clientY + (docBody.scrollTop || 0);
467
			}
468
			if (evt.type == "mouseover") {
469
				evt.relatedTarget = evt.fromElement;
470
			}
471
			if (evt.type == "mouseout") {
472
				evt.relatedTarget = evt.toElement;
473
			}
474
			this.currentEvent = evt;
475
			evt.callListener = this.callListener;
476
			evt.stopPropagation = this._stopPropagation;
477
			evt.preventDefault = this._preventDefault;
478
		}
479
		return evt;
480
	};
481
	this.stopEvent = function (evt) {
482
		if (window.event) {
483
			evt.cancelBubble = true;
484
			evt.returnValue = false;
485
		} else {
486
			evt.preventDefault();
487
			evt.stopPropagation();
488
		}
489
	};
490
};
491