focushandler.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2006 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 event handler allows you to catch focusin and focusout
  16. * events on descendants. Unlike the "focus" and "blur" events which do not
  17. * propagate consistently, and therefore must be added to the element that is
  18. * focused, this allows you to attach one listener to an ancester and you will
  19. * be notified when the focus state changes of ony of its descendants.
  20. * @author arv@google.com (Erik Arvidsson)
  21. * @see ../demos/focushandler.html
  22. */
  23. goog.provide('goog.events.FocusHandler');
  24. goog.provide('goog.events.FocusHandler.EventType');
  25. goog.require('goog.events');
  26. goog.require('goog.events.BrowserEvent');
  27. goog.require('goog.events.EventTarget');
  28. goog.require('goog.userAgent');
  29. /**
  30. * This event handler allows you to catch focus events when descendants gain or
  31. * loses focus.
  32. * @param {Element|Document} element The node to listen on.
  33. * @constructor
  34. * @extends {goog.events.EventTarget}
  35. * @final
  36. */
  37. goog.events.FocusHandler = function(element) {
  38. goog.events.EventTarget.call(this);
  39. /**
  40. * This is the element that we will listen to the real focus events on.
  41. * @type {Element|Document}
  42. * @private
  43. */
  44. this.element_ = element;
  45. // In IE we use focusin/focusout and in other browsers we use a capturing
  46. // listner for focus/blur
  47. var typeIn = goog.userAgent.IE ? 'focusin' : 'focus';
  48. var typeOut = goog.userAgent.IE ? 'focusout' : 'blur';
  49. /**
  50. * Store the listen key so it easier to unlisten in dispose.
  51. * @private
  52. * @type {goog.events.Key}
  53. */
  54. this.listenKeyIn_ =
  55. goog.events.listen(this.element_, typeIn, this, !goog.userAgent.IE);
  56. /**
  57. * Store the listen key so it easier to unlisten in dispose.
  58. * @private
  59. * @type {goog.events.Key}
  60. */
  61. this.listenKeyOut_ =
  62. goog.events.listen(this.element_, typeOut, this, !goog.userAgent.IE);
  63. };
  64. goog.inherits(goog.events.FocusHandler, goog.events.EventTarget);
  65. /**
  66. * Enum type for the events fired by the focus handler
  67. * @enum {string}
  68. */
  69. goog.events.FocusHandler.EventType = {
  70. FOCUSIN: 'focusin',
  71. FOCUSOUT: 'focusout'
  72. };
  73. /**
  74. * This handles the underlying events and dispatches a new event.
  75. * @param {goog.events.BrowserEvent} e The underlying browser event.
  76. */
  77. goog.events.FocusHandler.prototype.handleEvent = function(e) {
  78. var be = e.getBrowserEvent();
  79. var event = new goog.events.BrowserEvent(be);
  80. event.type = e.type == 'focusin' || e.type == 'focus' ?
  81. goog.events.FocusHandler.EventType.FOCUSIN :
  82. goog.events.FocusHandler.EventType.FOCUSOUT;
  83. this.dispatchEvent(event);
  84. };
  85. /** @override */
  86. goog.events.FocusHandler.prototype.disposeInternal = function() {
  87. goog.events.FocusHandler.superClass_.disposeInternal.call(this);
  88. goog.events.unlistenByKey(this.listenKeyIn_);
  89. goog.events.unlistenByKey(this.listenKeyOut_);
  90. delete this.element_;
  91. };