event.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2005 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A base class for event objects.
  16. *
  17. */
  18. goog.provide('goog.events.Event');
  19. goog.provide('goog.events.EventLike');
  20. /**
  21. * goog.events.Event no longer depends on goog.Disposable. Keep requiring
  22. * goog.Disposable here to not break projects which assume this dependency.
  23. * @suppress {extraRequire}
  24. */
  25. goog.require('goog.Disposable');
  26. goog.require('goog.events.EventId');
  27. /**
  28. * A typedef for event like objects that are dispatchable via the
  29. * goog.events.dispatchEvent function. strings are treated as the type for a
  30. * goog.events.Event. Objects are treated as an extension of a new
  31. * goog.events.Event with the type property of the object being used as the type
  32. * of the Event.
  33. * @typedef {string|Object|goog.events.Event|goog.events.EventId}
  34. */
  35. goog.events.EventLike;
  36. /**
  37. * A base class for event objects, so that they can support preventDefault and
  38. * stopPropagation.
  39. *
  40. * @suppress {underscore} Several properties on this class are technically
  41. * public, but referencing these properties outside this package is strongly
  42. * discouraged.
  43. *
  44. * @param {string|!goog.events.EventId} type Event Type.
  45. * @param {Object=} opt_target Reference to the object that is the target of
  46. * this event. It has to implement the {@code EventTarget} interface
  47. * declared at {@link http://developer.mozilla.org/en/DOM/EventTarget}.
  48. * @constructor
  49. */
  50. goog.events.Event = function(type, opt_target) {
  51. /**
  52. * Event type.
  53. * @type {string}
  54. */
  55. this.type = type instanceof goog.events.EventId ? String(type) : type;
  56. /**
  57. * TODO(tbreisacher): The type should probably be
  58. * EventTarget|goog.events.EventTarget.
  59. *
  60. * Target of the event.
  61. * @type {Object|undefined}
  62. */
  63. this.target = opt_target;
  64. /**
  65. * Object that had the listener attached.
  66. * @type {Object|undefined}
  67. */
  68. this.currentTarget = this.target;
  69. /**
  70. * Whether to cancel the event in internal capture/bubble processing for IE.
  71. * @type {boolean}
  72. * @public
  73. */
  74. this.propagationStopped_ = false;
  75. /**
  76. * Whether the default action has been prevented.
  77. * This is a property to match the W3C specification at
  78. * {@link http://www.w3.org/TR/DOM-Level-3-Events/
  79. * #events-event-type-defaultPrevented}.
  80. * Must be treated as read-only outside the class.
  81. * @type {boolean}
  82. */
  83. this.defaultPrevented = false;
  84. /**
  85. * Return value for in internal capture/bubble processing for IE.
  86. * @type {boolean}
  87. * @public
  88. */
  89. this.returnValue_ = true;
  90. };
  91. /**
  92. * Stops event propagation.
  93. */
  94. goog.events.Event.prototype.stopPropagation = function() {
  95. this.propagationStopped_ = true;
  96. };
  97. /**
  98. * Prevents the default action, for example a link redirecting to a url.
  99. */
  100. goog.events.Event.prototype.preventDefault = function() {
  101. this.defaultPrevented = true;
  102. this.returnValue_ = false;
  103. };
  104. /**
  105. * Stops the propagation of the event. It is equivalent to
  106. * {@code e.stopPropagation()}, but can be used as the callback argument of
  107. * {@link goog.events.listen} without declaring another function.
  108. * @param {!goog.events.Event} e An event.
  109. */
  110. goog.events.Event.stopPropagation = function(e) {
  111. e.stopPropagation();
  112. };
  113. /**
  114. * Prevents the default action. It is equivalent to
  115. * {@code e.preventDefault()}, but can be used as the callback argument of
  116. * {@link goog.events.listen} without declaring another function.
  117. * @param {!goog.events.Event} e An event.
  118. */
  119. goog.events.Event.preventDefault = function(e) {
  120. e.preventDefault();
  121. };