actioneventwrapper.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2009 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 Action event wrapper implementation.
  16. * @author eae@google.com (Emil A Eklund)
  17. */
  18. goog.provide('goog.events.actionEventWrapper');
  19. goog.require('goog.a11y.aria');
  20. goog.require('goog.a11y.aria.Role');
  21. goog.require('goog.dom');
  22. goog.require('goog.events');
  23. /** @suppress {extraRequire} */
  24. goog.require('goog.events.EventHandler');
  25. goog.require('goog.events.EventType');
  26. goog.require('goog.events.EventWrapper');
  27. goog.require('goog.events.KeyCodes');
  28. goog.require('goog.userAgent');
  29. /**
  30. * Event wrapper for action handling. Fires when an element is activated either
  31. * by clicking it or by focusing it and pressing Enter.
  32. *
  33. * @constructor
  34. * @implements {goog.events.EventWrapper}
  35. * @private
  36. */
  37. goog.events.ActionEventWrapper_ = function() {};
  38. /**
  39. * @interface
  40. * @private
  41. */
  42. goog.events.ActionEventWrapper_.FunctionExtension_ = function() {};
  43. /** @type {!Object|undefined} */
  44. goog.events.ActionEventWrapper_.FunctionExtension_.prototype.scope_;
  45. /** @type {function(?):?|{handleEvent:function(?):?}|null} */
  46. goog.events.ActionEventWrapper_.FunctionExtension_.prototype.listener_;
  47. /**
  48. * Singleton instance of ActionEventWrapper_.
  49. * @type {goog.events.ActionEventWrapper_}
  50. */
  51. goog.events.actionEventWrapper = new goog.events.ActionEventWrapper_();
  52. /**
  53. * Event types used by the wrapper.
  54. *
  55. * @type {Array<goog.events.EventType>}
  56. * @private
  57. */
  58. goog.events.ActionEventWrapper_.EVENT_TYPES_ = [
  59. goog.events.EventType.CLICK,
  60. goog.userAgent.GECKO ? goog.events.EventType.KEYPRESS :
  61. goog.events.EventType.KEYDOWN,
  62. goog.events.EventType.KEYUP
  63. ];
  64. /**
  65. * Adds an event listener using the wrapper on a DOM Node or an object that has
  66. * implemented {@link goog.events.EventTarget}. A listener can only be added
  67. * once to an object.
  68. *
  69. * @param {goog.events.ListenableType} target The target to listen to events on.
  70. * @param {function(?):?|{handleEvent:function(?):?}|null} listener Callback
  71. * method, or an object with a handleEvent function.
  72. * @param {boolean=} opt_capt Whether to fire in capture phase (defaults to
  73. * false).
  74. * @param {Object=} opt_scope Element in whose scope to call the listener.
  75. * @param {goog.events.EventHandler=} opt_eventHandler Event handler to add
  76. * listener to.
  77. * @override
  78. */
  79. goog.events.ActionEventWrapper_.prototype.listen = function(
  80. target, listener, opt_capt, opt_scope, opt_eventHandler) {
  81. var callback = function(e) {
  82. var listenerFn = goog.events.wrapListener(listener);
  83. var role = goog.dom.isElement(e.target) ?
  84. goog.a11y.aria.getRole(/** @type {!Element} */ (e.target)) :
  85. null;
  86. if (e.type == goog.events.EventType.CLICK && e.isMouseActionButton()) {
  87. listenerFn.call(opt_scope, e);
  88. } else if (
  89. (e.keyCode == goog.events.KeyCodes.ENTER ||
  90. e.keyCode == goog.events.KeyCodes.MAC_ENTER) &&
  91. e.type != goog.events.EventType.KEYUP) {
  92. // convert keydown to keypress for backward compatibility.
  93. e.type = goog.events.EventType.KEYPRESS;
  94. listenerFn.call(opt_scope, e);
  95. } else if (
  96. e.keyCode == goog.events.KeyCodes.SPACE &&
  97. e.type == goog.events.EventType.KEYUP &&
  98. (role == goog.a11y.aria.Role.BUTTON ||
  99. role == goog.a11y.aria.Role.TAB)) {
  100. listenerFn.call(opt_scope, e);
  101. e.preventDefault();
  102. }
  103. };
  104. callback.listener_ = listener;
  105. callback.scope_ = opt_scope;
  106. if (opt_eventHandler) {
  107. opt_eventHandler.listen(
  108. target, goog.events.ActionEventWrapper_.EVENT_TYPES_, callback,
  109. opt_capt);
  110. } else {
  111. goog.events.listen(
  112. target, goog.events.ActionEventWrapper_.EVENT_TYPES_, callback,
  113. opt_capt);
  114. }
  115. };
  116. /**
  117. * Removes an event listener added using goog.events.EventWrapper.listen.
  118. *
  119. * @param {goog.events.ListenableType} target The node to remove listener from.
  120. * @param {function(?):?|{handleEvent:function(?):?}|null} listener Callback
  121. * method, or an object with a handleEvent function.
  122. * @param {boolean=} opt_capt Whether to fire in capture phase (defaults to
  123. * false).
  124. * @param {Object=} opt_scope Element in whose scope to call the listener.
  125. * @param {goog.events.EventHandler=} opt_eventHandler Event handler to remove
  126. * listener from.
  127. * @override
  128. */
  129. goog.events.ActionEventWrapper_.prototype.unlisten = function(
  130. target, listener, opt_capt, opt_scope, opt_eventHandler) {
  131. for (var type, j = 0; type = goog.events.ActionEventWrapper_.EVENT_TYPES_[j];
  132. j++) {
  133. var listeners = goog.events.getListeners(target, type, !!opt_capt);
  134. for (var obj, i = 0; obj = listeners[i]; i++) {
  135. var objListener =
  136. /** @type {!goog.events.ActionEventWrapper_.FunctionExtension_} */ (
  137. obj.listener);
  138. if (objListener.listener_ == listener &&
  139. objListener.scope_ == opt_scope) {
  140. if (opt_eventHandler) {
  141. opt_eventHandler.unlisten(
  142. target, type, obj.listener, opt_capt, opt_scope);
  143. } else {
  144. goog.events.unlisten(target, type, obj.listener, opt_capt, opt_scope);
  145. }
  146. break;
  147. }
  148. }
  149. }
  150. };