/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. */
  5. function EventUtils() {
  6. throw 'RuntimeException: EventUtils is a static utility class and may not be instantiated';
  7. }
  8. EventUtils.addEventListener = function(target, type, callback, captures) {
  9. var result = true;
  10. if (target.addEventListener) {
  11. target.addEventListener(type, callback, captures);
  12. } else if (target.attachEvent) {
  13. result = target.attachEvent('on' + type, callback, captures);
  14. } else {
  15. // IE 5 Mac and some others
  16. target['on'+type] = callback;
  17. }
  18. return result;
  19. }
  20. EventUtils.findTarget = function(e, allowTextNodes) {
  21. var target;
  22. if (window.event) {
  23. target = window.event.srcElement;
  24. } else if (e) {
  25. target = e.target;
  26. } else {
  27. // we can't find it, just use window
  28. target = window;
  29. }
  30. if (!allowTextNodes && target.nodeType == 3) {
  31. target = target.parentNode;
  32. }
  33. return target;
  34. }
  35. /**
  36. * @return {x, y}
  37. */
  38. EventUtils.getMousePosition = function(e) {
  39. var posx = 0;
  40. var posy = 0;
  41. if (!e)
  42. {
  43. e = window.event;
  44. }
  45. if (e.pageX || e.pageY)
  46. {
  47. posx = e.pageX;
  48. posy = e.pageY;
  49. }
  50. else if (e.clientX || e.clientY)
  51. {
  52. posx = e.clientX + document.body.scrollLeft;
  53. posy = e.clientY + document.body.scrollTop;
  54. }
  55. return { x : posx, y : posy };
  56. }
  57. function TextSelectionEvent(selectedText, mousePosition) {
  58. this.selectedText = selectedText;
  59. this.x = mousePosition.x;
  60. this.y = mousePosition.y;
  61. }