PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/EventUtils.js

http://panojs.googlecode.com/
JavaScript | 70 lines | 52 code | 9 blank | 9 comment | 17 complexity | 10222bb799b144964a68ba3d24e107bd MD5 | raw file
 1/** 
 2 * A static utility class for accessing browser-neutral event properties and
 3 * methods.
 4 */
 5function EventUtils() {
 6	throw 'RuntimeException: EventUtils is a static utility class and may not be instantiated';
 7}
 8
 9EventUtils.addEventListener = function(target, type, callback, captures) {
10	var result = true;
11	if (target.addEventListener) {
12		target.addEventListener(type, callback, captures);
13	} else if (target.attachEvent) {
14		result = target.attachEvent('on' + type, callback, captures);
15	} else {
16		// IE 5 Mac and some others
17		target['on'+type] = callback;
18	}
19
20	return result;
21}
22
23EventUtils.findTarget = function(e, allowTextNodes) {
24    var target;
25	if (window.event) {
26		target = window.event.srcElement;
27	} else if (e) {
28		target = e.target;
29	} else { 
30		// we can't find it, just use window
31		target = window;
32	}
33
34	if (!allowTextNodes && target.nodeType == 3) {
35		target = target.parentNode;
36	}
37
38	return target;
39}
40
41/**
42 * @return {x, y}
43 */
44EventUtils.getMousePosition = function(e) {
45	var posx = 0;
46	var posy = 0;
47	if (!e)
48	{
49		e = window.event;
50	}
51
52	if (e.pageX || e.pageY)
53	{
54		posx = e.pageX;
55		posy = e.pageY;
56	}
57	else if (e.clientX || e.clientY)
58	{
59		posx = e.clientX + document.body.scrollLeft;
60		posy = e.clientY + document.body.scrollTop;
61	}
62
63	return { x : posx, y : posy };
64}
65
66function TextSelectionEvent(selectedText, mousePosition) {
67	this.selectedText = selectedText;
68	this.x = mousePosition.x;
69	this.y = mousePosition.y;
70}