menubuttonrenderer.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.style.app.MenuButton}s and
  16. * subclasses.
  17. *
  18. * @author attila@google.com (Attila Bodis)
  19. */
  20. goog.provide('goog.ui.style.app.MenuButtonRenderer');
  21. goog.require('goog.a11y.aria.Role');
  22. goog.require('goog.array');
  23. goog.require('goog.dom');
  24. goog.require('goog.dom.TagName');
  25. goog.require('goog.style');
  26. goog.require('goog.ui.Menu');
  27. goog.require('goog.ui.MenuRenderer');
  28. goog.require('goog.ui.style.app.ButtonRenderer');
  29. /**
  30. * Renderer for {@link goog.ui.style.app.MenuButton}s. This implementation
  31. * overrides {@link goog.ui.style.app.ButtonRenderer#createButton} to insert a
  32. * dropdown element into the content element after the specified content.
  33. * @constructor
  34. * @extends {goog.ui.style.app.ButtonRenderer}
  35. * @final
  36. */
  37. goog.ui.style.app.MenuButtonRenderer = function() {
  38. goog.ui.style.app.ButtonRenderer.call(this);
  39. };
  40. goog.inherits(
  41. goog.ui.style.app.MenuButtonRenderer, goog.ui.style.app.ButtonRenderer);
  42. goog.addSingletonGetter(goog.ui.style.app.MenuButtonRenderer);
  43. /**
  44. * Default CSS class to be applied to the root element of components rendered
  45. * by this renderer.
  46. * @type {string}
  47. */
  48. goog.ui.style.app.MenuButtonRenderer.CSS_CLASS =
  49. goog.getCssName('goog-menu-button');
  50. /**
  51. * Array of arrays of CSS classes that we want composite classes added and
  52. * removed for in IE6 and lower as a workaround for lack of multi-class CSS
  53. * selector support.
  54. * @type {Array<Array<string>>}
  55. */
  56. goog.ui.style.app.MenuButtonRenderer.IE6_CLASS_COMBINATIONS = [
  57. [
  58. goog.getCssName('goog-button-base-rtl'), goog.getCssName('goog-menu-button')
  59. ],
  60. [
  61. goog.getCssName('goog-button-base-hover'),
  62. goog.getCssName('goog-menu-button')
  63. ],
  64. [
  65. goog.getCssName('goog-button-base-focused'),
  66. goog.getCssName('goog-menu-button')
  67. ],
  68. [
  69. goog.getCssName('goog-button-base-disabled'),
  70. goog.getCssName('goog-menu-button')
  71. ],
  72. [
  73. goog.getCssName('goog-button-base-active'),
  74. goog.getCssName('goog-menu-button')
  75. ],
  76. [
  77. goog.getCssName('goog-button-base-open'),
  78. goog.getCssName('goog-menu-button')
  79. ],
  80. [
  81. goog.getCssName('goog-button-base-active'),
  82. goog.getCssName('goog-button-base-open'),
  83. goog.getCssName('goog-menu-button')
  84. ]
  85. ];
  86. /**
  87. * Returns the ARIA role to be applied to menu buttons, which
  88. * have a menu attached to them.
  89. * @return {goog.a11y.aria.Role} ARIA role.
  90. * @override
  91. */
  92. goog.ui.style.app.MenuButtonRenderer.prototype.getAriaRole = function() {
  93. // If we apply the 'button' ARIA role to the menu button, the
  94. // screen reader keeps referring to menus as buttons, which
  95. // might be misleading for the users. Hence the ARIA role
  96. // 'menu' is assigned.
  97. return goog.a11y.aria.Role.MENU;
  98. };
  99. /**
  100. * Takes the button's root element and returns the parent element of the
  101. * button's contents. Overrides the superclass implementation by taking
  102. * the nested DIV structure of menu buttons into account.
  103. * @param {Element} element Root element of the button whose content element
  104. * is to be returned.
  105. * @return {Element} The button's content element.
  106. * @override
  107. */
  108. goog.ui.style.app.MenuButtonRenderer.prototype.getContentElement = function(
  109. element) {
  110. return goog.ui.style.app.MenuButtonRenderer.superClass_.getContentElement
  111. .call(this, element);
  112. };
  113. /**
  114. * Takes an element, decorates it with the menu button control, and returns
  115. * the element. Overrides {@link goog.ui.style.app.ButtonRenderer#decorate} by
  116. * looking for a child element that can be decorated by a menu, and if it
  117. * finds one, decorates it and attaches it to the menu button.
  118. * @param {goog.ui.Control} control goog.ui.MenuButton to decorate the element.
  119. * @param {Element} element Element to decorate.
  120. * @return {Element} Decorated element.
  121. * @override
  122. */
  123. goog.ui.style.app.MenuButtonRenderer.prototype.decorate = function(
  124. control, element) {
  125. var button = /** @type {goog.ui.MenuButton} */ (control);
  126. // TODO(attila): Add more robust support for subclasses of goog.ui.Menu.
  127. var menuElem = goog.dom.getElementsByTagNameAndClass(
  128. '*', goog.ui.MenuRenderer.CSS_CLASS, element)[0];
  129. if (menuElem) {
  130. // Move the menu element directly under the body (but hide it first to
  131. // prevent flicker; see bug 1089244).
  132. goog.style.setElementShown(menuElem, false);
  133. goog.dom.appendChild(goog.dom.getOwnerDocument(menuElem).body, menuElem);
  134. // Decorate the menu and attach it to the button.
  135. var menu = new goog.ui.Menu();
  136. menu.decorate(menuElem);
  137. button.setMenu(menu);
  138. }
  139. // Let the superclass do the rest.
  140. return goog.ui.style.app.MenuButtonRenderer.superClass_.decorate.call(
  141. this, button, element);
  142. };
  143. /**
  144. * Takes a text caption or existing DOM structure, and returns the content and
  145. * a dropdown arrow element wrapped in a pseudo-rounded-corner box. Creates
  146. * the following DOM structure:
  147. *
  148. * <div class="goog-inline-block goog-button-outer-box">
  149. * <div class="goog-inline-block goog-button-inner-box">
  150. * <div class="goog-button-pos">
  151. * <div class="goog-button-top-shadow">&nbsp;</div>
  152. * <div class="goog-button-content">
  153. * Contents...
  154. * <div class="goog-menu-button-dropdown"> </div>
  155. * </div>
  156. * </div>
  157. * </div>
  158. * </div>
  159. *
  160. * @param {goog.ui.ControlContent} content Text caption or DOM structure to wrap
  161. * in a box.
  162. * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
  163. * @return {Element} Pseudo-rounded-corner box containing the content.
  164. * @override
  165. */
  166. goog.ui.style.app.MenuButtonRenderer.prototype.createButton = function(
  167. content, dom) {
  168. var contentWithDropdown = this.createContentWithDropdown(content, dom);
  169. return goog.ui.style.app.MenuButtonRenderer.superClass_.createButton.call(
  170. this, contentWithDropdown, dom);
  171. };
  172. /** @override */
  173. goog.ui.style.app.MenuButtonRenderer.prototype.setContent = function(
  174. element, content) {
  175. var dom = goog.dom.getDomHelper(this.getContentElement(element));
  176. goog.ui.style.app.MenuButtonRenderer.superClass_.setContent.call(
  177. this, element, this.createContentWithDropdown(content, dom));
  178. };
  179. /**
  180. * Inserts dropdown element as last child of existing content.
  181. * @param {goog.ui.ControlContent} content Text caption or DOM structure.
  182. * @param {goog.dom.DomHelper} dom DOM helper, used for document ineraction.
  183. * @return {Array<Node>} DOM structure to be set as the button's content.
  184. */
  185. goog.ui.style.app.MenuButtonRenderer.prototype.createContentWithDropdown =
  186. function(content, dom) {
  187. var caption = dom.createDom(
  188. goog.dom.TagName.DIV, null, content, this.createDropdown(dom));
  189. return goog.array.toArray(caption.childNodes);
  190. };
  191. /**
  192. * Returns an appropriately-styled DIV containing a dropdown arrow.
  193. * Creates the following DOM structure:
  194. *
  195. * <div class="goog-menu-button-dropdown"> </div>
  196. *
  197. * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
  198. * @return {Element} Dropdown element.
  199. */
  200. goog.ui.style.app.MenuButtonRenderer.prototype.createDropdown = function(dom) {
  201. return dom.createDom(
  202. goog.dom.TagName.DIV, goog.getCssName(this.getCssClass(), 'dropdown'));
  203. };
  204. /**
  205. * Returns the CSS class to be applied to the root element of components
  206. * rendered using this renderer.
  207. * @return {string} Renderer-specific CSS class.
  208. * @override
  209. */
  210. goog.ui.style.app.MenuButtonRenderer.prototype.getCssClass = function() {
  211. return goog.ui.style.app.MenuButtonRenderer.CSS_CLASS;
  212. };
  213. /** @override */
  214. goog.ui.style.app.MenuButtonRenderer.prototype.getIe6ClassCombinations =
  215. function() {
  216. return goog.ui.style.app.MenuButtonRenderer.IE6_CLASS_COMBINATIONS;
  217. };