menubuttonrenderer.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2008 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 Renderer for {@link goog.ui.MenuButton}s and subclasses.
  16. *
  17. * @author attila@google.com (Attila Bodis)
  18. */
  19. goog.provide('goog.ui.MenuButtonRenderer');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.style');
  23. goog.require('goog.ui.CustomButtonRenderer');
  24. goog.require('goog.ui.INLINE_BLOCK_CLASSNAME');
  25. goog.require('goog.ui.Menu');
  26. goog.require('goog.ui.MenuRenderer');
  27. /**
  28. * Renderer for {@link goog.ui.MenuButton}s. This implementation overrides
  29. * {@link goog.ui.CustomButtonRenderer#createButton} to create a separate
  30. * caption and dropdown element.
  31. * @constructor
  32. * @extends {goog.ui.CustomButtonRenderer}
  33. */
  34. goog.ui.MenuButtonRenderer = function() {
  35. goog.ui.CustomButtonRenderer.call(this);
  36. };
  37. goog.inherits(goog.ui.MenuButtonRenderer, goog.ui.CustomButtonRenderer);
  38. goog.addSingletonGetter(goog.ui.MenuButtonRenderer);
  39. /**
  40. * Default CSS class to be applied to the root element of components rendered
  41. * by this renderer.
  42. * @type {string}
  43. */
  44. goog.ui.MenuButtonRenderer.CSS_CLASS = goog.getCssName('goog-menu-button');
  45. /**
  46. * Takes the button's root element and returns the parent element of the
  47. * button's contents. Overrides the superclass implementation by taking
  48. * the nested DIV structure of menu buttons into account.
  49. * @param {Element} element Root element of the button whose content element
  50. * is to be returned.
  51. * @return {Element} The button's content element.
  52. * @override
  53. */
  54. goog.ui.MenuButtonRenderer.prototype.getContentElement = function(element) {
  55. return goog.ui.MenuButtonRenderer.superClass_.getContentElement.call(
  56. this,
  57. /** @type {Element} */ (element && element.firstChild));
  58. };
  59. /**
  60. * Takes an element, decorates it with the menu button control, and returns
  61. * the element. Overrides {@link goog.ui.CustomButtonRenderer#decorate} by
  62. * looking for a child element that can be decorated by a menu, and if it
  63. * finds one, decorates it and attaches it to the menu button.
  64. * @param {goog.ui.Control} control goog.ui.MenuButton to decorate the element.
  65. * @param {Element} element Element to decorate.
  66. * @return {Element} Decorated element.
  67. * @override
  68. */
  69. goog.ui.MenuButtonRenderer.prototype.decorate = function(control, element) {
  70. var button = /** @type {goog.ui.MenuButton} */ (control);
  71. // TODO(attila): Add more robust support for subclasses of goog.ui.Menu.
  72. var menuElem = goog.dom.getElementsByTagNameAndClass(
  73. '*', goog.ui.MenuRenderer.CSS_CLASS, element)[0];
  74. if (menuElem) {
  75. // Move the menu element directly under the body (but hide it first to
  76. // prevent flicker; see bug 1089244).
  77. goog.style.setElementShown(menuElem, false);
  78. goog.dom.appendChild(goog.dom.getOwnerDocument(menuElem).body, menuElem);
  79. // Decorate the menu and attach it to the button.
  80. var menu = new goog.ui.Menu();
  81. menu.decorate(menuElem);
  82. button.setMenu(menu);
  83. }
  84. // Let the superclass do the rest.
  85. return goog.ui.MenuButtonRenderer.superClass_.decorate.call(
  86. this, button, element);
  87. };
  88. /**
  89. * Takes a text caption or existing DOM structure, and returns the content and
  90. * a dropdown arrow element wrapped in a pseudo-rounded-corner box. Creates
  91. * the following DOM structure:
  92. *
  93. * <div class="goog-inline-block goog-menu-button-outer-box">
  94. * <div class="goog-inline-block goog-menu-button-inner-box">
  95. * <div class="goog-inline-block goog-menu-button-caption">
  96. * Contents...
  97. * </div>
  98. * <div class="goog-inline-block goog-menu-button-dropdown">
  99. * &nbsp;
  100. * </div>
  101. * </div>
  102. * </div>
  103. *
  104. * @param {goog.ui.ControlContent} content Text caption or DOM structure
  105. * to wrap in a box.
  106. * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
  107. * @return {Element} Pseudo-rounded-corner box containing the content.
  108. * @override
  109. */
  110. goog.ui.MenuButtonRenderer.prototype.createButton = function(content, dom) {
  111. return goog.ui.MenuButtonRenderer.superClass_.createButton.call(
  112. this, [this.createCaption(content, dom), this.createDropdown(dom)], dom);
  113. };
  114. /**
  115. * Takes a text caption or existing DOM structure, and returns it wrapped in
  116. * an appropriately-styled DIV. Creates the following DOM structure:
  117. *
  118. * <div class="goog-inline-block goog-menu-button-caption">
  119. * Contents...
  120. * </div>
  121. *
  122. * @param {goog.ui.ControlContent} content Text caption or DOM structure
  123. * to wrap in a box.
  124. * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
  125. * @return {Element} Caption element.
  126. */
  127. goog.ui.MenuButtonRenderer.prototype.createCaption = function(content, dom) {
  128. return goog.ui.MenuButtonRenderer.wrapCaption(
  129. content, this.getCssClass(), dom);
  130. };
  131. /**
  132. * Takes a text caption or existing DOM structure, and returns it wrapped in
  133. * an appropriately-styled DIV. Creates the following DOM structure:
  134. *
  135. * <div class="goog-inline-block goog-menu-button-caption">
  136. * Contents...
  137. * </div>
  138. *
  139. * @param {goog.ui.ControlContent} content Text caption or DOM structure
  140. * to wrap in a box.
  141. * @param {string} cssClass The CSS class for the renderer.
  142. * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
  143. * @return {!Element} Caption element.
  144. */
  145. goog.ui.MenuButtonRenderer.wrapCaption = function(content, cssClass, dom) {
  146. return dom.createDom(
  147. goog.dom.TagName.DIV, goog.ui.INLINE_BLOCK_CLASSNAME + ' ' +
  148. goog.getCssName(cssClass, 'caption'),
  149. content);
  150. };
  151. /**
  152. * Returns an appropriately-styled DIV containing a dropdown arrow element.
  153. * Creates the following DOM structure:
  154. *
  155. * <div class="goog-inline-block goog-menu-button-dropdown">
  156. * &nbsp;
  157. * </div>
  158. *
  159. * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
  160. * @return {Element} Dropdown element.
  161. */
  162. goog.ui.MenuButtonRenderer.prototype.createDropdown = function(dom) {
  163. // 00A0 is &nbsp;
  164. return dom.createDom(
  165. goog.dom.TagName.DIV, goog.ui.INLINE_BLOCK_CLASSNAME + ' ' +
  166. goog.getCssName(this.getCssClass(), 'dropdown'),
  167. '\u00A0');
  168. };
  169. /**
  170. * Returns the CSS class to be applied to the root element of components
  171. * rendered using this renderer.
  172. * @return {string} Renderer-specific CSS class.
  173. * @override
  174. */
  175. goog.ui.MenuButtonRenderer.prototype.getCssClass = function() {
  176. return goog.ui.MenuButtonRenderer.CSS_CLASS;
  177. };