menubase.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Definition of the MenuBase class.
  16. *
  17. */
  18. goog.provide('goog.ui.MenuBase');
  19. goog.require('goog.events.EventHandler');
  20. goog.require('goog.events.EventType');
  21. goog.require('goog.events.KeyHandler');
  22. goog.require('goog.ui.Popup');
  23. /**
  24. * The MenuBase class provides an abstract base class for different
  25. * implementations of menu controls.
  26. *
  27. * @param {Element=} opt_element A DOM element for the popup.
  28. * @deprecated Use goog.ui.Menu.
  29. * @constructor
  30. * @extends {goog.ui.Popup}
  31. */
  32. goog.ui.MenuBase = function(opt_element) {
  33. goog.ui.Popup.call(this, opt_element);
  34. /**
  35. * Event handler for simplifiying adding/removing listeners.
  36. * @type {goog.events.EventHandler<!goog.ui.MenuBase>}
  37. * @private
  38. */
  39. this.eventHandler_ = new goog.events.EventHandler(this);
  40. /**
  41. * KeyHandler to cope with the vagaries of cross-browser key events.
  42. * @type {goog.events.KeyHandler}
  43. * @private
  44. */
  45. this.keyHandler_ = new goog.events.KeyHandler(this.getElement());
  46. };
  47. goog.inherits(goog.ui.MenuBase, goog.ui.Popup);
  48. /**
  49. * Events fired by the Menu
  50. * @const
  51. */
  52. goog.ui.MenuBase.Events = {};
  53. /**
  54. * Event fired by the Menu when an item is "clicked".
  55. */
  56. goog.ui.MenuBase.Events.ITEM_ACTION = 'itemaction';
  57. /** @override */
  58. goog.ui.MenuBase.prototype.disposeInternal = function() {
  59. goog.ui.MenuBase.superClass_.disposeInternal.call(this);
  60. this.eventHandler_.dispose();
  61. this.keyHandler_.dispose();
  62. };
  63. /**
  64. * Called after the menu is shown. Derived classes can override to hook this
  65. * event but should make sure to call the parent class method.
  66. *
  67. * @protected
  68. * @override
  69. */
  70. goog.ui.MenuBase.prototype.onShow = function() {
  71. goog.ui.MenuBase.superClass_.onShow.call(this);
  72. // register common event handlers for derived classes
  73. var el = this.getElement();
  74. this.eventHandler_.listen(
  75. el, goog.events.EventType.MOUSEOVER, this.onMouseOver);
  76. this.eventHandler_.listen(
  77. el, goog.events.EventType.MOUSEOUT, this.onMouseOut);
  78. this.eventHandler_.listen(
  79. el, goog.events.EventType.MOUSEDOWN, this.onMouseDown);
  80. this.eventHandler_.listen(el, goog.events.EventType.MOUSEUP, this.onMouseUp);
  81. this.eventHandler_.listen(
  82. this.keyHandler_, goog.events.KeyHandler.EventType.KEY, this.onKeyDown);
  83. };
  84. /**
  85. * Called after the menu is hidden. Derived classes can override to hook this
  86. * event but should make sure to call the parent class method.
  87. * @param {?Node=} opt_target Target of the event causing the hide.
  88. * @protected
  89. * @override
  90. */
  91. goog.ui.MenuBase.prototype.onHide = function(opt_target) {
  92. goog.ui.MenuBase.superClass_.onHide.call(this, opt_target);
  93. // remove listeners when hidden
  94. this.eventHandler_.removeAll();
  95. };
  96. /**
  97. * Returns the selected item
  98. *
  99. * @return {Object} The item selected or null if no item is selected.
  100. */
  101. goog.ui.MenuBase.prototype.getSelectedItem = function() {
  102. return null;
  103. };
  104. /**
  105. * Sets the selected item
  106. *
  107. * @param {Object} item The item to select. The type of this item is specific
  108. * to the menu class.
  109. */
  110. goog.ui.MenuBase.prototype.setSelectedItem = function(item) {};
  111. /**
  112. * Mouse over handler for the menu. Derived classes should override.
  113. *
  114. * @param {goog.events.Event} e The event object.
  115. * @protected
  116. */
  117. goog.ui.MenuBase.prototype.onMouseOver = function(e) {};
  118. /**
  119. * Mouse out handler for the menu. Derived classes should override.
  120. *
  121. * @param {goog.events.Event} e The event object.
  122. * @protected
  123. */
  124. goog.ui.MenuBase.prototype.onMouseOut = function(e) {};
  125. /**
  126. * Mouse down handler for the menu. Derived classes should override.
  127. *
  128. * @param {!goog.events.Event} e The event object.
  129. * @protected
  130. */
  131. goog.ui.MenuBase.prototype.onMouseDown = function(e) {};
  132. /**
  133. * Mouse up handler for the menu. Derived classes should override.
  134. *
  135. * @param {goog.events.Event} e The event object.
  136. * @protected
  137. */
  138. goog.ui.MenuBase.prototype.onMouseUp = function(e) {};
  139. /**
  140. * Key down handler for the menu. Derived classes should override.
  141. *
  142. * @param {goog.events.KeyEvent} e The event object.
  143. * @protected
  144. */
  145. goog.ui.MenuBase.prototype.onKeyDown = function(e) {};