actionhandler.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2007 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 This file contains a class to provide a unified mechanism for
  16. * CLICK and enter KEYDOWN events. This provides better accessibility by
  17. * providing the given functionality to a keyboard user which is otherwise
  18. * would be available only via a mouse click.
  19. *
  20. * If there is an existing CLICK listener or planning to be added as below -
  21. *
  22. * <code>this.eventHandler_.listen(el, CLICK, this.onClick_);<code>
  23. *
  24. * it can be replaced with an ACTION listener as follows:
  25. *
  26. * <code>this.eventHandler_.listen(
  27. * new goog.events.ActionHandler(el),
  28. * ACTION,
  29. * this.onAction_);<code>
  30. *
  31. */
  32. goog.provide('goog.events.ActionEvent');
  33. goog.provide('goog.events.ActionHandler');
  34. goog.provide('goog.events.ActionHandler.EventType');
  35. goog.provide('goog.events.BeforeActionEvent');
  36. goog.require('goog.events');
  37. goog.require('goog.events.BrowserEvent');
  38. goog.require('goog.events.EventTarget');
  39. goog.require('goog.events.EventType');
  40. goog.require('goog.events.KeyCodes');
  41. goog.require('goog.userAgent');
  42. /**
  43. * A wrapper around an element that you want to listen to ACTION events on.
  44. * @param {Element|Document} element The element or document to listen on.
  45. * @constructor
  46. * @extends {goog.events.EventTarget}
  47. * @final
  48. */
  49. goog.events.ActionHandler = function(element) {
  50. goog.events.EventTarget.call(this);
  51. /**
  52. * This is the element that we will listen to events on.
  53. * @type {Element|Document}
  54. * @private
  55. */
  56. this.element_ = element;
  57. goog.events.listen(
  58. element, goog.events.ActionHandler.KEY_EVENT_TYPE_, this.handleKeyDown_,
  59. false, this);
  60. goog.events.listen(
  61. element, goog.events.EventType.CLICK, this.handleClick_, false, this);
  62. };
  63. goog.inherits(goog.events.ActionHandler, goog.events.EventTarget);
  64. /**
  65. * Enum type for the events fired by the action handler
  66. * @enum {string}
  67. */
  68. goog.events.ActionHandler.EventType = {
  69. ACTION: 'action',
  70. BEFOREACTION: 'beforeaction'
  71. };
  72. /**
  73. * Key event type to listen for.
  74. * @type {string}
  75. * @private
  76. */
  77. goog.events.ActionHandler.KEY_EVENT_TYPE_ = goog.userAgent.GECKO ?
  78. goog.events.EventType.KEYPRESS :
  79. goog.events.EventType.KEYDOWN;
  80. /**
  81. * Handles key press events.
  82. * @param {!goog.events.BrowserEvent} e The key press event.
  83. * @private
  84. */
  85. goog.events.ActionHandler.prototype.handleKeyDown_ = function(e) {
  86. if (e.keyCode == goog.events.KeyCodes.ENTER ||
  87. goog.userAgent.WEBKIT && e.keyCode == goog.events.KeyCodes.MAC_ENTER) {
  88. this.dispatchEvents_(e);
  89. }
  90. };
  91. /**
  92. * Handles mouse events.
  93. * @param {!goog.events.BrowserEvent} e The click event.
  94. * @private
  95. */
  96. goog.events.ActionHandler.prototype.handleClick_ = function(e) {
  97. this.dispatchEvents_(e);
  98. };
  99. /**
  100. * Dispatches BeforeAction and Action events to the element
  101. * @param {!goog.events.BrowserEvent} e The event causing dispatches.
  102. * @private
  103. */
  104. goog.events.ActionHandler.prototype.dispatchEvents_ = function(e) {
  105. var beforeActionEvent = new goog.events.BeforeActionEvent(e);
  106. // Allow application specific logic here before the ACTION event.
  107. // For example, Gmail uses this event to restore keyboard focus
  108. if (!this.dispatchEvent(beforeActionEvent)) {
  109. // If the listener swallowed the BEFOREACTION event, don't dispatch the
  110. // ACTION event.
  111. return;
  112. }
  113. // Wrap up original event and send it off
  114. var actionEvent = new goog.events.ActionEvent(e);
  115. try {
  116. this.dispatchEvent(actionEvent);
  117. } finally {
  118. // Stop propagating the event
  119. e.stopPropagation();
  120. }
  121. };
  122. /** @override */
  123. goog.events.ActionHandler.prototype.disposeInternal = function() {
  124. goog.events.ActionHandler.superClass_.disposeInternal.call(this);
  125. goog.events.unlisten(
  126. this.element_, goog.events.ActionHandler.KEY_EVENT_TYPE_,
  127. this.handleKeyDown_, false, this);
  128. goog.events.unlisten(
  129. this.element_, goog.events.EventType.CLICK, this.handleClick_, false,
  130. this);
  131. delete this.element_;
  132. };
  133. /**
  134. * This class is used for the goog.events.ActionHandler.EventType.ACTION event.
  135. * @param {!goog.events.BrowserEvent} browserEvent Browser event object.
  136. * @constructor
  137. * @extends {goog.events.BrowserEvent}
  138. * @final
  139. */
  140. goog.events.ActionEvent = function(browserEvent) {
  141. goog.events.BrowserEvent.call(this, browserEvent.getBrowserEvent());
  142. this.type = goog.events.ActionHandler.EventType.ACTION;
  143. };
  144. goog.inherits(goog.events.ActionEvent, goog.events.BrowserEvent);
  145. /**
  146. * This class is used for the goog.events.ActionHandler.EventType.BEFOREACTION
  147. * event. BEFOREACTION gives a chance to the application so the keyboard focus
  148. * can be restored back, if required.
  149. * @param {!goog.events.BrowserEvent} browserEvent Browser event object.
  150. * @constructor
  151. * @extends {goog.events.BrowserEvent}
  152. * @final
  153. */
  154. goog.events.BeforeActionEvent = function(browserEvent) {
  155. goog.events.BrowserEvent.call(this, browserEvent.getBrowserEvent());
  156. this.type = goog.events.ActionHandler.EventType.BEFOREACTION;
  157. };
  158. goog.inherits(goog.events.BeforeActionEvent, goog.events.BrowserEvent);