eventMaker.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. define(["jquery", "util"], function ($, util) {
  5. var eventMaker = util.Module("eventMaker");
  6. eventMaker.performClick = function (target) {
  7. // FIXME: should accept other parameters, like Ctrl/Alt/etc
  8. var event = document.createEvent("MouseEvents");
  9. event.initMouseEvent(
  10. "click", // type
  11. true, // canBubble
  12. true, // cancelable
  13. window, // view
  14. 0, // detail
  15. 0, // screenX
  16. 0, // screenY
  17. 0, // clientX
  18. 0, // clientY
  19. false, // ctrlKey
  20. false, // altKey
  21. false, // shiftKey
  22. false, // metaKey
  23. 0, // button
  24. null // relatedTarget
  25. );
  26. // FIXME: I'm not sure this custom attribute always propagates?
  27. // seems okay in Firefox/Chrome, but I've had problems with
  28. // setting attributes on keyboard events in the past.
  29. event.togetherjsInternal = true;
  30. target = $(target)[0];
  31. var cancelled = target.dispatchEvent(event);
  32. if (cancelled) {
  33. return;
  34. }
  35. if (target.tagName == "A") {
  36. var href = target.href;
  37. if (href) {
  38. location.href = href;
  39. return;
  40. }
  41. }
  42. // FIXME: should do button clicks (like a form submit)
  43. // FIXME: should run .onclick() as well
  44. };
  45. eventMaker.fireChange = function (target) {
  46. target = $(target)[0];
  47. var event = document.createEvent("HTMLEvents");
  48. event.initEvent("change", true, true);
  49. target.dispatchEvent(event);
  50. };
  51. return eventMaker;
  52. });